如何将Microsoft区域设置ID(LCID)转换为Java中的语言代码或Locale对象 [英] How to convert Microsoft Locale ID (LCID) into language code or Locale object in Java

查看:138
本文介绍了如何将Microsoft区域设置ID(LCID)转换为Java中的语言代码或Locale对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要翻译 Microsoft区域设置ID ,例如1033 (对于美国英语),可以使用 ISO 639语言代码,也可以直接使用Java 区域设置实例。 (编辑:或者甚至简单地进入微软表格中的语言 - 国家/地区。)

I need to translate a Microsoft locale ID, such as 1033 (for US English), into either an ISO 639 language code or directly into a Java Locale instance. ( or even simply into the "Language - Country/Region" in Microsoft's table.)

这是可能的,最简单的方法是什么?当然,最好只使用JDK标准库,但如果不可能,则使用第三方库。

Is this possible, and what's the easiest way? Preferably using only JDK standard libraries, of course, but if that's not possible, with a 3rd party library.

推荐答案

当它开始时看起来没有现成的Java解决方案来进行这种映射,我们花了大约20分钟来推出我们自己的东西,至少目前是这样。

As it started to look like there is no ready Java solution to do this mapping, we took the ~20 minutes to roll something of our own, at least for now.

我们拿了来自马口的信息,即 http://msdn.microsoft.com/en- us / goglobal / bb964664.aspx ,并将其(通过Excel)复制粘贴到.properties文件中,如下所示:

We took the information from the horse's mouth, i.e. http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx, and copy-pasted it (through Excel) into a .properties file like this:

1078 = Afrikaans - South Africa
1052 = Albanian - Albania
1118 = Amharic - Ethiopia
1025 = Arabic - Saudi Arabia
5121 = Arabic - Algeria 
...

(您可以下载文件这里如果你有类似的需求。)

(You can download the file here if you have similar needs.)

T hen有一个非常简单的类,它将.properties文件中的信息读入地图,并有一个进行转换的方法。

Then there's a very simple class that reads the information from the .properties file into a map, and has a method for doing the conversion.

Map<String, String> lcidToDescription;

public String getDescription(String lcid) { ... }

是的,这实际上并没有映射到语言代码 Locale对象(这是我最初提出的问题),而是映射到Microsoft的语言 - 国家/地区描述。事实证明,这足以满足我们当前的需求。

And yes, this doesn't actually map to language code or Locale object (which is what I originally asked), but to Microsoft's "Language - Country/Region" description. It turned out this was sufficient for our current need.

免责声明:这实际上是一种简约的虚拟方式,可以用Java自己做,并且显然保持(和维护)你自己的代码库中的LCID映射信息的副本不是很优雅。 (另一方面,我也不想包含一个巨大的库jar或者为这个简单的映射做任何过于复杂的事情。)所以尽管有这个答案,随时发布更优雅的解决方案或现有的库如果你知道这样的话。

Disclaimer: this really is a minimalistic, "dummy" way of doing it yourself in Java, and obviously keeping (and maintaining) a copy of the LCID mapping information in your own codebase is not very elegant. (On the other hand, neither would I want to include a huge library jar or do anything overly complicated just for this simple mapping.) So despite this answer, feel free to post more elegant solutions or existing libraries if you know of anything like that.

这篇关于如何将Microsoft区域设置ID(LCID)转换为Java中的语言代码或Locale对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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