Can localization resources be downloaded in runtime? - android

I have application with internet access and don't want to store many string.xml files for different languages.
What I want:
Application contains only one string.xml with english strings.
When user launches it - I see user's phone locale, and ask my own server for necessary language file. (resource keys will be the same)
When new resource file comes, all interface should work with new file.
The question is how to change existing or add new string.xml file in runtime?

You obviously cannot change, download or remove strings.xml at runtime
If you want to store locations, you will have to use SQLite storage to store translations.
similar:
How to modify strings.xml file at runtime
run time modification of strings.xml

Now I can see only solution:
Create some "localization proxy" that will return me necessary resource (from string.xml or downloaded resource)
Replace all getString() and getText() to your own method getStringFromLocalization
Override TextView, Button and some other views with custom one and change there init and setText methods.

Overriding the standered resource/language using resource files which are complied time then your scarifying performance over customization. Do it only if u need this.

Related

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

Android Strings

I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.

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

Change string.xml at runtime

I have a special request to implement dynamic language packs and themes in Android. This basically means:
to download a zip file containing a file named strings.xml (containing the translation)
replace in the application the file /res/values/strings.xml with the one downloaded
Is this possible? Thank you for your help.
No. Things inside the Res folder are static and you can't recreate the R class in runtime.
Why don't you try using sqlite for your situation?

How to get string resources from a certain file?

I have two resources file in res/values directory: string.xml and names.xml
how can I retreive all resources from names.xml only
the method
Field[] x=R.string.class.getFields();
retrieves resources from both files.
how can this be acheived
thanks
I am sorry, but that is not possible. It does not matter that you have your strings split between multiple named resource files -- Android combines them all when it compiles your project.
You are welcome to use prefixes or something to identify one set of strings from another. I do that with the support code for the Android Parcel Project, to allow reusable components to each define strings without one overwriting the strings of another.

Categories

Resources