Converting between LCIDs and RFC 1766 language codes
在 LCID 和 RFC 1766 语言代码之间转换
Occasionally, I see someone ask for a function that converts between LCIDs (such as 0x0409 for English-US) and RFC 1766 language identifiers (such as “en-us”). The rule of thumb is, if it’s something a web browser would need, and it has to do with locales and languages, you should look in the MLang library. In this case, the IMultiLanguage::GetRfc1766FromLcid method does the trick.
有时,我会遇到有人在寻求可以在 LCID(例如美国英语为 0x0409)和 RFC 1766 语言标识符(例如“en-us”)之间进行转换的函数。就经验而言,如果某些东西是 Web 浏览器会用到的,而且又和地域和语言相关,那就应该去 MLang 库(http://msdn.microsoft.com/workshop/misc/mlang/mlang.asp)里去看一下。对于本例,IMultiLanguage::GetRfc1766FromLcid 方法(http://msdn.microsoft.com/workshop/misc/mlang/reference/ifaces/imultilanguage/getrfc1766fromlcid.asp)正是诀窍所在。
For illustration, here’s a program that takes US-English and converts it to RFC 1766 format. For fun, we also convert “sv-fi” (Finland-Swedish) to an LCID.
为了演示,下面的程序会把美国英语(US-English)转换为 RFC 1766 格式。为了活跃气氛,我们还把“sv-fi”(芬兰-瑞典语)转换成了对应的 LCID。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <stdio.h> #include <ole2.h> #include <oleauto.h> #include <mlang.h> int __cdecl main(int argc, char **argv) { HRESULT hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { IMultiLanguage * pml; hr = CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_ALL, IID_IMultiLanguage, (void**)&pml); if (SUCCEEDED(hr)) { // Let's convert US-English to an RFC 1766 string BSTR bs; LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT); hr = pml->GetRfc1766FromLcid(lcid, &bs); if (SUCCEEDED(hr)) { printf("%ws\n", bs); SysFreeString(bs); } // And a sample reverse conversion just for good measure bs = SysAllocString(L"sv-fi"); if (bs && SUCCEEDED(pml->GetLcidFromRfc1766(&lcid, bs))) { printf("%x\n", lcid); } SysFreeString(bs); pml->Release(); } CoUninitialize(); } return 0; } |
When you run this program, you should get
在你运行此程序时,你会得到如下结果:
en-us
81d
“en-us” is the RFC 1766 way of saying “US-English”, and 0x081d is MAKELCID(MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND), SORT_DEFAULT).
“en-us”是 RFC 1766 方式的对美国英语的称呼,而 0x081d 就是 MAKELCID(MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND), SORT_DEFAULT)。
If you browse around, you’ll find lots of other interesting functions in the MLang library. You may recall that earlier we saw how to use MLang to display strings without those ugly boxes.
如果你浏览相邻的内容,你还可以在 MLang 库中找到很多其他感兴趣的功能。你还可能会回忆起早些时候我们如何使用 MLang 来显示字符串而避免出现那些难看的方框。