I have an android app that I want to internationalise.
I have extracted the app strings and deployed them in resource files and all that works fine.
The remaining issue I have is that my app reads a folder structure and actually pulls filenames in as words to use in the app.
I have these filenames/words defined in my xml, but I can't figure out how to dynamically lookup the english language word.
So. here's the scenario.
Filename = hello.png. I want the word "hello" to appear in my app corresponding to the image; I have the word "hello" defined in my strings.xml and the corresponding language files as "hello_file" (i.e. the word "hello" can be accessed by R.string.hello_file). What I think I need to do is take the english word from the filename and do a reverse lookup on the strings.xml file and find the node corresponding to that and then lookup the corresponding word in the strings_xx.xml file for the iso language translations.
But I don't know how to do that...
Perhaps I'm over complicating this? It does not seem an ideal use case for the strings_xx.xml translation facility.
Any other ideas?
Use string array http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
String[] files = getResources().getStringArray(R.array.file_names);
In code you can loop through all values to find which one you need.
Related
I create folder values-ru (for any region). But with using Ukrainian language or Belarus i have an error.
android.content.res.Resources$NotFoundException: String resource ID #0x7f0d0037
Tell me, why i got an error. Does i must create values-ru-uk and etc. for all region?
You should define all your strings in the default (source) language, regardless of what translations you're adding.
These goes in: values/strings.xml.
If this is missing (or the string is missing in that file) then I expect that's why you get an error.
You can then add as many language files as you need: e.g:
values-uk/strings.xml. (Ukrainian)
values-be/strings.xml. (Belarusian)
or specific regions thereof: e.g:
values-ru-rUA/strings.xml. (Russian in Ukraine)
Disclaimer: I'm not an Android developer, but I've written localisation software that generates strings files.
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
this is my first question :)
I'm developing an application that stores animal species in a database. The app must be multilanguage, so I tought to take advantage of using strings.xml resource files.
The idea is to store the english name of the species on the db, for example "cat", "dog" etc.. and then display to the user the actual translation, based on an xml like this (for italian):
<string name="dog">Cane</string>
<string name="cat">Gatto</string>
The problem is that R.string contains the name dog and cat, but they are actually int, so I'm searching a way to use the "dog" string to be used to compare the R.string.dog translated value.
I'm almost sure that my design is terribly wrong, but don't know what the correct way to doing this kind of work, since the app is now in a very early stage of development.
Thank you
EDIT with example
This example illustrates the problem:
Database data:
row1: id="1", value="dog"
row2: id="2", value="cat"
String file strings.xml:
<string name="dog">Dog</string>
<string name="cat">Cat</string>
String file strings-it.xml:
<string name="dog">Cane</string>
<string name="cat">Gatto</string>
My problem is: the user want to insert a specie in his native language (eg. "Cane"), and I want to search in the DB for its existence before inserting.
I should loop for every row on the DB (where values are stored in english), get the the translation of each row (eg: I found cat, then I translate to "Gatto") and compare with the user input.
Is it possible to do that?
If you have a string name you want to use, you can use getIdentifier() to get the string id. As an example, to find R.string.cat:
Resources res = getResources();
int stringId = res.getIdentifier("cat", "string", packageName);
In the above example, if there is no R.string.cat found, it will simply return 0. It's an easy test to see if a string exists.
Alternatively, you can get an array of all the string ids in your R.java by using something like:
Field[] fields = R.string.class.getFields();
int[] ids = new int[fields.length];
for(int i=0;i<field.length;i++)
ids[i] = field[i].getInt(null);
Of course, that will also look for any strings that you don't really intend as translations, such as dialog/window titles, label/button captions, etc. I wouldn't advise it in the general case. If I had to do it, I'd prefix the "translation" strings with something so I could easily tell what is what, something like "entry_cat".
Note that we're using reflection, and if you have a lot of strings, it could slow you down. If you are going to loop through R.java, I'd advise only doing it on start-up, and saving the values in some sort of array/list.
First read this.
http://developer.android.com/training/basics/supporting-devices/languages.html
You can create value folder with many language's i.e janapee,dutch etc
you can find out value folder inside the res folder in your project. and create new value folders.
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
JUST TRANSLATE YOUR WORDS BY GOOGLE TRANSLATOR IN ANY LANGUAGE AND PUT INSIDE THE STRING.XML FILE .
Well, first of all, start reading this here:
Suppose that your application's default language is English. Suppose
also that you want to localize all the text in your application to
French, and most of the text in your application (everything except
the application's title) to Japanese. In this case, you could create
three alternative strings.xml files, each stored in a locale-specific
resource directory:
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 except title. If your Java code refers to
R.string.title, here is what will happen at runtime:
If the device is set to any language other than French, Android will
load title from the res/values/strings.xml file. If the device is set
to French, Android will load title from the res/values-fr/strings.xml
file. Notice that if the device is set to Japanese, Android will look
for title in the res/values-ja/strings.xml file. But because no such
string is included in that file, Android will fall back to the
default, and will load title in English from the
res/values/strings.xml file.
Could somebody tell me what is better in terms of performance?
Is it better to save 2 strings at string.xml, like 'abc' and 'abc:'
Or should I save only the first one and concatenate ':' when needed at Java coding ???
Very difficult to answer depending on what your strings will represent and what you need to append. Localization is also an issue, for example...
Dog // English
Chien // French
Hund // German
Using string resources allows you to create different resource files depending on the locale of the device and Android will automatically use the right localized string resource file. If all you need to do is append a single character such as : then you'll double every string for every language.
If you choose to only save the basic strings and append the character using code, then the code will be universal and you'll simply need to append the character to whatever localized word - potentially a lot more efficient.
Both from storage perspective and performance you should save only "abc";
getting extra data from disk takes far longer as some quick in-memory actions.
storing the same data twice is bad practice in general
If you have to concatenate multiple strings you should use StringBuilder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html
It's much faster then using '+' or '.concat()'
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