I have just finished developing an android application that I was asked to make. I had also planned to translate it in different languages; below you can see the tree of the folders/files:
Inside strings.xml there are all the values that I am using inside the classes and they are all in Italian, my language (Here you can see an example if needed). I'd like to be able to translate all those fields in English and french as well.
I have read this article -> http://developer.android.com/training/basics/supporting-devices/languages.html
From what I have understood, I'd have to create a folder called values-fr with a strings.xml inside (it will have the same values names, but different translations).
QUESTION
I would like to be able to support English and Italian as well, but I don't understand how to manage the folders. Could you please tell me which option is the correct?
Inside the folder values use strings.xml with Italian (my language) strings. Then create another folder called values-en and, like I did before, create strings.xml with English values
Inside the folder values use strings.xml with English strings. Then create another folder called values-it and then, create strings.xml with Italian values
I guess that the point 2 is the correct solution but I'm not sure. The folder values has English as default language?
Thanks for the attention.
Technically speaking both are valid. values/ is the fallback if a more specific qualifier is not found. There is no written rule that enforces you in having the English strings in values/. It just makes a little more sense to have the English as fallback since it easier to find foreigners that speak English rather than Italian
Related
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.
I would like to know the difference between the following
values-b+de
and
values-de
These both folders are used for localization. The way that you have implemented both values folder will be select the country code of the "de" language.
In values-b+de folder you are passing the language code as null so it will select the default language code and in values-de you are the call for default language code.
Creating the directory, the format is as below.
<resource type>-b+<language code>[+<country code>]
For more details, you can go to this link
Actually there is not difference between them as you write them.
The values-b convention was introduced in Android 7 (API level 24) in order to Improve the resource-resolution strategy as mentioned here:
https://developer.android.com/guide/topics/resources/multilingual-support.html#postN
And here also:
https://developer.android.com/training/basics/supporting-devices/languages.html#CreateDirs
Example:
For Spanish in general you will have resources in values-b+es folder.
But if you want these resources to make influence only for Spanish in Latin america you will have to place you're resources in values-b+es+419 (which was values-es-rUS in the old convention).
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.
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?
Someone please explain what is the main idea of using strings.xml?
I think it would be useful for multi-language support but how can we organise it for that?
Do I need it if I don't want to use multi-language support in my android application?
The idea is that it represents a single location for various strings, so your code isn't peppered with string literals. In addition to that, you gain the ability to easily localize. Organization of files for localization is covered here:
http://developer.android.com/guide/topics/resources/localization.html#creating-alternatives
Do you need it if you're not localizing? No. But it may make things easier in the long run, and I would recommend using it just for that reason.
Hard-coding strings is Bad.
Parameterizing strings (e.g. with strings.xml) is Good.
Being able to internationalize your strings (with language and/or locale-specific versions of strings.xml) is even Better :)
PS:
To make use of internationalization, just create resource subdirectories. Google will give you plenty of references/examples. Herre's one:
http://developer.android.com/guide/topics/resources/localization.html
* res/values/strings.xml
Contains English text for all the strings that the application
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...
And yes, you should absolutely get in the habit of using strings.xml (and colors.xml and dimens.xml etc etc) even if you don't plan on internationalizing immediately.
IMHO....