Store language independent strings in Android resources - android

How would I store some language independent values like website address in an Android project. I am currently storing it inside my strings.xml. This does not look good as if I add another language and translations, this entry will still have the same value and will be duplicated.
I would also like to get some tips on creating Android projects with I18N in mind.

You don't need to create the same keys in the translated xml file again. If Android does not find link in the German/Spanish/Whatever language file, it will fall back to the original key stored in the default string.xml.
I suggest to implement the English version first and after that let someone translate it.

for i18n-format, you can create some resources folder for one local :
/res/values <- default locale
/res/values-fr <- values for french locale
...
For more examples, read documentation : http://developer.android.com/guide/topics/resources/localization.html

For language independent Strings just create a class with constants :
public class MyIndependentStrings {
public static final String SOME_STRING = "Value";
}
Usage :
tvMyTextView.setText(MyIndependentStrings.SOME_STRING);
In this case you will use constants and not i18n Strings from ../values/strings.xml

Try putting language independent strings in:
\res\values-nodpi\strings.xml

Related

Difference between writing text/string in java file and in string.xml file in values folder of android

I have created a class called Params.java in my project where i am writing all string parameters and accessing them as static wherever they are required.
Params.java
public static final String USERNAME = "username";
public static final String USER_TOTAL_FOLLOWERS = "user_total_followers";
public static final String USER_EARNINGS = "user_earnings";
public static final String USER_PROFILE_PICTURE_URL = "profilepic";
I also have strings.xml
<string name="btn_txt_cancel">Cancel</string>
<string name="btn_txt_okay">Okay</string>
<string name="btn_txt_not_now">Not Now</string>
<string name="btn_txt_update">Update Now!</string>
To access from Params, i simple need to write Params.variable_name and form strings.xml, i need to write getResources().getString(R.string.variable_name)
I want to know
What is the best practice ?
What difference it makes in performance when accessed from these to files ?
When you add a string in strings.xml, that means in current/future you can support multiple languages with the same key used in all language strings.xml file.
For normal constants like a key for using in bundle, better to create a class which holds all constants.
Finally the strings which are displayed in UI, the best practice is to add those in strings.xml and for code related usage constants use a class like Constants.class which holds all your constants.
Ok, So the best practice in android to store string is, Those strings are visible to user put them on string.xml file, for example hint of EditText "enter your name" and those string which are not going to visible to the user like your "Google API key" put them on Constant.java file.
This is the best practice while coding in Android.
The biggest advantage of using string.xml instead of a custom file, are translations.
Android Studio provides everything you need for translating your application simply adding a new resource localized for the locale you want.
This allows you to automatically change the lenguage without coding anything except the translated text.
(You can also buy translations from the Google Developer Console)
In your code you would have to add a switch and manually manage all cases.
More information can be found here
About performances
It's pretty much the same. they both are static resources with different access ways. If there is any performance impact it would be really hard to notice it, we are talking about milliseconds
you can use getString(R.string.stringName); from xml
for me i use string.xml for String Which are Visible to User. it has an Advantage when i m creating multiLanguage App.
for String that are not visible to users I prefer Params.java Like URL etc.
To support multiple languages and to separate concerns between view and logic, use strings.xml
If it is a string that used only in code and not visible to user, use a constants class.
This way you have a more cleaner code.

Where to store string values?In strings.xml or in constants class?

