Reputation: 1138
I have made a project in which I am using different language's xml,like language_hi.xml
,language_en.xml
. so according to documentation I have made different folder for my each xml. For example for language_hi.xml I have made es/values-hi
for language_en I have made res/values-en and in my each xml the string name is same,is there any way to select language_en.xml on one button click and language_hi.xml on another event.
this is my xml
res/values-hi/language_hi.xml
<string name="hello">sachit</string>
<string name="app">colon</string>
res/values-en/language_en.xml
<string name="hello">astro</string>
<string name="app">dev</string>
Upvotes: 2
Views: 1260
Reputation: 67522
You can select the Locale
to use at runtime; for example, setting it to English will result in the values from the res\values-en\...
folder being used.
To do this, the following code is necessary:
Resources res = getResources(); // From your context
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = new Locale("en"); // For English
res.updateConfiguration(conf, dm); // Now, update the language
However, I strongly recommend against using this. When a user runs an app, they expect it to either be in the default language (which in many cases is English), or their localized language. Giving someone an app in Hindi, for example, when they are in Russia, is really no good.
Keep in mind, that if you are using en
for your entire app, getting a value from languages_hi.xml
is incredibly difficult and, really, should not be done in any situation. An application should be the same language throughout, or it will begin to confuse the user (with the exception of apps that handle translations between languages). However, if you are interested in doing this (for whatever esoteric reason), you should review this answer.
PS: I assume you know this, but I should mention it anyway. Any strings in the res\values\...
folder are used if the locale which the phone uses is not specified using res\values-xx\...
. You can find information on this here.
Upvotes: 2
Reputation: 3294
No, there is not. Android decides language according to device's language on runtime. You can force to change device's language but it is not a well solution. Instead you can produce your own solution, and you can change every string by yourself.For instance, you can put language option to your preference. then according to chosen language you can show strings of that language. suppose you have a message like "please enter password". you can write a method getStringAccordingToLanguage("password_warning"). This method will return "please enter password" if the language is in English. you can put this method in place of strings.
Upvotes: 1