I'm going to translate my application strings.xml file. Which is default language of strings.xml file? because now i need to support italian (the language with i've write strings.xml for now) and english. Should i use string.xml for english and create
res/values-it/
folder for italian, and translate "default" strings.xml in english?
strings.xml is the default and will be used if there is no country specific file like res/values-it/strings.xml or res/values-fr/strings.xml. Read more about Localizing with Resources.
I would personally use strings.xml with english translations as a fallback as you already suggested.
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-it/ 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>
Italian, /values-it/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">la mia domanda </string>
<string name="hello_world">ciao mondo!</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.
If you want to have only one strings.xml file and you would like to localize to Italian, all you need to do is add the following line to your strings.xml file.
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="it">
locale="it" -> Italian, locale="fr" -> French, etc.
Related
I am translating my app to the Marathi language. I've made the value-mr-rIN/strings.xml for the Marathi language.
When I change my device language to Marathi the app still uses the default strings.xml which is English instead of using the values-mr-rIN/strings.xml.
What's the problem? I tried testing it on other devices still it shows English strings. I am pasting the code below
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">App name</string>
<string name="test">this is a test</string>
</resources>
values-mr-rIN/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">अॅप नाव</string>
<string name="test">हे एक चाचणी आहे</string>
</resources>
A single line of code in gradle was causing this issue.
defaultConfig {
..
resConfigs "en"
}
The above line in gradle means use only English language resource. Which is used to save space by removing other language resources from the app.
I changed it to..
defaultConfig {
..
resConfigs "en" ,"mr"
}
Now it uses English or Marathi resources with respect to the device language.
May this helps you:-
just change your folder name
values-mr/strings.xml
To
values-mr-rIN/string.xml
After making this change it will work 100%.
See Demo Example:
Regular string.xml Output:-
Marathi string.xml Output:-
I translated my application in the Japanese language. I added regularly the xml file, inside the folder values-ja-rJP .
Android studio is regularly set in UTF-8, but when I start my application on my device, the application uses the default language. I need to do something? the problem occurs only with the Japanese language. Thank you
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="action_settings">設定</string>
<string name="new_field">新規分野</string>
...
...
</resources>
I have the following xml code:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
In the xml code i found two warning first that dp contains that which i got the waring to use sp indeed. What is the reason it showing so?
Second warning and may be error is that i am using android:text="Press Button" it tell me to use #string indeed. If i uses the same #string is displayed in text which look awkward. What is the reason for it!
Hardcoded String value in View is not recommeded by developer.android.com as making of Android Application compatible with different languages is twisted up to.
Referenced from
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>
Referring to your OP:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="press">Press Button</string>
</resources>
This layout XML applies a string to a View:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/press"
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
This application code retrieves a string:
String string = getString(R.string.hello);
Use sp for setting size as suggested by developer.android.com
sp : Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
XML file saved at res/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="font_size">16sp</dimen>
</resources>
This application code retrieves a dimension
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
This layout XML applies dimensions to attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="#dimen/font_size"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"
/>
Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp)
from the developer.android
Android stander TextView size is use SP and you are hardcode String that i give warning.
Please User your String.xml in value folder and select this String then it do not give any error.
Thanks
It's better to use SP instead of DP.
It's recommend to use always string resources file, because if you need to change a single #String used in multiples xml files, you have to change only one time. Vice-versa, if you write your strings inside the xml layout files, if you need to change a string you need to search for the string, search for the xml file and then change as many occurrances you need.
In conclusion, it is not a good practice to hard code strings. You should specifies them to a string resource file and then reference them in your layout.This allows you to update every occurrence of a single word in all layouts at the same time by just editing your strings.xml file.
It is also necessary for supporting multiple languages definitions as a separate strings.xml One file for one language!
Is there a way to add localization setting in my app? I'm going to add a preference/setting in my app and there is a localization option. So the user can change the language I have provided from the values string.
I was googling around but found nothing. Wondering you guys can help me, and give example or link to the tutorial.
You don't need to provide a setting for that, Android will do that for you. You just have to provide the translations for the languages you want to support.
You can do this for French for example by creating a folder named res/values-fr in your resources folder and putting your translations in there.
so in the res/values folder you would have a strings.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Map</string>
</resources>
and in the res/values-fr you woud have another string.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Carte</string>
</resources>
Your app will use the translation that best matches the language that the user has selected on their device.
I'm writing an application were it requires to change the language of the application as and when the user requires. The data in different language is stored in the data base, from which the data is fetched and the UI is updated. I'm want to what should be done if device does not support the particular language font. Any help will be much appreciated.Thanks in advance. _/|_
I don't know more about this but ...
for example if you want your application to support both English
and French strings (in addition to the default strings),
you can simply create two additional
resource directories called /res/values-en (for the English strings.xml) and
/res/values-fr (for the French strings.xml).
Within the strings.xml files, the
resource names are the same.
For example, the /res/values-en/strings.xml file could
look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
Whereas, the /res/values-fr/strings.xml file would look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Bonjour en Français!</string>
</resources>
A default layout file in the /res/layout directory that displays the string refers to the
string by the variable name #string/hello, without regard to which language or directory
the string resource is in.
The Android operating system determines which version of
the string (French, English, or default) to load at runtime.A layout with a TextView control
to display the string might look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello" >
</LinearLayout>
The string is accessed programmatically in the normal way:
String str = getString(R.string.hello);
It’s as easy as that.
More you will found Here