31

My question:

How can I add or remove the language entries in the "Settings" -> "Language & keyboard settings" -> "Select Locale"(or "Select Language") in the Android phone from the source code level?

I'll provide more details on what I want and what I have done. Please excuse me if I present any silly understanding or information. I'm pretty new to Android development, starting to learn it several days ago. There is still a lot that I don't know.

More info on what I want:

I'm recently working on a task to localize the entire phone product with new languages(I mean, I'm not working on a specific application. This is why I need to add a new locale in the Settings). When I got the development phone, I found there are only several language entries available in the "Select Locale", such as "English", "French" and "Spain". So first of all, I need to add and display some new language entries to this list, such as "Japanese", "Chinese", "Korean".

I have all the source code of Android platform and my own code, so I think this task can be done by modifying the source code or the resource files somewhere. Unfortunately, I don't know where the code and files are.

The "Hello, L10N" tutorial shows a way to add a new locale from "Custom Locale" at run-time. But what I want is to add the new locales at build time so when the phone is started up, the new locales will be displayed in the "Select Locale" directly without any other actions.

What I have done:

I did a lot of research on the internet before I come here. Here is what I found:

  1. Sinkiru in Stackoverflow asked a question about what I am exactly looking for. See it here. However, nobody else replied him. His own reply only tells where to query the CURRENT language setting, which is not what I want. I personally sent him an email for this question but haven't got his reply.

  2. I searched a lot in Stackoverflow, with the keywords "android locale", "android language", "android new locale" and something similar. Most of the questions in this site are asking how to change the locale of an app, how to add multi-language support for an app, etc.. They are NOT what I want.

  3. I searched a lot in the android-platform Google group(http://groups.google.com/group/android-platform). Some people are asking my question, but after reading through the messages I still don't get my answer.. :-( Here is a list of the posts I read in the group(and I do not get what I want from them):

  4. Back to the entry "(a)" listed above. In the "OS localization" message thread, Dianne Hackborn, an Android framework engineer, said the following:

"The settings app just shows you all of the locales that are available in the platform resources. Adding strings for a new locale should automatically have those shown in settings. You tell the build system which locale resources to include when it does the build... unfortunately I can't help you on that part, but maybe someone else can."

His words in bold suggest that my task might be done automatically by adding new locale resource to the source code, and by setting up or changing something in the build system. I'm wondering if someone here has ever done the similar thing before? I'm going to give a try tomorrow.

  1. Last but not least, I searched in the official Android developer site. In the "Run and Test the Localized Application" of the "Hello, L10N" tutorial it says something about the available locales in the "Settings" application, but that is basically talking about what to do in the emulator and this shouldn't be what I need. I want it done on a real phone.

  2. I had also dived into the Android platform source code trying to discover something. I started at the $(ANDR_SRC_ROOT)/Packages/app/Settings/src/.../LocalePicker.java. In the LocalePicker::onCreate() method, I noticed this line of code:

    String[] locales = getAssets().getLocales();

The code below this line tries to set all the obtained locale names to an ArrayAdapter and then set the adapter to the list view to show. Thus I think the "getLocales()" method is the key to the solution.

I then looked into the getLocales() code. getLocales() is a member method of AssetManager and is declared as native, hence I stepped into the $(ANDR_SRC_ROOT)/frameworks/base/libs/utils/AssetManager.cpp which I GUESS should be the actual implementation of the java getLocales() method. The C++ AssetManager::getLocales() finally leads me to the C++ ResTable::getConfigurations() method in which a member variable "mPackageGroups" is scanned through to retrieve all the available ResTable_config items. Thus I think all the available locales should be added into this mPackageGroups during the system started up. However, I lost in the code at last and couldn't find where the locales are added to the mPackageGroups.

4
  • 2
    After some testing, I am now pretty sure that the available locales are obtained through the "String[] locales = getAssets().getLocales();" statement. I'll need to do more testing and study to figure out what is happening behind the scene.
    – yaobin
    Apr 8, 2011 at 5:38
  • @Melug: Wow I'd never expected this question has been viewed for so many times... Looks like this is a pretty common one and maybe I should do something... But, no, I haven't found the answer yet(Soon after I posted this question I left the previous company and since then I haven't touched Android development).
    – yaobin
    Apr 9, 2012 at 6:12
  • @rob I am still looking for answer :)
    – Melug
    Apr 16, 2012 at 2:53
  • you should look in the source code of customlocale at androidadb.com androidadb.com/package/com/android/customlocale/…
    – homi3kh
    Nov 13, 2012 at 15:59

6 Answers 6

2

you have to create "values" folder in "res" folder of android's framework-res package. Name of that new folder have to contain country code of your need ie. values-pl-rPL (the rPL part is required) for polish language (for british english: en-rGB, etc), that should be enough - if not - create strings.xml file in that folder, and add there empty "resources" node.

2

on HTC phones you must also modify /system/customize/CID/default.xml ... That is the file they use to specify locales on their stock roms. If the rom is a WWE, supposedly editing CID is all one need do, but this doesn't always hold true.

1

I think that a normal application should not add locations to the phone. There are special packages like MoreLocale 2 for that purpose. If you'd add a locale, you'd be responsible not only to provide localisation for your own application but for the whole system!

2
  • 2
    Hi Christian, our team are not working on a specific application. We are working on the localization of the entire phone. :-)
    – yaobin
    Apr 1, 2011 at 14:13
  • Hi rob, I would expect that locales are read from the underlying Linux. Maybe you should try looking at the contents of a package like MoreLocale 2 mentioned above. Apr 3, 2011 at 17:57
1

To create or add multiple languages to your application, first you have to define all you string values in the string.xml in the values folder rather than hard coding them in you program. Then you also have to have different Value folders for different languages, e.g. Value-es for Spanish and Value-fr for french. This should do the job. I dont think the application can change locale by default.

0

I think you have to edit the

PRODUCT_LOCALES

Present in build/target/product/languages_full.mk , and add and remove the strings assigned to this.

You can define your custom string like zz_zz and add folder values_zz_zz in each of your res folders.

Have not tried this but this might work.

Note: PRODUCT_LOCALES might be overridden somewhere else depending on your build system

0

For removing languages, if we remove the locale string from PRODUCT_LOCALES, it will work.

I am also looking for a way to add new languages, but just adding to this doesn't help. I think we need to update some low level files or libraries in order to add new languages.

Not the answer you're looking for? Browse other questions tagged or ask your own question.