I have an application with only russian locale. If I am not mistaken, the string.xml in the res/values is the english locale by default. But english and russian have different plurals. For example:
In russian:
1,21,31..x1 книга
2-4, 2(2-4), 3(2-4), .., x(2-4) книги
in other cases - книг
In english:
1 book
n books
Problems begin when the user changes system language from russian to other language.
How can I change default language for my application? Or maybe it is possible to force the application to use the russian plurals?
I finally found the answer:
https://developer.android.com/studio/write/tool-attributes#toolslocale
<resources xmlns:tools="http://schemas.android.com/tools"
tools:locale="es">
From the doc:
This tells the tools what the default language/locale is for the resources in the given element (because the tools otherwise assume English) in order to avoid warnings from the spell checker. The value must be a valid locale qualifier.
you can create different String.xml files
like this
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Check google official document https://developer.android.com/training/basics/supporting-devices/languages.html
& https://developer.android.com/guide/topics/resources/localization.html
create new value folder values-ru and place string.xml in that.
in java change your app language programatically with this code. for change to rushion call method with ru parameter like this changeLanguage("ru").
public void changeLanguage(String languageToLoad) {
//for language change
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}
Related
I'm new to Android. I'm planning quiz app in several languages. Pictures will be same for all languages, but questions, answers and add. Information will be different. It can be 300 - 500 sentences for every language.
Should I use string res in dif directories for dif languages or maybe better way exists? SQL or something?
you can do that with a string.xml file in your resources folder. Create different folders for each language (values-en, values-es, etc) and inside these folders add the strings.xml file with your text resources. Use the same tags in all files and store a different value for each language. Then populate your views with the corresponding xml as you please
example:
\res\values-en\strings.xml:
<resources>
<string name="greeting">Hello</string>
</resources>
\res\values-es\strings.xml:
<resources>
<string name="greeting">Hola</string>
</resources>
and this is how you change the language 'locale' programmatically:
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
make sure in your manifest you include the following under your activity:
android:configChanges="locale"
(source)
Which language does android treats default strings.xml as?
Specifically, when there are <plurals> tags inside default strings.xml, rules for which language will Android pick? Is there a way to specify that?
It doesn't treat default values folder as a language dependant. It's basically a fallback if it doesn't find a better match. You can read more here.
Android will pick plurals rules for language that is currently selected in the phone(e.g. if users has german language then android will pick german rules).
Use this to change the language by programmatically--
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
Write the countrycode of language in place of "en_US" whatever language you want...like for japanese--"ja_JP" For Arabic--"ar" or check this link for code of country--
http://code.google.com/apis/igoogle/docs/i18n.htmlenter code here
I only want set only default English language of application in android.
After Supporting Different Languages, I also use values/strings.xml to store characters in that file.
If in English language mode of System Settings, I can show correct words.
If in Chinese language mode of System Settings, The English words was translated to Chinese.
I don't want it translate automatically.
People who know how to set only default English language,
Please help me.
Thanks,
I can get correct answer now via the codes in below :
public static void setDefaultLanguage(Context context, String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
In that, what is lang value? You should get correct Language System to pass this value to lang parameter.
Thanks,
Are you storing all the strings for both languages in one .xml? If so that is probably your issue…
res/values/strings.xml
Contains English text for all the strings that the application uses,
res/values-ja/strings.xml - for japanese, but I am sure you can find one for the language you are looking for! Android looks for the "values-ja" tag.
I have one android application supporting diff languages.
I have different versions of string for every language like.
values-aa, values-bb and values-cc etc.
But my application is not displaying strings in diff languages.
I have done all the necessary thing for multiple language support but still some times app does not display text in diff. languages.
What can be the cause?
Take a look here; as stated:
Create Locale Directories and String Files
To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO country code at the end of the directory name. For example, values-es/ is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
English (default locale), /values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
Spanish, /values-es/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
French, /values-fr/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mon Application</string>
<string name="hello_world">Bonjour le monde !</string>
</resources>
Note: You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see Localization.
I guess that at first glimpse you have to change the folder names.
You have to handle and change the default phone Locale.
Imagine that you select the portuguese language (tag = "pt_PT"):
Locale locale = new Locale("pt_PT");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Now when you do: getString(R.string.YOUR_STRING_NAME) it will return the string located at values-pt.
Remember to add this to your Manifest (in the activity that you whant to control the language):
android:configChanges="locale"
It is very easy just try some step which is provided in this link
Some code for reference
String languageToLoad = "hi"; // 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.activity_main);
Its work. Best Luck :)
In test you must change the device language to get the specific language of your app and the user to if he don't change the device language he will get the default language.
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.