I have a TextView that displays a message to the user depending on the situation. I get the string to change depending on the situation and that's working great.
My problem is, how do I manage that for different languages?
I've looked around and most suggestions are to use a separate string.xml depending on the locale. But since this is only one string resource that changes its content programmatically it did not really work.
Is there a way to have something like
if(language is blabla)
change the text to "this"
or something of the sort.
You can programatically check the user's language by using class:
http://developer.android.com/reference/java/util/Locale.html
i.e.
Locale.getDefault().toString()
Next you should use path to the appropriate string.xml
You only change bold part:
values-en-rMU
I managed this by using SQLite database. I had 5 different tables for 5 languages. In my listAdapter I imported tables name from query:
SELECT * FROM dbname.sqlite_master WHERE type='table'
Next, in one singleton class called OutputStrings I import all used strings for UI.
From each Activity Views are reading strings from OutputStrings.
Best Regards,
You can easily change your local to your desired local:
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration mConfig = new Configuration();
mConfig.locale = locale;
getBaseContext().getResources().updateConfiguration(mConfig,
getBaseContext().getResources().getDisplayMetrics());
Related
Basically what I am trying to do is,
I start with MainActivity(activity_main) go to SettingActivity(activity_setting) & change my locale from given options(like french, english, dutch etc.)
So what I have done till now is...
OnClick of language name it re-create SettingActivity(activity-setting) & change it's string values according to language selected.
What I really want is Without re-creating Activity, All string values should be applied according to that selected language.
All suggestions & Answers are greatly appreciated.
Thank you in Advance.
Simply change the language property first.
Then call a new function that sets all strings displayed in the UI to the right language.
Pseudocode:
TextView myTextView = (TextView) findViewbyId(R.id.tv1);
myTextView.setText(yourCustomGetLocaleFunction("some id of the string you want to display", "some language name"));
But that's a bad approach. You should let Android handle the language of your app. Just localize the strings.xml file (you can load strings from the file from code).
Edit:
To change the language of your app for the moment, you can use the Code from this solution:
How to change android app language without changing phone language?
I'm working on a app in Xamarin.Android and it has to support English and Bulgarian. Most of the text is stored in the Strings.xml in the values folder.
The English text is in the default values folder and the Bulgarian text is in values-bg.
The filename for both is String.xml, and the name of each string is the same as it's translated version.
Now how can I make it so that when i click one button it reads the strings from values-bg and when i click another it reads from the default again? Also I would like that to be so for every activity in the app.
I've been testing how to change locale programmatically, and I finally found a solution that worked for me.
As you said, you need a values folder, containing strings.xml with default values, which would be english, then a values-bg folder, with the same file but with Bulgarian translated values. It is important that strings have the same name, so you did it right.
Then, in your click event, you can add this :
var locale = new Java.Util.Locale ("bg");
Android.Content.Res.Configuration conf = Resources.Configuration;
conf.Locale = locale;
DisplayMetrics dm = Resources.DisplayMetrics;
Resources.UpdateConfiguration (conf, dm);
Recreate ();
First, that code will declare a Locale variable for "bg" locale. Then, it gets the actual configuration of the device, and apply the new Locale to it. Using UpdateConfiguration, it save the changes. The Recreate is optional, it just, as its name explains it all, recreates the current activity, which will be loaded using the new configuration, with a locale "bg" instead of whatever default locale was applied. It will now fetch strings in the values-bg folder.
NOTE : I've read in the comments of the accepted answer here that on some devices, the configuration will reset to default after some time in the app. I'm not sure why this happens, but you might want to check if the locale has been changed somehow when loading a new activity, to prevent that rollback.
I'm porting my iPhone app to android and I'm having a problem with the string files now.
The app is a translation tool and users can switch the languages, so all the localized strings are in both languages and they are independent from what locale the OS is running.
For iOS version I have different files like de.strings, en.strings and fr.strings and so on. For every target with specified language pair I read the strings from the string tables, e.g. for de-fr I will include de.strings and fr.strings in project and set the name of the string tables in the info-list file and read strings from them. In the end I have one project containing different targets (with different info-list files) and all are well configured.
I'm intending to do the same on android platform, but
Is only one strings.xml allowed per project?
How do I set different build target? e.g. my de-fr and de-en translation app are actually two apps where the only difference is the language pairs. How can I set something so that I can use one project to generate the two apps? In XCode it's simple but I can't find a solution with eclipse.
How do I specify per target which strings.xml it should read?
Thank you for your answers but Please Note that I need OS locale independent language settings, i.e. if the user changes OS locale from en to de, my app still shows localized strings in English. What I'm asking is actually how I can set something like prebuild and load different string files for different targets.
Automatic locale selection, according to user settings
The strings.xml contains the original text, assuming for the English language.
To create translations into different languages you can create folders, for example:
values-gr, values.it, for the Greek end Italian.
Just copy strings.xml into those folders and translate it.
On application launch, OS automatically picks a language according to the user's preferences.
Manually locale selection, overriding user settings
To force Greek for example you can use:
Locale locale = new Locale("gr");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
You should, of course, provide a Greek translation for this to work.
Read more
You can check the documentation here:
Support Different Languages - Android
you have to put your localized strings in different folders like values-es, values-de, values-fr, etc.
The file must contain the same keys, for example in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello</string>
</resources>
in values-es folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hola</string>
</resources>
and so on.
You have to create one values folder for each language adding the language ISO code of the language you want to have a translation using this format: values-es, values-de, ... In each folder you have to add a strings.xml with strings of its language.
The values folder (withoud country code) will be the default language.
For choose the string language you want to use:
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String languageToLoad = "fa"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);
}
}
ad 1. No you can have as many as you want. See ad 3 for more information.
ad 2. ????
ad 3. To make language selection in our app you should update context. Then proper xml will be selected automatically. Read about this to see how to do it.
I wanna develop android application for different types of languages. So I have used localization for it. For that I have created different values folder like values-fr, values-ja , values-de. N also created strings.xml with static value according to that values folder. So All is good. But now my question is that I wanna change UI text according to user's selection of language. So How can I manually get particular string values from values->string.xml for particular language???
I think it may be easy, But I have no idea.
Thanks,
Locale locale = new Locale("cn");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
http://developer.android.com/guide/topics/resources/localization.html
You just need to get the String from the file like this way
String string = getString(R.string.hello);
Android will choose the folder based on the mobile locale. See this link for further information about Android localization.
http://developer.android.com/guide/topics/resources/localization.html
And this one for String resources if you still have some doubts on how to acces the strings.
http://developer.android.com/guide/topics/resources/string-resource.html
Hope it helps, good luck! :)
I am making an android app in which i want to change the language of app on selecting a particular language in spinner without changing the language of device.I have made different string files for all language. Now what to do next??
Can anyone please help me over this?
thanks
Most simple way is just to change VM's locale, like:
Locale locale=new Locale("zh"); //Chinese
Locale.setDefault(locale); //set VM's default locale
Once I wanted to implement a multiple language feature for my application. Where that language was not even supported by android.
I made the appropriate strings for all the application and stored them in my resources.
Then I kept the language selection in my shared preferences, so that when user opens the app the next time, we can show the previously selected language. I implemented the code and language changing in my OnResume() function of Activity. the code was like:
if(SelectedLanguage.compareTo("ar")==0)
{
String text = getString(R.string.ar_Options);
tv_Options.setText(ArabicUtilities.reshape(text));
text = getString(R.string.ar_Minimize);
tv_Minimize.setText(ArabicUtilities.reshape(text));
}
else
{
String text = getString(R.string.en_Options);
tv_Options.setText(text);
text = getString(R.string.en_Minimize);
tv_Minimize.setText(text);
}
I hope you get the basic idea. In this way you don't have to change the lanuguage of your device and you can provide multiple languages for your application.