使用语言环境强制Android将特定的strings.xml文件用于不受支持的语言 [英] Using Locale to force Android to use a specific strings.xml file for a non-supported language

查看:422
本文介绍了使用语言环境强制Android将特定的strings.xml文件用于不受支持的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...如果是,怎么办?

...and if so, how?

我们制造了专用于工业环境的Android设备.它基本上是一台平板电脑,但仅运行一个应用程序.用户不应访问设备的任何其他功能,甚至系统设置(例如WiFi和时间设置)都不能通过我们的应用程序执行,而不能通过Android设置小部件来执行. 因此,基本上,他们看到的每个按钮和消息都使用我们的strings.xml文件.

We make a dedicated Android device for use in an industrial environment. It's basically a tablet, but with only one app running. The user is not expected to access any other features of the device and even the system settings, like WiFi and Time settings are performed through our app instead of through the Android Settings widget. So basically every button and message they see uses our strings.xml file.

当前,我们所有的客户都对使用默认的美式英语设置感到满意,但是我们很快就会有一些希望使用本地语言并向我们提供了翻译文件的客户.当前,其中之一是罗马尼亚语,这不是该设备上具有任何本机支持的语言(三星Galaxy标签4);另一个是捷克人.

Currently all of our customers are satisfied to use the default US-English settings but we will soon have some customers who want local languages and have supplied us with translation files. Currently one of them is Romanian, which is not a language with any native support on this device (a Samsung Galaxy tab 4); another is Czech.

因此,我们想在适当的res文件夹中添加strings.xml文件,以使用非英语语言,并在应用程序中添加一个下拉菜单以选择使用的语言.以编程方式,我们认为我们可以使用Locale设置其使用的strings.xml文件,因此,例如,如果从下拉列表中选择了罗马尼亚语,则可以使用Locale将平板电脑设置为罗马尼亚语,因此我们所有应用的用户界面都会使用罗马尼亚语strings.xml文件.

So we want to add strings.xml files in appropriate res folders, for the non-English languages and a dropdown in our app to select which language we're using. Programmatically we think we can use Locale to set which strings.xml file it uses, so for example, if Romanian has been selected from the dropdown we would use Locale to set the tablet into Romanian so all of our app's UI will use the Romanian strings.xml file.

客户无法访问我们的设置,包括新的下拉菜单-现场服务工程师在客户现场进行设置.

Our settings, including the new dropdown, are inaccessible to customers - they're set at the customer site by a field-service engineer.

问题:

  1. 这项工作有用吗?即,即使设备不支持该语言,我们也可以通过Locale控制它使用哪个strings.xml文件吗?
  2. 由于罗马尼亚语不是此设备的母语支持的语言,因此我们假设系统消息仍将以英语显示.这是真的? (如果这样的话,这不是问题-系统消息在我们的应用程序中很少见,并且如果发生这种情况,我们的产品用户也经过培训可以与支持人员联系.我只是想确保将Locale设置为罗马尼亚语或捷克语或其他没有本机支持的语言,如果它尝试发出系统消息,则不会使平板电脑崩溃.
  1. Will this work? I.e., can we control which strings.xml file it uses via Locale, even if the device has no native support for that language?
  2. Since Romanian is not a natively-supported language with this device we assume that system messages will still come up in English. Is this true? (it's not a problem if it does - system messages are rare with our app and the users of our products are trained to contact support if that happens. I just want to make sure that if we set the Locale to Romanian, or Czech or some other language without native support it won't crash the tablet if it does try to issue a system message).

推荐答案

这项工作有效吗?即,即使设备不支持该语言,我们也可以通过Locale控制它使用哪个strings.xml文件吗?

Will this work? I.e., can we control which strings.xml file it uses via Locale, even if the device has no native support for that language?

是的,您可以通过更新 Locale Configuration (请参见下面的示例).如果您尝试使用没有相应资源的语言环境(在您的应用程序或系统中),则

Yes, you can, by updating Locale within Configuration (see an example below). If you try to use the locale for which there are no corresponding resources (either within your app or system), the default string resources (res/values/strings.xml) of your app will be utilized.

由于该设备不是罗马尼亚语支持的本地语言,因此我们假定系统消息仍将以英语显示.这是真的吗?

Since Romanian is not a natively-supported language with this device we assume that system messages will still come up in English. Is this true?

的确如此,如果英语是当前的系统语言环境.

It is true, if English is the current system locale.

我只想确保如果将语言环境设置为罗马尼亚语,捷克语或其他语言而不提供本机支持,则即使它尝试发出系统消息,也不会使平板电脑崩溃.

I just want to make sure that if we set the Locale to Romanian, or Czech or some other language without native support it won't crash the tablet if it does try to issue a system message.

您的应用不会崩溃.在应用内进行的语言环境更改会影响应用的语言环境资源,而不是系统资源.

Your app won't crash. Locale changes made within an app effect locale resources of the app, not system one's.

示例来回答如果是,怎么办?" .该方法可用于在运行Activity *时测试语言环境更改.

An example to answer "if so, how?" The method can be used to test locale changes while an Activity is running*.

public static void changeLocale(Context context, String locale) {
    Resources res = context.getResources();
    Configuration conf = res.getConfiguration();
    conf.locale = new Locale(locale);
    res.updateConfiguration(conf, res.getDisplayMetrics());
}


*您可能要调用重新创建( ),以查看动态"字符串资源的变化.


* You might want to call recreate() to see string resource changes "on the fly".

这篇关于使用语言环境强制Android将特定的strings.xml文件用于不受支持的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