Localization of code generated natural language strings - android

I'm writing an app in which I want to display a string, describing an object in my model, to the user. I've started thinking about localization and I'm a bit stumped as to the best approach to deal with the difference in grammar between languages.
Imagine if in my model I have a verb an object and a quantity (pseudocode):
int _quantity = 6;
String _object = "#string/object";
String _verb = "#string/verb";
This should render in English as _verb + _quantity + _object (i.e. "eat 6 eggs", whereas in German it should render as _quantity + _object + _verb (i.e. "6 eggs eat" (but, obviously, using German word tokens rather than English :) ))
Is there a standard approach to this type of problem in Android?
Thanks.

OK, I've come up with a solution which, based on what I know, is the best I can find. If anyone has a better solution, please post!
1) In strings.xml, define any bits of text needed (for me, these equated to the localised names for enumeration members and for the string used to build natural language strings (more on this in a bit))
Example:
<resources>
...
<string name="verb_ate">ate</string>
<string name="verb_threw">threw</string>
...
<string name="modelobject_naturallanguagedescription">I {0} {1} {2}</string>
...
</resources>
2) In any enumerations, decorate them with the resource ids of the strings they are to use for display
Example:
public enum VerbType
Ate(R.string.verb_ate),
Threw(R.string.verb_threw);
private final int _stringID;
private VerbType(int stringID) {
_stringID = stringID;
}
public int getStringID() {
return _stringID;
}
}
3) Make a helper function which will take the model object and the context and will piece together the required natural language string (using java.text.MessageFormat), plus a helper function for each enumeration:
Example (assume that ModelObject has _verb, _quantity, _object and relevant getters):
public static String getVerb(VerbType v, Context c) {
return c.getResources().getString(v.getStringID());
}
public static String getNaturalLanguageString(ModelObject o, Context c) {
MessageFormat mf = new MessageFormat(c.getResources().getString(R.string.modelobject_naturallanguagedescription);
return mf.format(new Object[] {getVerb(o.getVerb(),c), o.getQuantity(), o.getObject()});
}
So, having done all of this, it's easy enough to define another strings file for a different language (German for example (I don't know German, so the language won't be correct)):
<resources>
...
<string name="verb_ate">gegessen</string>
<string name="verb_threw">gethrown</string>
...
<string name="modelobject_naturallanguagedescription">Ich haben {1} {2} {0}</string>
...
</resources>
So, in an english locale getNaturalLanguageString might return:
I ate 6 eggs
whereas in a German locale it might return:
Ich haben 6 eggs gegessen

Related

Remove trailing zeros from currency string android

I tried to convert int to curency format and everything worked as I expected
example 1000 --> Rp1.000
but on certain devices the results are not what I expected
i.e. 1000 --> Rp1.000,00
So the question is how to remove the last 2 extra nolls if the device displays them.
my current code:
public static String toUang(String DigitUang) {
return NumberFormat.getCurrencyInstance(new Locale("in", "ID")).format(Double.parseDouble(DigitUang));
}
The best solution here would probably be to fix your locale settings and handle the presentation there. That being said, you also could probably handle this using a regex replacement:
public static String toUang(String DigitUang) {
return NumberFormat.getCurrencyInstance(new Locale("in", "ID"))
.format(Double.parseDouble(DigitUang))
.replaceAll("[,.]00$", "");
}

Android multilanguage support: string formatting issue due to different placeholder counts

I'm making a french Android app and I'm trying to support English.
I use "placeholders" to format my strings, so I can adapt them to male and female users. For example, this string in my strings.xml file:
<string name="encouraging_comment">
Les %1$s sont compliqué%2$ss...
</string>
will become "Les hommes sont compliqués" ("Men are complicated") or "Les femmes sont compliquées" ("Women are complicated").
And there lies my problem. The string translation, as follows...
<string name="encouraging_comment">
%1$s are complicated...
</string>
...needs only one placeholder, when the french equivalent needs two.
How can I manage this issue ?
Thanks in advance.
Just omit the second placeholder from the English template string and use an empty string for the second argument (or any other string, it doesn't matter, the value will be ignored) when you render the string:
XML:
<string name="encouraging_comment">
%1$s are complicated...
</string>
Java:
getString(R.string.encouraging_comment, "women", "");
getString(R.string.encouraging_comment, "men", "");
This works because it's not an error if there are more arguments than placeholders, only if there are fewer arguments than placeholders.
I'm assuming that you'll have some kind of table or mapping where you look up the placeholder values based on language and gender. In pseudo-code:
(French, Female) -> ("femmes", "e")
(French, Male) -> ("hommes", "" )
(English, Female) -> ("women", "" )
(English, Male) -> ("men", "" )
AFAIK, you can do this by adding check in your code like
String result;
if ( Locale.getDefault().getLanguage().equals("en")){ // examples "en", "fr", "sp"
result= getString(R.string.encouraging_comment, myString);
}
else{
result= getString(R.string.encouraging_comment, myString, myStringTwo);
}
textView.setText(result);
Hope this helps you :)
One way to do this is create a string locale in your strings.xml
for English Strings.xml
<string name="locale">En</string>
for French Strings.xml
<string name="locale">Fr</string>
Then while setting string
switch (getString(R.string.locale)) {
case "Fr":
//set your string for French
break;
case "En":
//set your string for English
break;
}

Getting Locale variant for simplified chinese

How can I get simplified chinese description (简体)? From the available locale Locale.SIMPLIFIED_CHINESE, no method seems to return this description:
getDisplayLanguage() returns the correct language name, but without the variant.
getDisplayName() returns the correct language name and country, but also without the variant.
getDisplayVariant() returns an empty string.
I've also tried to build a new Locale using the different constructors, also to no avail.
new Locale("zh", "CN");
new Locale("zh", "CN", "Hans");
I've checked the Android source code for LocalePicker and I've concluded that it is loaded from the resources (special_locale_codes and special_locale_names).
Any solutions besides having to hardcode/include this string in my resources?
Let me explain my process on how I tackled this. First, I found this block of code in LocalePicker.java
private static String getDisplayName(Locale l, String[] specialLocaleCodes, String[] specialLocaleNames) {
String code = l.toString();
for (int i = 0; i < specialLocaleCodes.length; i++) {
if (specialLocaleCodes[i].equals(code)) {
return specialLocaleNames[i];
}
}
return l.getDisplayName(l);
}
which takes in a Locale as you already know. Then it tries to find the locale code in the specialLocaleCodes string array. The specialLocaleNames you are seeking are obtained from arrays.xml as you've helpfully stated:
<string-array translatable="false" name="special_locale_codes">
<item>ar_EG</item>
<item>zh_CN</item>
<item>zh_TW</item>
</string-array>
and the corresponding languages
<string-array translatable="false" name="special_locale_names">
<item>العربية</item>
<item>中文 (简体)</item>
<item>中文 (繁體)</item>
</string-array>
Notice the code with the simplified Chinese is zh_CN and the last two characters are capitalized.
However,
Locale locale = new Locale("zh_CN");
System.out.println("Locale: " + locale);
prints
Locale: zh_cn
Notice the lower case. So there is no way specialLocaleCodes[i].equals(code) will return true. So then I poked around Locale.java and, long story short, we can bypass that case-changing jumble by doing this (and you MUST keep the 3rd parameter as an empty string for this to work):
Locale locale = new Locale("zh", "CN", "");
System.out.println("Locale: " + locale);
Prints
Locale: zh_CN
With this you should be able to do this:
Locale locale = new Locale("zh", "CN", "");
System.out.println("Name:" + locale.getDisplayName(locale));
Upon further inspection on Kitkat using this (thank you Andrew!)
int specialLocaleNamesId = Resources.getSystem().getIdentifier("special_locale_names", "array", "android");
String[] specialLocaleNames = Resources.getSystem().getStringArray(specialLocaleNamesId);
it was possible to print out
العربية,中文 (简体),中文 (繁體)
as expected. However, something in Kitkat is still preventing the correct string to display. Frustrating.
However, in Lollipop 5.0+ and Java 1.7 this works using forLanguageTag() in Locale.
Locale locale = Locale.forLanguageTag("zh-Hans");
System.out.println("getDisplayName:" + locale.getDisplayName(locale));
System.out.println("getDisplayLanguage:" + locale.getDisplayLanguage(locale));
which prints
getDisplayName:中文 (简体中文)
getDisplayLanguage:中文
You could probably access the android internal resource: com.android.internal.R.array.special_locale_names the same way it's done in LocalePicker:
final Resources resources = context.getResources();
final String[] specialLocaleNames = resources.getStringArray(com.android.internal.R.array.special_locale_names);
But it's probably safer to use your own resource here (avoiding
the use of internals)

