diff options
Diffstat (limited to '7zip/FileManager/LangUtils.cpp')
-rwxr-xr-x | 7zip/FileManager/LangUtils.cpp | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/7zip/FileManager/LangUtils.cpp b/7zip/FileManager/LangUtils.cpp index a5fcb635..2e4cd82a 100755 --- a/7zip/FileManager/LangUtils.cpp +++ b/7zip/FileManager/LangUtils.cpp @@ -4,9 +4,11 @@ #include "LangUtils.h" #include "Common/StringConvert.h" +#include "Common/StringToInt.h" #include "Windows/ResourceString.h" #include "Windows/Synchronization.h" #include "Windows/Window.h" +#include "Windows/FileFind.h" #include "RegistryUtils.h" #include "ProgramLocation.h" @@ -19,7 +21,7 @@ void ReloadLang() { ReadRegLang(g_LangID); g_Lang.Clear(); - if (!g_LangID.IsEmpty()) + if (!g_LangID.IsEmpty() && g_LangID != TEXT("-")) { CSysString langPath = g_LangID; if (langPath.Find('\\') < 0) @@ -98,3 +100,96 @@ UString LangLoadStringW(UINT resourceID, UInt32 langID) return message; return NWindows::MyLoadStringW(resourceID); } + +void LoadLangs(CObjectVector<CLangEx> &langs) +{ + langs.Clear(); + UString folderPath; + if (!::GetProgramFolderPath(folderPath)) + return; + folderPath += L"Lang\\"; + NWindows::NFile::NFind::CEnumeratorW enumerator(folderPath + L"*.txt"); + NWindows::NFile::NFind::CFileInfoW fileInfo; + while (enumerator.Next(fileInfo)) + { + if (fileInfo.IsDirectory()) + continue; + CLangEx lang; + UString filePath = folderPath + fileInfo.Name; + const kExtSize = 4; + if (fileInfo.Name.Right(kExtSize) != L".txt") + continue; + lang.ShortName = fileInfo.Name.Left(fileInfo.Name.Length() - kExtSize); + if (lang.Lang.Open(GetSystemString(filePath))) + langs.Add(lang); + } +} + +bool SplidID(const UString &id, WORD &primID, WORD &subID) +{ + primID = 0; + subID = 0; + const wchar_t *start = id; + const wchar_t *end; + UInt64 value = ConvertStringToUInt64(start, &end); + if (start == end) + return false; + primID = (WORD)value; + if (*end == 0) + return true; + if (*end != L'-') + return false; + start = end + 1; + value = ConvertStringToUInt64(start, &end); + if (start == end) + return false; + subID = (WORD)value; + return (*end == 0); +} + +void FindMatchLang(UString &shortName) +{ + shortName.Empty(); + LANGID langID = GetUserDefaultLangID(); + WORD primLang = PRIMARYLANGID(langID); + WORD subLang = SUBLANGID(langID); + CObjectVector<CLangEx> langs; + LoadLangs(langs); + for (int i = 0; i < langs.Size(); i++) + { + const CLangEx &lang = langs[i]; + UString id; + if (lang.Lang.GetMessage(0x00000002, id)) + { + WORD primID; + WORD subID; + if (SplidID(id, primID, subID)) + if (primID == primLang) + { + if (subID == 0) + shortName = lang.ShortName; + if (subLang == subID) + { + shortName = lang.ShortName; + return; + } + } + } + } +} + +void ReloadLangSmart() +{ + #ifdef _UNICODE + ReadRegLang(g_LangID); + if (g_LangID.IsEmpty()) + { + UString shortName; + FindMatchLang(shortName); + if (shortName.IsEmpty()) + shortName = L"-"; + SaveRegLang(GetSystemString(shortName)); + } + #endif + ReloadLang(); +} |