Adding/Removing the language entries in the “Setti

2019-03-09 12:15发布

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.

6条回答
闹够了就滚
2楼-- · 2019-03-09 12:47

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.

查看更多
神经病院院长
3楼-- · 2019-03-09 12:52

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.

查看更多
【Aperson】
4楼-- · 2019-03-09 12:52

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.

查看更多
一纸荒年 Trace。
5楼-- · 2019-03-09 12:53

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.

查看更多
疯言疯语
6楼-- · 2019-03-09 12:54

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

查看更多
【Aperson】
7楼-- · 2019-03-09 13:00

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!

查看更多
登录 后发表回答