Android app language localization - android

I have developed an app. Now I want to support other regional languages in it. But now the app is almost ready, changing each java file to set the texts in the textview for the corresponding language is a big pain. Is there any other simple way to do that? Like making a different string.xml file and doing some stuff, then all the views will get changed???

Create alternative resources
A large part of localizing an app is providing alternative text for different languages. In some cases you also provide alternative graphics, sounds, layouts, and other locale-specific resources.
An app can specify many res// directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination. (The name of a resource directory must conform to the naming scheme described in Providing Alternative Resources, or else your app cannot compile.)
Example:
Suppose that your app's default language is English. Suppose also that you want to localize all the text in your app to French, and most of the text in your app (everything except the app's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:
res/values/strings.xml Contains English text for all the strings
that the app uses, including text for a string named title.
res/values-fr/strings.xml Contain French text for all the strings,
including title.
res/values-ja/strings.xml Contain Japanese text for
all the strings except title.
If your Java-based code refers to R.string.title, here is what happens at runtime:
If the device is set to any language other than French, Android loads title from the res/values/strings.xml file.
If the device is set to French, Android loads title from the res/values-fr/strings.xml file.
Notice that if the device is set to Japanese, Android looks for title in the res/values-ja/strings.xml file. But because no such string is included in that file, Android falls back to the default, and loads title in English from the res/values/strings.xml file.

If you properly used resource references (like R.string.hello) You can create another language configuration and it should be properly loaded. If you "hard coded" strings within JAVA code with literals like "hello world" You have quite a bit of refactoring to do.

Related

How to know if Android resolved to use a localized strings.xml file or the default one?

The motivation of this question comes from this other question:
How do I get the current language my app is in? (Not the device's language as specified in the Settings. I want the language that Android resolved to use for my app).
This question has been asked several times on the site, but it fails to consider this corner-case:
Imagine the user has only one preferred language in his device: say German, for example.
My app two strings.xml files: The default one, and one in French (values-fr/strings.xml).
Obviously, Android will resolve to use the default strings.xml in this case.
But if I do any of the following, it will return German:
Locale.getDefault()
getResources().getConfiguration().getLocales().get(0)
getResources().getConfiguration().locale.
(And many other suggestions that I have found on the site)
And who told Android that the default strings.xml file was in German? Why did it made that assumption? The default file could be in Spanish, Italian, Polish...whatever.
Ideally, I would like a method that returns null in this case. Some method that tells me that no match was found for German and Android had to fall-back to the default strings.xml.
Does such method exist?
Put the language name in both strings.xml files. For example, as languageName.
When you get the string for R.string.languageName, it will be the language chosen by Android among the ones you support.
Those functions all return the phone's locale. They have nothing to do with resource localization. So nobody said the strings.xml file was German. The user set the phone to German, and the resource subsystem decided strings.xml was the best match for that. Basically you have the way it works backwards.
I don't think there is a way to get what you want for two reasons:
1)It's supposed to be transparent to the programmer.
2)It doesn't pick one file over the other. It picks independently for each string. So if you have a strings.xml with two strings A and B, and had a german strings file with only A, it would give you the german A and the default B.

How does Android handle unsupported languages?

I've made an app that runs in multiple languages, but if the user runs it in an unsupported language, will it display the main one?
If it is the case, how can I make it display in English instead?
You are right.
Just put your english strings on the strings.xml file, so it will be loaded by default if no other language matches the phone lang.
Hope this helps.
if the user runs it in a not supported language, will it display the main one?
The default language, yes See the docs for detailed information.
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml. If this default file is absent, or if it is missing a string that your application needs, then your application will not run and will show an error.
how can I make it to display English instead?
Also see the docs for that, you should use english in the default strings.xml
How to Create Default Resources
Put the application's default text in a file with the following location and name:
res/values/strings.xml (required directory)
The text strings in res/values/strings.xml should use the default language, which is the language that you expect most of your application's users to speak.
Depends really what language are you using in your string.xml (main one). If there is no language case for what the user selects in the Settings, the app will load in the default values from the strings.xml
Language which you want to load by default then put that strings.xml
in values
folder without any suffix with values folder
In fact when you run your app, the app will check if the locale is available thanks to the string.xml contains in the value-locale folder. If nothing is found the string.xml in values is used so just put english traduction in your string.xml.
I hope to be clear.

