-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_manager.cpp
More file actions
499 lines (453 loc) · 24.5 KB
/
Copy pathlanguage_manager.cpp
File metadata and controls
499 lines (453 loc) · 24.5 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "language_manager.h"
#include "util.h"
#include <windows.h>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <map>
#include <mutex>
std::wstring g_languageId = L"russian";
static std::map<std::string, std::wstring> g_languageStrings;
static std::vector<LanguageOption> g_languageOptions;
static std::mutex g_languageMutex;
static constexpr uintmax_t kMaxLanguageFileBytes = 256 * 1024;
static constexpr size_t kMaxLanguageLineBytes = 4096;
static constexpr size_t kMaxLanguageEntries = 512;
static constexpr size_t kMaxLanguageKeyBytes = 128;
static constexpr size_t kMaxLanguageValueBytes = 3072;
static constexpr size_t kMaxLanguageIdChars = 64;
static constexpr size_t kMaxLanguageFiles = 128;
static std::string TrimLanguageAscii(std::string value)
{
auto notSpace = [](unsigned char ch) { return !std::isspace(ch); };
value.erase(value.begin(), std::find_if(value.begin(), value.end(), notSpace));
value.erase(std::find_if(value.rbegin(), value.rend(), notSpace).base(), value.end());
return value;
}
static bool IsValidLanguageId(const std::wstring& id)
{
if (id.empty() || id.size() > kMaxLanguageIdChars || id == L"." || id == L"..") {
return false;
}
for (wchar_t ch : id) {
const bool isAsciiLetter = (ch >= L'A' && ch <= L'Z') || (ch >= L'a' && ch <= L'z');
const bool isDigit = (ch >= L'0' && ch <= L'9');
if (!isAsciiLetter && !isDigit && ch != L'_' && ch != L'-') {
return false;
}
}
return true;
}
static bool IsValidLanguageKey(const std::string& key)
{
if (key.empty() || key.size() > kMaxLanguageKeyBytes) {
return false;
}
for (unsigned char ch : key) {
const bool isAsciiLetter = (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
const bool isDigit = (ch >= '0' && ch <= '9');
if (!isAsciiLetter && !isDigit && ch != '_' && ch != '-' && ch != '.') {
return false;
}
}
return true;
}
static std::filesystem::path GetExeDirectory()
{
wchar_t path[MAX_PATH] = {};
DWORD len = GetModuleFileNameW(nullptr, path, MAX_PATH);
if (len == 0 || len >= MAX_PATH) {
return std::filesystem::current_path();
}
return std::filesystem::path(path).parent_path();
}
static std::filesystem::path GetLanguageDirectory()
{
return GetExeDirectory() / L"Language";
}
static const char* GetDefaultRussianLanguageText()
{
return
"language.name=Русский\n"
"settings.title=Настройки\n"
"settings.ok=OK\n"
"settings.group.recording= Режимы записи \n"
"settings.group.effects= Эффекты воспроизведения \n"
"settings.group.program= Настройки программы \n"
"settings.language=Язык:\n"
"settings.recording.mp3= MP3, LAME, 320 kbit/sec\n"
"settings.recording.flac= FLAC, s16, ~1000 kbit/sec\n"
"settings.effect.stereo_width= Расширение Стерео\n"
"settings.effect.exciter= Exciter / Яркость\n"
"settings.effect.deep_bass= DeepBass / Глубокий Бас\n"
"settings.effect.speech_intelligibility_compressor= Компрессор разборчивости речи\n"
"settings.effect.lufs_gain_normalizer= LUFS-нормализация станций\n"
"settings.effect.gain_rider= GainRider / Контроль Пиков\n"
"settings.program.minimize_to_tray= При минимизации отправлять в трей\n"
"settings.program.show_track_toast= В трее и компактном режиме показывать обложку\n"
"settings.program.compact_always_on_top= Компактный режим поверх всех окон\n"
"settings.program.compact_without_spectrum= Без спектрограммы в компактном режиме\n"
"tooltip.play_stop=Воспроизвести или остановить текущую станцию\n"
"tooltip.prev_station=Перейти к предыдущей станции в списке\n"
"tooltip.next_station=Перейти к следующей станции в списке\n"
"tooltip.previous_station=Вернуться к ранее звучавшей станции\n"
"tooltip.record=Начать или остановить запись текущего потока\n"
"tooltip.volume=Громкость: левая кнопка или колесо вверх увеличивает, правая кнопка или колесо вниз уменьшает\n"
"tooltip.equalizer=Открыть шестиполосный параметрический эквалайзер\n"
"tooltip.settings=Открыть настройки программы\n"
"tooltip.compact.restore=Вернуться в обычный режим\n"
"tooltip.recording.mp3=Записывать поток в MP3 320 kbit/sec\n"
"tooltip.recording.flac=Записывать поток в FLAC без потерь\n"
"tooltip.effect.stereo_width=Включить расширение стереобазы\n"
"tooltip.effect.exciter=Добавить яркость и выразительность верхним частотам\n"
"tooltip.effect.deep_bass=Усилить глубину низких частот\n"
"tooltip.effect.speech_intelligibility_compressor=Поднимать тихие фразы и мягко сжимать громкие для лучшей разборчивости речи\n"
"tooltip.effect.lufs_gain_normalizer=Медленно приводить уровень разных станций к эталону -9.00 LUFS\n"
"tooltip.effect.gain_rider=Контролировать пики и удерживать комфортный уровень сигнала\n"
"tooltip.program.minimize_to_tray=При нажатии кнопки свернуть прятать программу в трей\n"
"tooltip.program.show_track_toast=Когда программа в трее или компактном режиме, показывать обложку при смене трека\n"
"tooltip.program.compact_always_on_top=В компактном режиме держать окно поверх других окон\n"
"tooltip.program.compact_without_spectrum=Скрыть спектрограмму в компактном режиме и оставить строку текущего трека с кнопками\n"
"tooltip.settings.ok=Сохранить настройки и закрыть окно\n"
"tooltip.about.ok=Закрыть окно информации о программе\n"
"tooltip.add.ok=Добавить станцию в список\n"
"tooltip.add.cancel=Закрыть окно без добавления станции\n"
"tray.balloon.title=IRPffmpeg работает в трее\n"
"tray.balloon.text=Дважды щелкните значок, чтобы вернуть окно. Для выхода используйте меню трея.\n"
"tray.restore=Открыть IRPffmpeg\n"
"tray.play=Воспроизвести\n"
"tray.stop=Остановить\n"
"tray.prev=Предыдущая станция\n"
"tray.next=Следующая станция\n"
"tray.previous=Вернуться к прошлой станции\n"
"tray.settings=Настройки\n"
"tray.about=О программе\n"
"tray.exit=Выход\n"
"context.reload_m3u=Обновить m3u\n"
"context.add_station=Добавить станцию\n"
"context.edit_station=Изменить название станции\n"
"context.save_station=Сохранить радиостанцию\n"
"context.delete_station=Удалить станцию\n"
"edit_station.title=Изменить название станции\n"
"edit_station.msg.enter_name=Введите название станции.\n"
"edit_station.msg.one_line=Название станции должно быть записано в одну строку.\n"
"edit_station.msg.too_long=Название станции не должно быть длиннее 128 символов.\n"
"edit_station.msg.create_failed=Не удалось создать форму изменения станции.\n"
"edit_station.msg.open_failed=Не удалось открыть форму изменения станции.\n"
"add.title=Добавить станцию\n"
"eq.title=Параметрический эквалайзер\n"
"add.name_label=Название станции:\n"
"add.url_label=URL - адрес:\n"
"common.ok=OK\n"
"common.cancel=Отмена\n"
"add.msg.enter_name=Введите название станции.\n"
"add.msg.one_line=Название и URL должны быть записаны в одну строку.\n"
"add.msg.enter_url=Введите интернет адрес станции.\n"
"add.msg.invalid_url=URL должен начинаться с http:// или https://\n"
"add.msg.invalid_supported_url=Некорректный URL. Поддерживаются адреса вида http://, https://, icy://, mms:// или rtsp:// без пробелов.\n"
"add.msg.duplicate_url=Станция с таким URL уже есть в плейлисте.\n"
"add.msg.create_failed=Не удалось создать форму добавления станции.\n"
"add.msg.open_failed=Не удалось открыть форму добавления станции.\n"
"nowplaying.no_data=Нет данных о треке\n"
"status.stopped=Остановлено\n"
"status.http_ok=Чтение заголовков (HTTP/1.1 200 OK)\n"
"status.ffmpeg_reconnect=FFmpeg: переподключение к потоку...\n"
"status.ffmpeg_timeout=FFmpeg: таймаут сети / ожидание переподключения\n"
"status.ffmpeg_icy_metadata=FFmpeg: ICY / metadata\n"
"status.http_request=Подключение к URL (HTTP request...)\n"
"status.ffmpeg_prefix=FFmpeg: \n"
"status.reconnect_attempt_prefix=Переподключение... попытка \n"
"status.reconnect_attempt_middle= из \n"
"status.stream_unavailable_timeout=Поток недоступен / таймаут подключения\n"
"status.reconnect_url_prefix=Переподключение к URL (\n"
"status.reconnect_url_middle=), попытка \n"
"status.connect_url_prefix=Подключение к URL (\n"
"status.connect_url_suffix=)\n"
"status.avformat_alloc_error=FFmpeg: ошибка avformat_alloc_context()\n"
"status.reading_stream_headers=Чтение заголовков потока...\n"
"status.retry_after_timeout_prefix=Попытка \n"
"status.retry_after_timeout_suffix= после таймаута.\n"
"status.connection_attempts_exceeded=Поток недоступен: превышено число попыток подключения\n"
"status.analyzing_stream=Анализ потока и определение формата...\n"
"status.stream_header_read_error=Ошибка чтения заголовков потока\n"
"status.demuxer_prefix=Используемый демультиплексер: \n"
"status.audio_stream_not_found=Не найден аудиопоток\n"
"status.audio_output_init_error=Ошибка инициализации аудиовывода\n"
"status.resampler_init_error=Ошибка инициализации ресемплера\n"
"status.stream_read_error_reconnect=Ошибка чтения потока. Переподключение...\n"
"status.stream_reconnect_attempts_exceeded=Ошибка потока: превышено число попыток переподключения\n"
"status.audio_device_changed=Аудиоустройство изменено, переинициализация...\n"
"status.skipping_bad_server_data=Пропускаем битые данные от сервера...\n"
"msg.file_error=File Error\n"
"msg.playlist_error=Playlist Error\n"
"msg.sdl_error=SDL Error\n"
"msg.sdl_image_error=SDL_image Error\n";
}
static const char* GetDefaultEnglishLanguageText()
{
return
"language.name=English\n"
"settings.title=Settings\n"
"settings.ok=OK\n"
"settings.group.recording= Recording modes \n"
"settings.group.effects= Playback effects \n"
"settings.group.program= Program settings \n"
"settings.language=Language:\n"
"settings.recording.mp3= MP3, LAME, 320 kbit/sec\n"
"settings.recording.flac= FLAC, s16, ~1000 kbit/sec\n"
"settings.effect.stereo_width= Stereo Width\n"
"settings.effect.exciter= Exciter / Brightness\n"
"settings.effect.deep_bass= DeepBass\n"
"settings.effect.speech_intelligibility_compressor= Speech Intelligibility Compressor\n"
"settings.effect.lufs_gain_normalizer= Station LUFS Normalizer\n"
"settings.effect.gain_rider= GainRider / Peak Control\n"
"settings.program.minimize_to_tray= Minimize to tray\n"
"settings.program.show_track_toast= Show cover popup in tray and compact mode\n"
"settings.program.compact_always_on_top= Keep compact mode on top\n"
"settings.program.compact_without_spectrum= Compact mode without spectrum\n"
"tooltip.play_stop=Play or stop the current station\n"
"tooltip.prev_station=Switch to the previous station in the list\n"
"tooltip.next_station=Switch to the next station in the list\n"
"tooltip.previous_station=Return to the previously played station\n"
"tooltip.record=Start or stop recording the current stream\n"
"tooltip.volume=Volume: left click or mouse wheel up raises it, right click or mouse wheel down lowers it\n"
"tooltip.equalizer=Open the six-band parametric equalizer\n"
"tooltip.settings=Open program settings\n"
"tooltip.compact.restore=Return to normal mode\n"
"tooltip.recording.mp3=Record the stream as MP3 320 kbit/sec\n"
"tooltip.recording.flac=Record the stream as lossless FLAC\n"
"tooltip.effect.stereo_width=Enable stereo widening\n"
"tooltip.effect.exciter=Add brightness and presence to high frequencies\n"
"tooltip.effect.deep_bass=Enhance low-frequency depth\n"
"tooltip.effect.speech_intelligibility_compressor=Lift quiet speech and gently compress loud passages for clearer dialogue\n"
"tooltip.effect.lufs_gain_normalizer=Slowly match station loudness to the -9.00 LUFS reference\n"
"tooltip.effect.gain_rider=Control peaks and keep a comfortable signal level\n"
"tooltip.program.minimize_to_tray=Hide the program in the tray when minimized\n"
"tooltip.program.show_track_toast=When the program is in tray or compact mode, show the cover on track change\n"
"tooltip.program.compact_always_on_top=Keep the compact window above other windows\n"
"tooltip.program.compact_without_spectrum=Hide the spectrum in compact mode and keep the current-track bar and controls\n"
"tooltip.settings.ok=Save settings and close this window\n"
"tooltip.about.ok=Close the about window\n"
"tooltip.add.ok=Add the station to the list\n"
"tooltip.add.cancel=Close without adding a station\n"
"tray.balloon.title=IRPffmpeg is running in the tray\n"
"tray.balloon.text=Double-click the icon to restore the window. Use the tray menu to exit.\n"
"tray.restore=Open IRPffmpeg\n"
"tray.play=Play\n"
"tray.stop=Stop\n"
"tray.prev=Previous station\n"
"tray.next=Next station\n"
"tray.previous=Return to previous station\n"
"tray.settings=Settings\n"
"tray.about=About\n"
"tray.exit=Exit\n"
"context.reload_m3u=Reload m3u\n"
"context.add_station=Add station\n"
"context.edit_station=Edit station name\n"
"context.save_station=Save station\n"
"context.delete_station=Delete station\n"
"edit_station.title=Edit station name\n"
"edit_station.msg.enter_name=Enter a station name.\n"
"edit_station.msg.one_line=Station name must fit on one line.\n"
"edit_station.msg.too_long=Station name must not be longer than 128 characters.\n"
"edit_station.msg.create_failed=Could not create the station edit form.\n"
"edit_station.msg.open_failed=Could not open the station edit form.\n"
"add.title=Add station\n"
"eq.title=Parametric equalizer\n"
"add.name_label=Station name:\n"
"add.url_label=URL address:\n"
"common.ok=OK\n"
"common.cancel=Cancel\n"
"add.msg.enter_name=Enter a station name.\n"
"add.msg.one_line=Name and URL must each fit on one line.\n"
"add.msg.enter_url=Enter the station internet address.\n"
"add.msg.invalid_url=URL must start with http:// or https://\n"
"add.msg.invalid_supported_url=Invalid URL. Supported addresses look like http://, https://, icy://, mms:// or rtsp:// and must not contain spaces.\n"
"add.msg.duplicate_url=A station with this URL is already in the playlist.\n"
"add.msg.create_failed=Could not create the add station form.\n"
"add.msg.open_failed=Could not open the add station form.\n"
"nowplaying.no_data=No track data\n"
"status.stopped=Stopped\n"
"status.http_ok=Reading headers (HTTP/1.1 200 OK)\n"
"status.ffmpeg_reconnect=FFmpeg: reconnecting to stream...\n"
"status.ffmpeg_timeout=FFmpeg: network timeout / waiting to reconnect\n"
"status.ffmpeg_icy_metadata=FFmpeg: ICY / metadata\n"
"status.http_request=Connecting to URL (HTTP request...)\n"
"status.ffmpeg_prefix=FFmpeg: \n"
"status.reconnect_attempt_prefix=Reconnecting... attempt \n"
"status.reconnect_attempt_middle= of \n"
"status.stream_unavailable_timeout=Stream unavailable / connection timeout\n"
"status.reconnect_url_prefix=Reconnecting to URL (\n"
"status.reconnect_url_middle=), attempt \n"
"status.connect_url_prefix=Connecting to URL (\n"
"status.connect_url_suffix=)\n"
"status.avformat_alloc_error=FFmpeg: avformat_alloc_context() failed\n"
"status.reading_stream_headers=Reading stream headers...\n"
"status.retry_after_timeout_prefix=Attempt \n"
"status.retry_after_timeout_suffix= after timeout.\n"
"status.connection_attempts_exceeded=Stream unavailable: connection attempt limit exceeded\n"
"status.analyzing_stream=Analyzing stream and detecting format...\n"
"status.stream_header_read_error=Failed to read stream headers\n"
"status.demuxer_prefix=Active demuxer: \n"
"status.audio_stream_not_found=Audio stream not found\n"
"status.audio_output_init_error=Audio output initialization failed\n"
"status.resampler_init_error=Resampler initialization failed\n"
"status.stream_read_error_reconnect=Stream read error. Reconnecting...\n"
"status.stream_reconnect_attempts_exceeded=Stream error: reconnect attempt limit exceeded\n"
"status.audio_device_changed=Audio device changed, reinitializing...\n"
"status.skipping_bad_server_data=Skipping corrupted data from server...\n"
"msg.file_error=File Error\n"
"msg.playlist_error=Playlist Error\n"
"msg.sdl_error=SDL Error\n"
"msg.sdl_image_error=SDL_image Error\n";
}
static void WriteDefaultLanguageFileIfMissing(const std::filesystem::path& path, const char* text)
{
std::error_code ec;
if (std::filesystem::exists(path, ec)) {
return;
}
std::ofstream out(path, std::ios::binary);
if (out.is_open()) {
out.write(text, static_cast<std::streamsize>(std::strlen(text)));
}
}
static void EnsureDefaultLanguageFiles()
{
std::error_code ec;
std::filesystem::create_directories(GetLanguageDirectory(), ec);
WriteDefaultLanguageFileIfMissing(GetLanguageDirectory() / L"russian.lng", GetDefaultRussianLanguageText());
WriteDefaultLanguageFileIfMissing(GetLanguageDirectory() / L"english.lng", GetDefaultEnglishLanguageText());
}
static std::map<std::string, std::wstring> LoadLanguageFile(const std::filesystem::path& path)
{
std::map<std::string, std::wstring> result;
std::error_code ec;
if (!std::filesystem::is_regular_file(path, ec)) {
return result;
}
const uintmax_t fileSize = std::filesystem::file_size(path, ec);
if (ec || fileSize == 0 || fileSize > kMaxLanguageFileBytes) {
return result;
}
std::ifstream in(path, std::ios::binary);
if (!in.is_open()) {
return result;
}
std::string line;
while (result.size() < kMaxLanguageEntries && std::getline(in, line)) {
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.size() > kMaxLanguageLineBytes) {
continue;
}
if (line.empty() || line[0] == '#' || line[0] == ';') {
continue;
}
size_t eq = line.find('=');
if (eq == std::string::npos) {
continue;
}
std::string key = TrimLanguageAscii(line.substr(0, eq));
if (key.size() >= 3 &&
static_cast<unsigned char>(key[0]) == 0xEF &&
static_cast<unsigned char>(key[1]) == 0xBB &&
static_cast<unsigned char>(key[2]) == 0xBF) {
key.erase(0, 3);
}
if (!IsValidLanguageKey(key)) {
continue;
}
std::string value = line.substr(eq + 1);
if (value.size() > kMaxLanguageValueBytes) {
value.resize(kMaxLanguageValueBytes);
}
result[key] = utf8_to_wstring(value);
}
return result;
}
static bool HasLanguageIdentity(const std::map<std::string, std::wstring>& strings)
{
auto it = strings.find("language.name");
return it != strings.end() && !it->second.empty();
}
bool LoadLanguageById(const std::wstring& languageId)
{
if (!IsValidLanguageId(languageId)) {
return false;
}
auto strings = LoadLanguageFile(GetLanguageDirectory() / (languageId + L".lng"));
if (strings.empty() || !HasLanguageIdentity(strings)) {
return false;
}
{
std::lock_guard<std::mutex> lock(g_languageMutex);
g_languageStrings = std::move(strings);
g_languageId = languageId;
}
return true;
}
void InitializeLanguageSystem()
{
EnsureDefaultLanguageFiles();
if (!LoadLanguageById(g_languageId)) {
g_languageId = L"russian";
LoadLanguageById(g_languageId);
}
}
const wchar_t* Tr(const char* key, const wchar_t* fallback)
{
thread_local std::wstring translated;
translated = TrString(key, fallback);
return translated.c_str();
}
std::wstring TrString(const char* key, const wchar_t* fallback)
{
if (!key) {
return fallback ? fallback : L"";
}
std::lock_guard<std::mutex> lock(g_languageMutex);
auto it = g_languageStrings.find(key);
if (it != g_languageStrings.end()) {
return it->second;
}
return fallback ? fallback : L"";
}
void LoadAvailableLanguages()
{
g_languageOptions.clear();
EnsureDefaultLanguageFiles();
std::error_code ec;
for (const auto& entry : std::filesystem::directory_iterator(GetLanguageDirectory(), ec)) {
if (ec) {
break;
}
if (g_languageOptions.size() >= kMaxLanguageFiles) {
break;
}
std::error_code entryEc;
if (!entry.is_regular_file(entryEc) || entry.path().extension() != L".lng") {
continue;
}
const std::wstring id = entry.path().stem().wstring();
if (!IsValidLanguageId(id)) {
continue;
}
auto strings = LoadLanguageFile(entry.path());
if (!HasLanguageIdentity(strings)) {
continue;
}
g_languageOptions.push_back({ id, strings["language.name"] });
}
std::sort(g_languageOptions.begin(), g_languageOptions.end(),
[](const LanguageOption& a, const LanguageOption& b) {
return a.displayName < b.displayName;
});
}
const std::vector<LanguageOption>& GetAvailableLanguages()
{
return g_languageOptions;
}