Friday 4 August 2017

Localization implementation in android apps

The Language translation for the android apps to support the Localization.

In android apps, if you want to support multiple languages then you have to provide the strings.xml file for every language . Now this file is to be there in the values folder for the languages .
The pattern to create a language folder is

values-<language code>-<country code>

→ language code is a 2 alphabets code that signifies the language like es for Spanish
→ country code is again a 2 alphabet code that signifies the country or the region in the country like IN for India, US for america.
→ region can also be defined for a language being used in more than one regions in a countries with a 'r' prefix to the country code.

Examples of values folder -

values-en-US → this is for english language in USA
values-it_IT → for italian in Italy
values-it-CH → for italian in Switzerland.

Now to generate such files is a cumbersome task if you tend to do it all on your own.

So here is a method to generate the strings.xml file for every language . What we need to do prior to it is make a list of all the strings used in the app like this


Play
Share
Details
Hide
OK
File name :
TimeStamp :
Location :

Now in this file you may keep changing the translations in all languages like chinese simplified

分享
细节
隐藏
文件名 :
时间戳
位置 :


So finally we make values folder in our app res like this for chinese simplified → values-zh_CN with the strings.xml file from the common values folder so that its having the tags in the proper format .
Like :
<string name="play">Play</string>
<string name="share">Share</string>
<string name="details">Details</string>
<string name="Hide">Hide</string>
<string name="generic_ok">OK</string>
<string name="fileName">File name :</string>
<string name="timestamp">TimeStamp :</string>
<string name="location">Location :</string>


Now here is the Java program that we may use to generate the localized folder for chinese language with the <string> tags in it . What we are doing hers is just replacing the value in the string tag in the above sequence itself

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Mainclass {

public static void main(String argv[]) {

BufferedReader br = null;
FileReader fr = null;

try {
File readFromFile = new File("D:/Localization/strings.xml");

fr = new FileReader(readFromFile);
br = new BufferedReader(fr);

String sCurrentLine;

File fXmlFile = new File("D:/app/src/main/res/values-zh-rCN/strings.xml");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();

NodeList nList = doc.getElementsByTagName("string");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

if(temp!=1){
Node nNode = nList.item(temp);
sCurrentLine = br.readLine();
nNode.setTextContent(sCurrentLine);
}

}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fXmlFile.getAbsolutePath()));
transformer.transform(source, result);

System.out.println("Done");
}
catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {

if (br != null)
br.close();

if (fr != null)
fr.close();

} catch (IOException ex) {
ex.printStackTrace();
}

}


}
}


Now this reads the chinese language strings from the strings.xml file and keeps replacing the content in the values-zh_CN/strings.xml file to make the chinese localized strings.xml file.

Few locale codes that I have in my glossary are :


Language codes can be found here : http://www.science.co.il/language/Locale-codes.php

Language / Locale Supported since version

English, US (en_US) 1.1
German, Germany (de_DE) 1.1
Chinese, PRC (zh_CN) 1.5
Chinese, Taiwan (zh_TW) 1.5
Czech, Czech Republic (cs_CZ) 1.5
Dutch, Belgium (nl_BE) 1.5
Dutch, Netherlands (nl_NL) 1.5
English, Australia (en_AU) 1.5
English, Britain (en_GB) 1.5
English, Canada (en_CA) 1.5
English, New Zealand (en_NZ) 1.5
English, Singapore(en_SG) 1.5
French, Belgium (fr_BE) 1.5
French, Canada (fr_CA) 1.5
French, France (fr_FR) 1.5
French, Switzerland (fr_CH) 1.5
German, Austria (de_AT) 1.5
German, Liechtenstein (de_LI) 1.5
German, Switzerland (de_CH) 1.5
Italian, Italy (it_IT) 1.5
Italian, Switzerland (it_CH) 1.5
Japanese (ja_JP) 1.5
Korean (ko_KR) 1.5
Polish (pl_PL) 1.5
Russian (ru_RU) 1.5
Spanish (es_ES) 1.5
Arabic, Egypt (ar_EG) 2.3
Arabic, Israel (ar_IL) 2.3
Bulgarian, Bulgaria (bg_BG) 2.3
Catalan, Spain (ca_ES) 2.3
Croatian, Croatia (hr_HR) 2.3
Danish, Denmark(da_DK) 2.3
English, India (en_IN) 2.3
English, Ireland (en_IE) 2.3
English, Zimbabwe (en_ZA) 2.3
Finnish, Finland (fi_FI) 2.3
Greek, Greece (el_GR) 2.3
Hebrew, Israel (iw_IL)* 2.3
Hindi, India (hi_IN) 2.3
Hungarian, Hungary (hu_HU) 2.3
Indonesian, Indonesia (in_ID)* 2.3
Latvian, Latvia (lv_LV) 2.3
Lithuanian, Lithuania (lt_LT) 2.3
Norwegian-Bokmål, Norway(nb_NO) 2.3
Portuguese, Brazil (pt_BR) 2.3
Portuguese, Portugal (pt_PT) 2.3
Romanian, Romania (ro_RO) 2.3
Serbian (sr_RS) 2.3
Slovak, Slovakia (sk_SK) 2.3
Slovenian, Slovenia (sl_SI) 2.3
Spanish, US (es_US) 2.3
Swedish, Sweden (sv_SE) 2.3
Tagalog, Philippines (tl_PH) 2.3
Thai, Thailand (th_TH) 2.3
Turkish, Turkey (tr_TR) 2.3
Ukrainian, Ukraine (uk_UA) 2.3

Vietnamese, Vietnam (vi_VN) 2.3

Localization implementation in android apps

The Language translation for the android apps to support the Localization. In android apps, if you want to support multiple languages ...