How to make Russian quantity strings work properly?

I've faced a problem with quantity strings (plurals).
The manual says, I may provide quantity strings that are specific for each localization, and there are several common cases: "zero", "one", "two", "few", "many", and "other". I don't know if all possible cases for all languages in the world were covered; anyway, it is more than enough for Russian that I'm trying to make a localization for.
In Russian, numbers from 2 to 4 should be treated like "few" (the rule is actually more complex but I only need numbers below ten).
However, when I request a quantity string for 2, the system takes the "other" string. It doesn't take neither "two" string nor "few" one (I have provided them in my resources). If I removed the "other" string, I get the exception:
android.content.res.Resources$NotFoundException:
Plural resource ID #0x7f080000 quantity=2 item=other
I tried this both on emulator (Android 2.1) and on a real device (Android 2.3), the behaviour is the same wrong in both cases. Obviously, there is a mistake somewhere—the system does not recognize locale-specific quantities for my language. Could it be that Android has some bug here?
I believe this is currently broken in Android.
http://code.google.com/p/android/issues/detail?id=8287
Specifically, the following code in PluralRules.java shows that most languages only use the one or other strings, but Czech will use the few string:
static final PluralRules ruleForLocale(Locale locale) {
String lang = locale.getLanguage();
if ("cs".equals(lang)) {
if (cs == null) cs = new cs();
return cs;
}
else {
if (en == null) en = new en();
return en;
}
}
private static PluralRules cs;
private static class cs extends PluralRules {
int quantityForNumber(int n) {
if (n == 1) {
return QUANTITY_ONE;
}
else if (n >= 2 && n <= 4) {
return QUANTITY_FEW;
}
else {
return QUANTITY_OTHER;
}
}
}
private static PluralRules en;
private static class en extends PluralRules {
int quantityForNumber(int n) {
if (n == 1) {
return QUANTITY_ONE;
}
else {
return QUANTITY_OTHER;
}
}
}
Thanks to Brigham who has pointed to the issue where the problem is explained; that explanation first rised more questions but now they seem to be solved. Indeed, the quantity strings don't work properly (at least prior to API 11, which is Android 3.x), so you have to use an alternative.
The solution for APIs prior to version 11 is mentioned in the comment 15 that contains a link to the project for alternative handling of quantity strings. That project is a program that simply does what the system was supposed to do. It can be converted to a library easily, so you just add it to your project, import the class and go.
Define your plurals strings in values-ru/strings.xml not in values/strings.xml
set your mobile device language as Russian (from setting->Language&input->Language->select Russian)
If your language is not Russian it will take plurals strings from values/strings.xml

