using different string files in android - android

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.

Related

Store data for dif . language

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)

Android. How change default locale fo string.xml in values

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());
}

How to load alternative strings.xml from resources programmatically

I want to provide three different langauges. The default language is english so all my string values in the values folder are in enlish. I created two other folders
values-de for the german language
values-cn for the chinese language
each folder contains a strings.xml file where i defined the values in german for the de folder and in chinese for the cn folder.
My question now is: How can i load a different language programmatically because i want to provide buttons in my app interface where the user can switch the language. The settings of the device wont be editable for our users. Our users can just see the app itself and nothing else so i have to provide the language switching from within my application.
You can change the configuration at runtime
Do this in onCreate of the activity
String languageToUse = "de"; // The language you want to change
Locale locale = new Locale(languageToUse);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

Android supporting different languages.

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.

Get String From Different Values Folder in Android

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! :)

Categories

Resources