android localization resource chooser

I have an android application on eclipse it's text-views and the buttons' texts on Arabic language , I want to localize my application to both Arabic and English users .
Firstly under res folder I have created another folder and named it values-en .
I let the original folder values as is it and later I will use it for Arabic .
My first question it is necessary to create another folder (values-ar) for Arabic localization issues.
Or original values folder is just enough?.
Secondly
On values-en folder I created strings.xml file and added the following statement to it
COMTAS
I set the text of one of the textviews from resource chooser so choose application-title .
after that the textview's text converted to #string/application-title.
the project gives me the following message
NOTE: This project contains Java compilation errors, which can cause rendering failures for custom views. Fix compilation problems first.
Can someone explains to me how to make localization in simple steps.
And how I can retrieve textviews' texts either in english or arabic (reading from values folder)?
First: Leaving a strings.xml in the "values" folder will cause this language to be "default" (or rather "fallback") language.
Second: The text is read automatically from the strings.xml placed in the values-folder matching your System Locale (the locale you get when you call Locale.getDefault()), e.g. if system locale is set to english, the values are taken from values-en folder. Therefore it would be wise to place the arabic values in a values-ar folder.
I can't say anything related to your compile errors as you didn't post the code causing the error.
It's always good to follow the documentation. Here is the link for Localizing with Resources.
Create a values-ar folder for the arabic language, and let the default language as english. Why?
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml.
So if your application is running on a device set to Spanish language, it would still load arabic language (if it's set as default).
English is a most talked language, which would be better to let english by default, ant let the arabic language in values-ar(in my humble opinion).
Hope this helps you.

How to show Locale List based on the Resource availability

I am developing an application that fetches all the system available locales and show in a listview to be selected by user to change the application language(just like android's language change option).
But I have a few res/values folder in my application resources like values (default), values-bn, values-zh, values-ar. SO I want these 4 languages to be shown in the list. How can I do this? I need to do this programmatically.
Suppose, I have 4 folders values, values-bn, values-en, values-zh
Then I only show,
Bengali
English
Chinese
OK, so what you want to do is have four languages: English, Bengali, Chinese, and Arabic, have I got that right? (Your question is internally inconsistent so it's hard to tell).
And you want to have a set of fallback values for when a string is missing in one of those four languages. The fallback values are in English, although the system won't know or care about that.
So what you want to do is put the strings for the four languages in the values-en, values-bn, values-zh, and values-ar directories and the fallback strings in the values directory.
In fact, since your fallback strings are in English, you could just put them in the values directory and have an empty resources file in the values-en directory. English would still show up in the list.
But why not just put the English versions of the strings in the values-en directory?

internationalisation in android

I am making an app in which I want to implement internationalization.
I have created alternative resources like
res/values-fr/strings.xml
which Contains French text for all the strings, including title
Can anyone tell me what to do next...
thanks
You should always have default strings in res/values/strings.xml, because Android tries to use the most specific resource available. If you have for example res/values-fr/strings.xml and res/values-de/strings.xml and the users phone is set to English, your app will crash because neither de nor fr are applicable for English there are no fallback resources.
After you have specified your default strings and any translations in their respective subfolders, you can use the strings by their qualifiers. For example R.string.some_string. Android will then use the most appropriate translation that is available for the users current device language.
All that and more is explained here: Localizing with Resources
Device will load locale automatically based on system languge. No extra steps required unless you want to change locale in your app independently.

Categories

Resources