如何将 Microsoft Locale ID (LCID) 转换为 Java 中的语言代码或 Locale 对象
我需要翻译 Microsoft 区域设置 ID,例如 1033(对于美国英语),转换为 ISO 639 语言代码 或直接转换为 Java Locale 实例。 (编辑:或者甚至简单地进入 Microsoft 表中的“语言 - 国家/地区”。)
这可能吗?最简单的方法是什么? 当然,最好仅使用 JDK 标准库,但如果不可能,则使用第 3 方库。
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. (Edit: or even simply into the "Language - Country/Region" in Microsoft's table.)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 GetLocaleInfo 来执行此操作(假设您在 Windows (win2k+) 上运行。
此 C++ 代码演示了如何使用该函数:
将其转换为 JNA 打电话。 (提示:将常量作为整数发出以查找其值。)
示例 JNA 代码:
使用 JNI 有点复杂,但对于相对简单的任务来说是可以管理的。
至少,我会考虑使用本机调用来构建转换数据库。 我不确定 Windows 是否有办法枚举 LCID,但 .Net 中肯定有。 作为构建级别的事情,这并不是一个巨大的负担。 我想避免手动维护列表。
You could use GetLocaleInfo to do this (assuming you were running on Windows (win2k+)).
This C++ code demonstrates how to use the function:
It would not take much work to turn this into a JNA call. (Tip: emit constants as ints to find their values.)
Sample JNA code:
Using JNI is a bit more involved, but is manageable for a relatively trivial task.
At the very least, I would look into using native calls to build your conversion database. I'm not sure if Windows has a way to enumerate the LCIDs, but there's bound to be something in .Net. As a build-level thing, this isn't a huge burden. I would want to avoid manual maintenance of the list.
由于看起来没有现成的 Java 解决方案来完成此映射,因此我们花了约 20 分钟来推出我们自己的解决方案,至少目前是这样。
我们从马口中获取信息,即 http://msdn.microsoft.com /en-us/goglobal/bb964664.aspx,并将其复制粘贴(通过 Excel)到 .properties 文件中,如下所示:(
您可以下载该文件 这里(如果您有类似的需求)。)
然后有一个非常简单的类,它将 .properties 文件中的信息读取到映射,并有一个进行转换的方法。
是的,这实际上并不映射到语言代码或区域设置对象(这是我最初要求的),而是映射到微软的“语言 - 国家/地区”描述。 事实证明这足以满足我们当前的需求。
免责声明:这确实是一种在 Java 中自行实现的简约、“虚拟”方式,显然在您自己的代码库中保留(和维护)LCID 映射信息的副本并不是很优雅。 (另一方面,我也不想仅仅为了这个简单的映射而包含一个巨大的库 jar 或做任何过于复杂的事情。)因此,尽管有这个答案,请随意发布更优雅的解决方案或现有库如果你知道类似的事情。
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.
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:
(You can download the file here if you have similar needs.)
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.
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.
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 LCID 代码和 Java 区域设置之间的映射,从而更轻松地保持映射最新:
Maven 依赖项:
用法:
将打印“语言:英语”。
意思是,LCID 1033 映射到英语。
注意:这只会生成运行时 JVM 上可用的区域设置的映射。 这意味着,您只会获得所有可能的区域设置的子集。 也就是说,我认为在技术上不可能实例化 JVM 不支持的区域设置,所以这可能是我们能做的最好的事情......
The following code will programmatically create a mapping between Microsoft LCID codes and Java Locales, making it easier to keep the mapping up-to-date:
Maven dependencies:
Usage:
will print "Language: English".
Meaning, LCID 1033 maps to the English language.
NOTE: This only generates mappings for locales available on your runtime JVM. Meaning, you will only get a subset of all possible Locales. That said, I don't think it is technically possible to instantiate Locales that your JVM doesn't support, so this is probably the best we can do...
这是 Google 上的第一个点击< “Java LCID”的 /a> 是这个 javadoc:
我不知道去哪里下载这个库,但它是 GNU 的,所以应该不难找到。
The was the first hit on google for "Java LCID" is this javadoc:
I'm not sure where to go about downloading this library, but it's GNU, so it shouldn't be too hard to find.
下面是一个脚本,用于粘贴到 F12 控制台并将当前 273 种语言的映射提取到其 lcid(用于 https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5 -ce1a8b0cdb9c):
Here is a script to paste into the F12 console and extract the mapping for the currently 273 languages to their lcid (to be used on https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c):
使用 Apache POI 可以轻松实现 org.apache .poi.util.LocaleID 类。
Java 区域设置到 LCID:
LCID 到 Java 区域设置:
It is easily possible with Apache POI using the org.apache.poi.util.LocaleID class.
Java Locale to LCID:
LCID to Java Locale: