What's the point of defining strings in xml at the res/values/string.xml directory? Is it more efficient than just defining the strings as constants within your classes? (e.g. Database table creation scripts, etc..)
Is it a matter of organization or is there some benefit in how Android handles these objects in memory?
So you can easily translate them into different languages.
So they're nicely organized and you have them all in one place.
You can easily translate your app if you use strings.xml. Just create a new folder with suffix like values-cs and put the xml with czech strings in it and the whole app will be translated to czech if you have set czech localization in your phone.
applications access them/compute them faster than normal strings
localization
The hello string is defined in the res/values/strings.xml file. This is the recommended practice for inserting strings to your application, because it makes the localization of your application to other languages graceful, without need to hard-code changes to the layout file.
language (computer) translation
For me the first point is the deal breaker, anything to make your app faster. (this is assumed from the countless hours of creating my own applications and being told using string.xml is best from an optimization point of view, plus, especially in a long listview, it does seem to load faster for me (droid A855) )
Related
is there any way to use different string files? I know that I can create files for another languages, but I want to load different files for English, depending on some initial value. It will be good if I could have one more file with default value. It is possible?
Android string resources are optimized for supporting different locales and you can force app to use particular locale. If you want use custom dictionaries for one language I would recommend to use custom class that will read string-array resources. Imho nice example is: Dirty Phrasebook
Few articles about this app,
Source code
I am a bit noob in Android and recently I found out that I can use the predefined string that Android provides as #android:string/cancel or #android:string/ok. At first I thought it was a good idea to use them because is a way to reuse code, but now I am not so sure about that.
What if somebody with a device configured with a language that I don't support install my app?
I assume that the app will use a default language, probably english, but those string from #android:string will get translated to the user's language, so he will end up with a mix of languages.
It this true? What do you think about use #android:string?
Thanks!
EDIT: Apparently my question hasn't been understood properly. I am NOT asking about how to support different languages. My question is about the convenience of use strings defined on #string:android, if it is correct to use them or can be lead to undesirable situation like a mix up of languages in the same application.
To ensure that your strings are appearing properly on devices configured with different languages, you'll want to create different values directories for different languages. For example, your default string values would be kept under values/strings.xml and French string values would be kept under values-fr/strings.xml.
The Android Developer website gives you plenty of information for supporting different languages in your application. http://developer.android.com/training/basics/supporting-devices/languages.html
The android: values (strings, icons, themes, etc.) will differ between devices and Android versions. If you want to use them, it's safest to copy them into your project. So for strings, you wouldn't have to worry about partial translation.
In the ressource folder of your app (res), ther is a folder "values" in it, and in this folder is the string ressource xml (strings.xml).
Usually, your app selects the strings from this file.
But you can add other value folders like this: Just create a new folder and name it "values-countryCode", for example "values-ch" for Switzerland ;)
Your app automaticly chooses the right string ressource, depending on your device's langague settings. If the langague of your device isn't available, it just takes the sting ressource of the default "values" folder.
A list if the country-codes is here.
Further information can be found here.
Hope I helped, and this is what you're looking for!
In the app I'm building, I'm using multiple languages. It's easy to add a different language into an app by adding a new folder (for example: values-fr) and adding a new strings.xml file in there. But I have pretty large text files (complete articles) that I need to add. These articles are also written in different languages. What is the best way to add them to my app?
I'd consider using res/raw-<qualifiers> as alternative to the assets. The raw folder can store arbitrary files in their - you guessed it - raw form. For example, a 'Hello World' article written in French and English, would be stored under:
res/raw-fr
res/raw-en
The raw resource can then be opened by calling openRawResource(int id) on a resources object, similar to how it works for other resources like drawables, strings etc. The id's generated by the framework will be in the familiar format of R.raw.filename (without file extension).
The benefit of using this approach is that you can fully leverage Android's localization system, meaning that as a developer you basically don't have to worry about any of that at all. You can also easily add more qualifiers to further filter on device characteristics (e.g. screen size, density, platform version etc etc - see here for a full overview). The downside is that it imposes some limitations in terms of the original file name/extension and doesn't support a proper folder/file hierarchy.
The 'better' approach (/raw vs /assets that is) will probably depend on your project's requirements.
I would probably use assets -- that is, create assets/data/fr/ and store the fr files there. Note that assets require explicit extraction -- which probably is good since you may save memory having only one set of articles installed.
Another possibility is to place everything on an http server, and thus make both keeping and accessing the articles somebody else's problem :) .
BTW, if you files are really big, you will have to install the application without them, and download the articles later. (There are restrictions on the apk size.)
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....
I have an app with support for several different languages.
Now lets say I want to add a new feature, which will usually will require new string resources to be added.
What is the (Or is there) way to add new string resources that will be added automatically to all the localization strings files? (Means i'll just need to translate the content of the string and not to create new one with the same key for each localization file)
Because it dosen't make any sense to copy-paste it manually when you have a lot of localization files...
If you follow Android localization rules, there isin't any other way other than manually adding to each language strings.xml file. You can script this but then you will have to write the script. So, my suggestion is to bite the bullet and do it.
As you said if there are a lot of files, then scripting it is best way, even then you may have to manually add the localized string values to each file.
What you refer to is common Localization problem. It is not just strings.xml and Android platform related.
As you might be guessing, people actually found ways to resolve it. Typically, you will send just the English file to translators (translation vendors) and they will update it using Translation Memory software.
Or if you are independent Software Developer, you may want to use some crowd-sourcing platform for your translations, like Launchpad, BabelZilla or Crowdin. These platforms also act as Translation Memory, so you won't have to manually synchronize individual language files.