android: referring to ressources in custom xml

i have a multilingual android app, where i have put the different translations in the strings.xml in the respective directory.
now i also have a custom xml file, where i would like to reference texts like this:
<?xml version="1.0" encoding="UTF-8"?>
<rooms>
<room title="#+string/localizedtext" />
</rooms>
now when i read the title attribute in my code, i obviously get the unresolved string "#+string/localizedtext" like it is.
is it possible to somehow resolve this link to the localized text automatically?
thanks!
Almost a year later:
public static String getStringResource(Context context, String thingie) {
try {
String[] split = thingie.split("/");
String pack = split[0].replace("#", "");
String name = split[1];
int id = context.getResources().getIdentifier(name, pack, context.getPackageName());
return context.getResources().getString(id);
} catch (Exception e) {
return thingie;
}
}
That'll do it.
This might seem like a broad answer but I believe it'll clarify a lot of things for people who spent hours looking for it (I'm one of them).
The short answer is yes, you can use references in custom XML, not just for strings, but that's the example I use, for ease of understanding.
Considering the context:
res/values/strings.xml
(Default strings, usually en-US for convenience but that's up to the developer)
<resources>
<string name="sample_string">This is a sample string.</string>
</resources>
res/values-fr/strings.xml
(Localized french strings)
<resources>
<string name="sample_string">Ceci est un exemple de chaîne</string>
</resources>
res/xml/test.xml
(Custom XML file)
<!-- #string/sample_string identifies both
the default and french localized strings,
the system settings determine which is used at runtime.
-->
<test>
<sample name="sampleName" text="#string/sample_string"/>
</test>
src/com/example/app/TestXmlParser.java
//Omitted imports for clarity.
public class testXmlParser {
public static final String ns = null;
public int parse(XmlResourceParser parser) throws XmlPullParserException,
IOException{
while(parser.next() != XmlPullParser.END_DOCUMENT){
if(parser.getEventType() == XmlPullParser.START_TAG){
if(parser.getName().equalsIgnoreCase("sample")){
// This is what matters, we're getting a
// resource identifier and returning it.
return parser.getAttributeResourceValue(ns, "text", -1);
}
}
}
return -1;
}
Use String getText(int id) to obtain the string corresponding to id (localized, if available).
Using the example above it would amount to replace :
//Return the resource id
return parser.getAttributeResourceValue(ns, "text", -1);
with :
//Return the localized string corresponding to the id.
int id = parser.getAttributeResourceValue(ns, "text", -1);
return getString(id);
The way you tried is not possible.
You might get similar functionality with <string-array> resource:
<resources>
<string-array name="room">
<item>#string/localizedText</item>
<item>#string/otherLocalizedText</item>
</string-array>
</resources>
then you would use it like this :
String[] room = getResources().getStringArray(R.array.room);
String localizedText = room[0];
String otherLocalizedText = room[1];
Localization in Android is done with resource identifiers. Check out this Android tutorial.
http://developer.android.com/resources/tutorials/localization/index.html
See discussion below.
Great answer kyis, shame I still don't have enough brownie points to rate it. To answer Nick's question, just change the last bit of code to:
int id = parser.getAttributeResourceValue(ns, "text", 0);
return (id != 0) ? getString(id) : parser.getAttributeValue(ns, "text");
Note that I used 0 for the default value of the resource as this is guaranteed never to be a real resource value. -1 would have done also.

Categories

Resources