Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Translations.cpp « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bbe498f195b814bd881e2359625366b347205b7b (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* (C) 2014-2015 see Authors.txt
*
* This file is part of MPC-HC.
*
* MPC-HC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* MPC-HC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "stdafx.h"
#include "Translations.h"
#include "FileVersionInfo.h"
#include "VersionInfo.h"
#include "PathUtils.h"

static const std::vector<Translations::LanguageResource> languageResources = {
    { 1025,   _T("Arabic"),                   _T("Lang\\mpcresources.ar.dll") },
    { 1067,   _T("Armenian"),                 _T("Lang\\mpcresources.hy.dll") },
    { 1069,   _T("Basque"),                   _T("Lang\\mpcresources.eu.dll") },
    { 1059,   _T("Belarusian"),               _T("Lang\\mpcresources.be.dll") },
    { 1093,   _T("Bengali"),                  _T("Lang\\mpcresources.bn.dll") },
    { 1027,   _T("Catalan"),                  _T("Lang\\mpcresources.ca.dll") },
    { 2052,   _T("Chinese (Simplified)"),     _T("Lang\\mpcresources.zh_CN.dll") },
    { 1028,   _T("Chinese (Traditional)"),    _T("Lang\\mpcresources.zh_TW.dll") },
    { 1050,   _T("Croatian"),                 _T("Lang\\mpcresources.hr.dll") },
    { 1029,   _T("Czech"),                    _T("Lang\\mpcresources.cs.dll") },
    { 1030,   _T("Danish"),                   _T("Lang\\mpcresources.da.dll") },
    { 1043,   _T("Dutch"),                    _T("Lang\\mpcresources.nl.dll") },
    { 0,      _T("English"),                  nullptr },
    { 2057,   _T("English (British)"),        _T("Lang\\mpcresources.en_GB.dll") },
    { 1035,   _T("Finnish"),                  _T("Lang\\mpcresources.fi.dll") },
    { 1036,   _T("French"),                   _T("Lang\\mpcresources.fr.dll") },
    { 1110,   _T("Galician"),                 _T("Lang\\mpcresources.gl.dll") },
    { 1031,   _T("German"),                   _T("Lang\\mpcresources.de.dll") },
    { 1032,   _T("Greek"),                    _T("Lang\\mpcresources.el.dll") },
    { 1037,   _T("Hebrew"),                   _T("Lang\\mpcresources.he.dll") },
    { 1038,   _T("Hungarian"),                _T("Lang\\mpcresources.hu.dll") },
    { 1040,   _T("Italian"),                  _T("Lang\\mpcresources.it.dll") },
    { 1041,   _T("Japanese"),                 _T("Lang\\mpcresources.ja.dll") },
    { 1042,   _T("Korean"),                   _T("Lang\\mpcresources.ko.dll") },
    { 1086,   _T("Malay"),                    _T("Lang\\mpcresources.ms_MY.dll") },
    { 1045,   _T("Polish"),                   _T("Lang\\mpcresources.pl.dll") },
    { 1046,   _T("Portuguese (Brazil)"),      _T("Lang\\mpcresources.pt_BR.dll") },
    { 1048,   _T("Romanian"),                 _T("Lang\\mpcresources.ro.dll") },
    { 1049,   _T("Russian"),                  _T("Lang\\mpcresources.ru.dll") },
    { 3098,   _T("Serbian"),                  _T("Lang\\mpcresources.sr.dll") },
    { 1051,   _T("Slovak"),                   _T("Lang\\mpcresources.sk.dll") },
    { 1060,   _T("Slovenian"),                _T("Lang\\mpcresources.sl.dll") },
    { 1053,   _T("Swedish"),                  _T("Lang\\mpcresources.sv.dll") },
    { 3082,   _T("Spanish"),                  _T("Lang\\mpcresources.es.dll") },
    { 1092,   _T("Tatar"),                    _T("Lang\\mpcresources.tt.dll") },
    { 1054,   _T("Thai"),                     _T("Lang\\mpcresources.th_TH.dll") },
    { 1055,   _T("Turkish"),                  _T("Lang\\mpcresources.tr.dll") },
    { 1058,   _T("Ukrainian"),                _T("Lang\\mpcresources.uk.dll") },
    { 1066,   _T("Vietnamese"),               _T("Lang\\mpcresources.vi.dll") }
};

Translations::LanguageResource Translations::GetLanguageResourceByLocaleID(LANGID localeID)
{
    Translations::LanguageResource defaultResource;

    for (auto& lr : languageResources) {
        if (localeID == lr.localeID) {
            return lr;
        } else if (0 == lr.localeID) {
            defaultResource = lr;
        }
    }

    return defaultResource;
}

std::list<Translations::LanguageResource> Translations::GetAvailableLanguageResources()
{
    std::list<Translations::LanguageResource> availableResources;

    CString appPath = PathUtils::GetProgramPath();

    for (auto& lr : languageResources) {
        if (0 == lr.localeID || PathUtils::Exists(PathUtils::CombinePaths(appPath, lr.dllPath))) {
            availableResources.emplace_back(lr);
        }
    }

    return availableResources;
}

LANGID Translations::SetDefaultLanguage()
{
    auto languageResource = GetLanguageResourceByLocaleID(GetUserDefaultUILanguage());

    // Try to set the language resource but don't fail if it can't be loaded
    // English will we used instead in case of error
    return SetLanguage(languageResource, false) ? languageResource.localeID : 0;
}

static LRESULT CALLBACK RTLWindowsLayoutCbtFilterHook(int code, WPARAM wParam, LPARAM lParam)
{
    if (code == HCBT_CREATEWND) {
        HWND hWnd = (HWND)wParam;
        if ((GetWindowLongPtr(hWnd, GWL_STYLE) & WS_CHILD) == 0) {
            SetWindowLongPtr(hWnd, GWL_EXSTYLE, GetWindowLongPtr(hWnd, GWL_EXSTYLE) | WS_EX_LAYOUTRTL);
        }
    }
    return CallNextHookEx(nullptr, code, wParam, lParam);
}

bool Translations::SetLanguage(LanguageResource languageResource, bool showErrorMsg /*= true*/)
{
    HMODULE hMod = nullptr;
    bool success = false;

    // Note that all messages should stay in English in that method!

    // Try to load the resource dll if any
    if (languageResource.dllPath) {
        hMod = LoadLibrary(languageResource.dllPath);
        if (hMod == nullptr) { // The dll failed to load for some reason
            if (showErrorMsg) {
                MessageBox(nullptr, _T("Error loading the chosen language.\n\nPlease reinstall MPC-HC."),
                           _T("MPC-HC"), MB_ICONWARNING | MB_OK);
            }
        } else { // Check if the version of the resource dll is correct
            CString strSatVersion = FileVersionInfo::GetFileVersionStr(languageResource.dllPath);
            CString strNeededVersion;
            strNeededVersion.Format(_T("%u.%u.%u.0"), VersionInfo::GetMajorNumber(), VersionInfo::GetMinorNumber(), VersionInfo::GetPatchNumber());

            if (strSatVersion == strNeededVersion) {
                success = true;
            } else { // The version wasn't correct
                if (showErrorMsg) {
                    int sel = MessageBox(nullptr, _T("Your language pack will not work with this version.\n\nDo you want to visit the download page to get a full package including the translations?"),
                                         _T("MPC-HC"), MB_ICONWARNING | MB_YESNO);
                    if (sel == IDYES) {
                        ShellExecute(nullptr, _T("open"), DOWNLOAD_URL, nullptr, nullptr, SW_SHOWDEFAULT);
                    }
                }
                // Free the loaded resource dll
                FreeLibrary(hMod);
                hMod = nullptr;
            }
        }
    }

    // In case no dll was loaded, load the English translation from the executable
    if (hMod == nullptr) {
        hMod = AfxGetApp()->m_hInstance;
        // If a resource dll was supposed to be loaded we had an error
        success = (languageResource.dllPath == nullptr);
    }
    // In case a dll was loaded, check if some special action is needed
    else if (PRIMARYLANGID(languageResource.localeID) == LANG_ARABIC || PRIMARYLANGID(languageResource.localeID) == LANG_HEBREW) {
        // Hebrew needs the RTL flag.
        SetProcessDefaultLayout(LAYOUT_RTL);
        SetWindowsHookEx(WH_CBT, RTLWindowsLayoutCbtFilterHook, nullptr, GetCurrentThreadId());
    }

    // Free the old resource if it was a dll
    if (AfxGetResourceHandle() != AfxGetApp()->m_hInstance) {
        FreeLibrary(AfxGetResourceHandle());
    }
    // Set the new resource
    AfxSetResourceHandle(hMod);

    return success;
}