forked from guilryder/clavier-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI18n.cpp
223 lines (174 loc) · 6.3 KB
/
I18n.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Clavier+
// Keyboard shortcuts manager
//
// Copyright (C) 2000-2008 Guillaume Ryder
//
// This program 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.
//
// This program 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 <https://www.gnu.org/licenses/>.
#include "StdAfx.h"
#include "I18n.h"
#include "MyString.h"
extern HINSTANCE e_instance;
extern HWND e_modal_dialog;
namespace i18n {
namespace {
// Current language.
Language s_lang;
LANGID s_lang_id;
// Same order as the Language enum.
constexpr LANGID kLangIds[kLangCount] = {
MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA),
MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH),
MAKELANGID(LANG_ITALIAN, SUBLANG_ITALIAN),
MAKELANGID(LANG_HUNGARIAN, SUBLANG_DEFAULT),
MAKELANGID(LANG_DUTCH, SUBLANG_DUTCH_BELGIAN),
MAKELANGID(LANG_POLISH, SUBLANG_DEFAULT),
MAKELANGID(LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN),
MAKELANGID(LANG_SLOVAK, SUBLANG_DEFAULT),
MAKELANGID(LANG_FINNISH, SUBLANG_DEFAULT),
MAKELANGID(LANG_GREEK, SUBLANG_DEFAULT),
MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT),
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL),
MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT),
};
void* loadResource(UINT id, LPCTSTR type) {
const HRSRC resource = FindResourceEx(e_instance, type, MAKEINTRESOURCE(id), s_lang_id);
VERIFP(resource, nullptr);
const HGLOBAL resource_data = LoadResource(e_instance, resource);
VERIFP(resource_data, nullptr);
return LockResource(resource_data);
}
} // namespace
Language getLanguage() {
return s_lang;
}
void setLanguage(Language lang) {
s_lang = lang;
s_lang_id = kLangIds[lang];
}
Language getDefaultLanguage() {
const LANGID lang_id = PRIMARYLANGID(LANGIDFROMLCID(GetUserDefaultLCID()));
for (int lang = 0; lang < arrayLength(kLangIds); lang++) {
if (PRIMARYLANGID(kLangIds[lang]) == lang_id) {
return Language(lang);
}
}
return kLangDefault;
}
LPCTSTR getLanguageDonateUrl() {
switch (s_lang) {
case kLangEN:
case kLangES:
case kLangPT:
case kLangRU:
case kLangZH_CN:
case kLangZH_TW:
case kLangJA:
return _T("https://www.paypal.com/donate/?business=guillaume@ryder.fr&item_name=UtilFr+-+Clavier%2b¤cy_code=USD");
default:
return _T("https://www.paypal.com/donate/?business=guillaume@ryder.fr&item_name=UtilFr+-+Clavier%2b¤cy_code=EUR");
}
}
const STRING_RESOURCE* loadStringResource(UINT id) {
const auto* resource = reinterpret_cast<const STRING_RESOURCE*>(loadResource(id / 16 + 1, RT_STRING));
VERIFP(resource, nullptr);
for (id &= 15; id > 0; id--) {
resource = reinterpret_cast<const STRING_RESOURCE*>(resource->strbuf + resource->length);
}
return resource;
}
void loadString(UINT id, LPTSTR strbuf, int buffer_length) {
const STRING_RESOURCE *const resource = loadStringResource(id);
if (buffer_length > resource->length) {
buffer_length = resource->length + 1;
}
if (resource) {
resource->copy(strbuf, buffer_length);
} else {
*strbuf = _T('\0');
}
}
INT_PTR dialogBox(UINT id, HWND hwnd_parent, DLGPROC window_proc, LPARAM init_param) {
const HWND hdlgModalOld = e_modal_dialog;
const INT_PTR result = DialogBoxIndirectParam(
e_instance,
reinterpret_cast<const DLGTEMPLATE*>(loadResource(id, RT_DIALOG)),
hwnd_parent, window_proc, init_param);
e_modal_dialog = hdlgModalOld;
return result;
}
HMENU loadMenu(UINT id) {
return LoadMenuIndirect(reinterpret_cast<const MENUTEMPLATE*>(loadResource(id, RT_MENU)));
}
HBITMAP loadBitmap(UINT id) {
auto *bitmap_info = reinterpret_cast<BITMAPINFO*>(loadResource(id, RT_BITMAP));
VERIFP(bitmap_info, NULL);
const HDC hdc = GetDC(/* hWnd= */ NULL);
const HBITMAP bitmap = CreateDIBitmap(
hdc, &bitmap_info->bmiHeader, CBM_INIT,
bitmap_info->bmiColors + bitmap_info->bmiHeader.biClrUsed,
bitmap_info, DIB_RGB_COLORS);
ReleaseDC(/* hWnd= */ NULL, hdc);
return bitmap;
}
HICON loadNeutralIcon(UINT id, int cx, int cy) {
return reinterpret_cast<HICON>(
LoadImage(e_instance, MAKEINTRESOURCE(id), IMAGE_ICON, cx,cy, 0));
}
void formatInteger(int number, String* output) {
constexpr LCID locale = LOCALE_USER_DEFAULT;
static NUMBERFMT format;
// Populate format once for all.
static bool format_initialized = false;
if (!format_initialized) {
format_initialized = true;
format = {
.NumDigits = 0,
.LeadingZero = true,
.lpDecimalSep = const_cast<LPTSTR>(_T(".")), // unused
};
// Number groups. Convert the LOCALE_SGROUPING string ("3;2;0") into a number (320).
TCHAR grouping_buf[10];
GetLocaleInfo(locale, LOCALE_SGROUPING, grouping_buf, arrayLength(grouping_buf));
format.Grouping = parseNumberGroupingString(grouping_buf);
static TCHAR thousand_sep_buf[4];
GetLocaleInfo(locale, LOCALE_STHOUSAND, thousand_sep_buf, arrayLength(thousand_sep_buf));
format.lpThousandSep = thousand_sep_buf;
GetLocaleInfo(
locale, LOCALE_RETURN_NUMBER | LOCALE_INEGNUMBER,
reinterpret_cast<LPTSTR>(&format.NegativeOrder),
sizeof(format.NegativeOrder) / sizeof(TCHAR));
}
TCHAR number_buf[kIntegerBufSize];
wsprintf(number_buf, _T("%d"), number);
GetNumberFormat(locale, 0, number_buf, &format, output->getBuffer(kIntegerBufSize), kIntegerBufSize);
}
UINT parseNumberGroupingString(LPCTSTR input) {
UINT result = 0;
LPCTSTR current_char;
for (current_char = input; *current_char; current_char++) {
if (_T('0') <= *current_char && *current_char <= _T('9')) {
result = (result * 10) + (*current_char - _T('0'));
}
}
if (*input && (current_char[-1] == _T('0'))) {
result /= 10;
} else {
result *= 10;
}
return result;
}
} // namespace i18n