void ASLocalizer::setLanguageFromLCID(size_t lcid) // Windows get the language to use from the user locale. // NOTE: GetUserDefaultLocaleName() gets nearly the same name as Linux. // But it needs Windows Vista or higher. // Same with LCIDToLocaleName(). { m_lcid = lcid; m_langID = "en"; // default to english size_t lang = PRIMARYLANGID(LANGIDFROMLCID(m_lcid)); size_t sublang = SUBLANGID(LANGIDFROMLCID(m_lcid)); // find language in the wlc table size_t count = sizeof(wlc) / sizeof(wlc[0]); for (size_t i = 0; i < count; i++) { if (wlc[i].winLang == lang) { m_langID = wlc[i].canonicalLang; break; } } if (m_langID == "zh") { if (sublang == SUBLANG_CHINESE_SIMPLIFIED || sublang == SUBLANG_CHINESE_SINGAPORE) m_subLangID = "CHS"; else m_subLangID = "CHT"; // default } setTranslationClass(); }
ASLocalizer::ASLocalizer() // Set the locale information. { // set language default values to english (ascii) // this will be used if a locale or a language cannot be found m_localeName = "UNKNOWN"; m_langID = "en"; m_lcid = 0; m_subLangID.clear(); m_translation = NULL; // Not all compilers support the C++ function locale::global(locale("")); // For testing on Windows change the "Region and Language" settings or use AppLocale. // For testing on Linux change the LANG environment variable: LANG=fr_FR.UTF-8. // setlocale() will use the LANG environment variable on Linux. char* localeName = setlocale(LC_ALL, ""); if (localeName == NULL) // use the english (ascii) defaults { fprintf(stderr, "\n%s\n\n", "Cannot set native locale, reverting to English"); setTranslationClass(); return; } // set the class variables #ifdef _WIN32 size_t lcid = GetUserDefaultLCID(); setLanguageFromLCID(lcid); #else setLanguageFromName(localeName); #endif }
void ASLocalizer::setLanguageFromName(const char* langID) // Linux set the language to use from the langID. // // the language string has the following form // // lang[_LANG][.encoding][@modifier] // // (see environ(5) in the Open Unix specification) // // where lang is the primary language, LANG is a sublang/territory, // encoding is the charset to use and modifier "allows the user to select // a specific instance of localization data within a single category" // // for example, the following strings are valid: // fr // fr_FR // de_DE.iso88591 // de_DE@euro // de_DE.iso88591@euro { // the constants describing the format of lang_LANG locale string static const size_t LEN_LANG = 2; static const size_t LEN_SUBLANG = 2; m_lcid = 0; string langStr = langID; m_langID = langStr.substr(0, LEN_LANG); // need the sublang for chinese if (m_langID == "zh" && langStr[LEN_LANG] == '_') { string subLang = langStr.substr(LEN_LANG + 1, LEN_SUBLANG); if (subLang == "CN" || subLang == "SG") m_subLangID = "CHS"; else m_subLangID = "CHT"; // default } setTranslationClass(); }