{"id":153,"date":"2016-06-21T18:45:42","date_gmt":"2016-06-21T18:45:42","guid":{"rendered":"https:\/\/wp.local\/?p=153"},"modified":"2016-06-23T05:43:17","modified_gmt":"2016-06-23T05:43:17","slug":"multi-language-support-and-spinnerpreference-part-i","status":"publish","type":"post","link":"https:\/\/blog.c-devtech.net\/?p=153","title":{"rendered":"Multi-Language support and SpinnerPreference &#8211; Part I"},"content":{"rendered":"<p>When I first started Android programming, I felt the best way to learn was to try and implement various features I felt were important in my Windows programming.\u00a0 So after writing a simple Android notes application, I looked into what it would take to make it multi-language. For added flexibility, I decided to make the app&#8217;s language a user setting, which could be independent of the current locale of the device.<\/p>\n<p><strong>In Android, adding support for multiple languages<\/strong> is pretty straight forward: just create separate <em>strings.xml<\/em> files in <em>values<\/em> directories appended with a hyphen and the <a href=\"https:\/\/github.com\/championswimmer\/android-locales\" target=\"_blank\">ISO language code<\/a> inside your applications res directory. For example in Windows:<\/p>\n<pre class=\"classic no-pretty-print\">MyProject\\app\\src\\main\\\r\n    res\\\r\n       values\\\r\n           strings.xml\r\n       values-es\\\r\n           strings.xml\r\n       values-fr\\\r\n           strings.xml<\/pre>\n<p>At runtime, Android will load the correct string file based on the locale set for the device. For more information on this check out <a href=\"https:\/\/developer.android.com\/training\/basics\/supporting-devices\/languages.html\" target=\"_blank\">Supporting Different Languages<\/a><\/p>\n<p>One thing to remember is that string literals are discouraged in Android, which brings up the issue of strings that shouldn&#8217;t be translated. To get around this, put all the strings that need to be translated in the language specific <em>strings.xml<\/em> files and then create a file in the values directory called <em>literals.xml<\/em> for strings that do not need to be translated. In<em> literals.xml<\/em> you need to tell the compiler that the strings in the file do not need to be translated which is done by the following line:<\/p>\n<pre>&lt;resources xmlns:tools=\"http:\/\/schemas.android.com\/tools\" \r\ntools:ignore=\"MissingTranslation\"&gt;<\/pre>\n<p><strong>To set the locale of the device<\/strong>, the same <a href=\"https:\/\/github.com\/championswimmer\/android-locales\" target=\"_blank\">ISO language code<\/a> used to name the <em>values<\/em> directories is used to create a region specific locale that then gets passed to the <em>updateConfiguration<\/em> function:<\/p>\n<pre>Locale locale = new Locale(\"en\");\r\nlocale.setDefault(locale);\r\nconfig.locale = locale;\r\ngetBaseContext().getResources().updateConfiguration( config, \r\ngetBaseContext().getResources().getDisplayMetrics());\r\n\r\n\/\/ Since new language, force the activity to restart\r\nrecreate();<\/pre>\n<p>In part II we&#8217;ll look at saving state in an Android app and preference dialogs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I first started Android programming, I felt the best way to learn was to try and implement various features I felt were important in my Windows programming.\u00a0 So after writing a simple Android notes application, I looked into what &hellip; <a href=\"https:\/\/blog.c-devtech.net\/?p=153\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-android-app-develoment"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1IEK3-2t","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":11,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}