What is the purpose of using translatable in Android strings? - android

What does the translatable attribute, like translatable="false", mean?

This attribute points out that this attribute will be the same for all locales. Why is it useful?
The localization files are more readable for human and saves time.
It tells the Lint tool that everything is fine and that Android does not need to look for this resource translation.
Full explanation:
http://tools.android.com/recent/non-translatablestrings
So, generally, this means if don't put this attribute, you should always localize this resource, otherwise tell people and compiler that this is unique for all locales by specifying this attribute.

If you are supporting multiple languages, and there are some strings which should not be translated (that means same across all languages) then you can use translatable="false"
For ex: Numbers<string name="account_setup_imap" translatable="false">IMAP</string>
Detailed Description

I've never heard of that attribute, but apparently (now) there's a reason for using it: see this post from ADT-Dev google group.
BTW, I just tried it and it works in fixing those Android Lint errors.

I don't see any reference to that in official docs, however, it probably is just an indicator for humans looking at the file that that particular string entity should not be translated.
If you have an Android app that you wish to internationalize, a common tactic is to just send your strings.xml file to translators. So that attribute would just be a flag for them.

Related

What does "resource varying by configuration" error mean?

In Android Studio 2.2.2 I have an error in AndroidManifest.xml file saying
Resources referenced from the manifest cannot vary by configuration
There is a StackOverflow question by someone else on this message but the answer only describes how to ignore it. What I want to know is what does it mean?
The line associated with the error says
android:versionName="#string/appvername"
what does the error mean and how do I prevent (not just ignore) it? What is a "configuration" in this context?
What is a "configuration" in this context?
Quoting the documentation:
You should always externalize resources such as images and strings from your application code, so that you can maintain them independently. Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations. In order to provide compatibility with different configurations, you must organize resources in your project's res/ directory, using various sub-directories that group resources by type and configuration.
So, a configuration is a mix of device capabilities and states that controls what resources get loaded. For example, the device locale settings determine which strings get used from your available string resources.
What I want to know is what does it mean?
Not every attribute in the manifest can be populated by a resource, because the system cannot handle varying values based on configuration.
For example, you cannot change the Java class name of an <activity> by using a string resource in android:name, with an eye towards using different Java classes with different screen sizes. While that's an interesting concept, Android is not set up to support that.
how do I prevent (not just ignore) it?
In this case, I think you are encountering an IDE bug. android:versionName should support string resources, as that is a user-facing value, and therefore you might want to translate the string. So, add tools:ignore="ManifestResource" to the <manifest> element, until the bug gets fixed.

Using strings from #android:string

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!

Change set of strings based on app purpose

I'm building an Android app that can be used for two different purposes, say biking and running. I would like to load the sport-specific keywords given a user's preference. They could be defined in different files, like strings_biking.xml and strings_running.xml .
How can I implement this as efficient/elegant as possible? I've taken a look at locales, styles, themes and lots of other topics in the Android documentation, but nothing seems to fit well enough.
AFAIK it does not support such distinguish as per category. strings.xml are used for localization only. For your purpose, you can define some prefix/suffix to the string name. While using them in activity, create string runtime by applying that prefix/suffix and string name for biking or running or any other condition.
May be this link can help for dynamic creation of string.

ANDROID: What is the main idea behind of using strings.xml?

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....

Better documentation for android.R.*?

I'm obviously pretty new to Android. Does better documentation exist for the set of android.R.* classes somewhere? The API reference is pretty bare bones, many times only containing the resource literal IDs and not explaining how/where they are used. Similarly, it would be nice if additional information (or pictorial examples) existed for the various platform defined layouts and styles.
Or am approaching this the wrong way? Perhaps there's a better method to determine their usage than going straight to the documentation. I'm finding this portion (I'm not even sure what to call it) to be the most confusing aspect of Android programming by far and am open to any suggestions that will better my understanding.
Thanks!
Source code would be the best documentation. At least, it's very helpful when you need to understand how the function does what is briefly described in documentation.
I think you are confused about what R is.. The R file is autogenerated during the compilation process based on the resources you have in your res folders.
So, if you add a strings file: res/values/strings.xml with contents:
<resources>
<string name="string_name">text_string</string>
</resources>
R will now contain an entry for R.string.string_name.
For a details on all of the different types of resources, see resource types.
Edit:
As to "platform defined layouts", have you looked at the developer docs on it? See common layout objects. Also each layout type has a tutorial on it, search for "Hello --layout type--"

Categories

Resources