Skip to content

Commit 3ec3288

Browse files
authored
Add filter for ampersands in PO strings on macOS. (issue #1352, PR #1354)
This fixes broken translation on Mac. Swell doesn't support keyboard mnemonics for controls indicated by '&'. To deal with this, we read and filter the po file into a string and pass an istringstream to tinygettext. We then add entries to the translation dictionary with the '&' chars stripped. This change also adds "Deutsch" to the langpack aliases because that is the name of the German translation linked from https://www.reaper.fm/langpack/
1 parent 81172a0 commit 3ec3288

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/translation.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using namespace std;
2020
// be (and often are) multiple REAPER language packs per language.
2121
map<string, string> REAPER_LANG_TO_CODE = {
2222
{"DE_(+SWS)", "de_DE"},
23+
{"Deutsch", "de_DE"},
2324
{"pt-BR", "pt_BR"},
2425
{"Reaper+SWS_CHSDOU", "zh_CN"},
2526
{"REAPER_zh_CN_www.szzyyzz.com", "zh_CN"},
@@ -32,6 +33,39 @@ map<string, string> REAPER_LANG_TO_CODE = {
3233

3334
tinygettext::Dictionary translationDict;
3435

36+
#ifndef _WIN32
37+
static istringstream filterPoAmpersands(istream& input) {
38+
string filtered;
39+
input.seekg(0, ios::end);
40+
auto size = input.tellg();
41+
if (size > 0) {
42+
filtered.reserve(static_cast<size_t>(size));
43+
}
44+
input.seekg(0, ios::beg);
45+
bool inString = false;
46+
bool escaped = false;
47+
char ch;
48+
while (input.get(ch)) {
49+
if (inString && !escaped && ch == '&') {
50+
continue;
51+
}
52+
if (inString) {
53+
if (escaped) {
54+
escaped = false;
55+
} else if (ch == '\\') {
56+
escaped = true;
57+
} else if (ch == '"') {
58+
inString = false;
59+
}
60+
} else if (ch == '"') {
61+
inString = true;
62+
}
63+
filtered.push_back(ch);
64+
}
65+
return istringstream(filtered);
66+
}
67+
#endif
68+
3569
void initTranslation() {
3670
// Figure out which file name to load. We base it on the REAPER language
3771
// pack.
@@ -67,10 +101,13 @@ void initTranslation() {
67101
// deal with this is to convert the string to UTF-16, which Windows will
68102
// interpret correctly.
69103
ifstream input(widen(path));
104+
tinygettext::POParser::parse(path, input, translationDict);
70105
#else
106+
// SWELL doesn't support mnemonics, so strip ampersands inside PO strings on macOS.
71107
ifstream input(path);
108+
istringstream filteredInput = filterPoAmpersands(input);
109+
tinygettext::POParser::parse(path, filteredInput, translationDict);
72110
#endif
73-
tinygettext::POParser::parse(path, input, translationDict);
74111
}
75112

76113
BOOL CALLBACK translateWindow(HWND hwnd, LPARAM lParam) {

0 commit comments

Comments
 (0)