In android, we can store string values either in strings.xml file or in some constants class as static final variable.Is there some reason for selecting one over another in some circumstances?
In a nutshell:
value for use in code: use always constants class.Advantage: codes remain integrated and your package can be utilized in other projects/contexts. You can not do that with string.xml as it is not transported with your package.
value for display in UI: use string.xml. Advantage: you can use localization to display translated texts.
Some situation may arise when both option appears viable. You will have to then decide where are its related values are stored.
As a general rule, use the strings.xml because android uses that XML to enable translating your app into different languages, which it can't do with strings that are hardcoded.
The official android guide on localization say the following;
Move all strings into strings.xml
As you build your apps, remember not to hard code any string. Instead
declare all of your strings as resources in a default strings.xml file
which makes it easy to update and localize. Strings in strings.xml
file can be extracted, translated and integrated back into your app
(with appropriate qualifiers) without any changes to compiled code.
If you generate images with text, put those strings in strings.xml as
well, and regenerate the images after translation.
Strings that are not going to be displayed to the user in any way needn't be stored in the XML, because they will never need translating, and you probably don't want the android system tampering with them in ways you might not know about during runtime.
If the string value is used to display in UI store in Strings.xml Otherwise keep it in code. There can be JSONTags, Key for different api/Thirdparty libraries.These kind of things should be kept in code itself.
strings.xml it is used for localization and needs a context to retrieve the content of a String. If you need a java constant to be accessed in different classes, you a public static final String member. If the string is a message for the user you should use strings.xml
If strings represent text readable by user, and which could potentially be translated to other languages (names of buttons, labels, notification/error messages, etc.) then they should be in strings.xml (actually, it can be any file name you like, not just "strings").
If string is some constant which is used in the app internally (bundle/intent keys, fragments tags, etc.) they should be declared in class
It depends, if it is a text string that will be translated or displayed to the user then for 118n sake, you will want to put in into strings.xml.
However, if the string is something like a server url or api code then you'll want to store those in code as a public static final String

How can I have both res/values-pt_PT and res/values-pt_BR on Android?

I keep getting an error when creating folders for internationalization. But the errors appear just for folders with the name like values-xx_XX. If I have values-xx everything is ok, but like I asked in the title I want to make 2 separate folders for the (aprox.)the same language: values-pt_PT,values-pt_BR. How can I do that without getting any errors? Note: The error is not specified anywhere, the eclipse is just marking the folder with a red cross and doesn't allow me to run the project.
Every hint is appreciated. Thank you! :)
Use the format values-xx-rXX instead of values-xx_XX. In this instance you should use values-pt-rPT and values-pt-rBR.
See http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
The language is defined by a two-letter ISO 639-1 language code,
optionally followed by a two letter ISO 3166-1-alpha-2 region code
(preceded by lowercase "r").
I haven't done it myself, but after a quick search here, I found Setting region based Local in android where they suggest using the constructor as you did:
Locale locale = new Locale("ar","SA"); //(language,country)
But also, if you wish to pass it as one parameter, you'd still leverage the lowercase "r" as in the "values" folder, like this:
Locale locale = new Locale("ar-rSA");
Hope it helps :)

Which strings to store in strings.xml?

Are there any suggestions on which strings you should store in strings.xml and which strings can be stored as String objects? For example, do I have to put a string into strings.xml, if I use it only to complete a certain action and then it can be destroyed? And what is the main reason in storing strings in xml? Thanks in advance for your answers.
Any string that will be displayed to the user should be in strings.xml. This is useful in case you ever want to support other languages for your application. If you do, you just create a new strings.xml file that language with translated values. You can learn more about it here.
One reason is multi-language support.
You should store the strings that you use in Activities - TextView, button's caption and so on.
You should put most constants in strings.xml, your app title, button names, textview contents...mostly things that wont change in your application.
Another reason for storing strings in xml is for localization. You can store different files for each different Locale or language, and Android will grab the correct file for the phone's selected Locale or language.
Here is a link to the String resource Android page, it will go more deeply into how the language support is done.
You don't store all the strings in strings.xml, but only strings constants related to user interface, the strings that you want to translate in different languages.
You can have different folder like :
values
values-fr
values-de
in each a strings.xml file with you UI messages translated in many languages.
Regards,
Stéphane

Is there a way to retrieve ALL localised versions of an Android application's label?

If an application label is localised, I have a requirement to retrieve all localisation variants. Effectively I need to know if the android:label points to a string reference and, if so, all the locale values present in the string resources. Any ideas?
I don't have a full answer but here's what I came up with.
Localization - Here is the official documentation for android localization. You can ensure that your android:label points to a string reference if you have it point to a key in the /res/values/strings.xml. Specific localization strings can be overridden by using qualifiers on the directory, for instance for French versions you can put them in /res/values-fr/strings.xml. Any values not found in the French version will default to the previous unqualified file.
I'm not sure about getting all the locale strings for a specific resource but there is getAssets().getLocales() which will return a string array of the locales the asset manager has resources for (I'd assume for any resource, not just a particular resource)
Lastly, if you don't need to do this at run time you could try to write a script/program to examine all the localization directories under /res and compile a list of all resources with their localized values.

Categories

Resources