Skip to content

Commit

Permalink
remove support for WCHAR* strings from Translations.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 22, 2024
1 parent 2e188bc commit 93f6738
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ HMENU BuildMenuFromMenuDef(MenuDef* menuDef, HMENU menu, BuildMenuCtx* ctx) {
noTranslate |= (subMenuDef == menuDefDebug);
const char* title = md.title;
if (!noTranslate) {
title = trans::GetTranslationA(md.title);
title = trans::GetTranslation(md.title);
}

if (isSubMenu) {
Expand Down Expand Up @@ -1504,7 +1504,7 @@ void MenuUpdatePrintItem(MainWindow* win, HMENU menu, bool disableOnly = false)
if (def.idOrSubmenu != CmdPrint) {
continue;
}
str::Str printItem = trans::GetTranslationA(def.title);
str::Str printItem = trans::GetTranslation(def.title);
if (!filePrintAllowed) {
printItem = _TRA("&Print... (denied)");
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/SumatraPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4995,8 +4995,9 @@ void DeleteStaleFilesAsync() {
return;
}
TempStr dir = GetNotImportantDataDirTemp();
logf("DeleteStaleFiles: dir: '%s', gIsPreRelaseBuild: %d, ver: %s\n", (int)gIsPreReleaseBuild,
GetVerDirNameTemp(""));
TempStr ver = GetVerDirNameTemp("");
logf("DeleteStaleFiles: dir: '%s', gIsPreRelaseBuild: %d, ver: %s\n", dir, (int)gIsPreReleaseBuild,
ver);
RunAsync(DeleteStaleFiles, "DeleteStaleFilesThread");
}

Expand Down
5 changes: 3 additions & 2 deletions src/Toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ static TBBUTTON TbButtonFromButtonInfo(int i) {
info.iBitmap = (int)btInfo.bmpIndex;
info.fsState = TBSTATE_ENABLED;
info.fsStyle = TBSTYLE_BUTTON;
info.iString = (INT_PTR)trans::GetTranslation(btInfo.toolTip);
auto s = trans::GetTranslation(btInfo.toolTip);
info.iString = (INT_PTR)ToWStrTemp(s);
}
return info;
}
Expand All @@ -215,7 +216,7 @@ void UpdateToolbarButtonsToolTipsForWindow(MainWindow* win) {
AppendAccelKeyToMenuString(accelStr, accel);
}

const char* s = trans::GetTranslationA(tb.toolTip);
const char* s = trans::GetTranslation(tb.toolTip);
if (accelStr.size() > 0) {
accelStr[0] = '(';
accelStr.Append(")");
Expand Down
51 changes: 10 additions & 41 deletions src/Translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ namespace trans {

// defined in Trans*_txt.cpp
extern int gLangsCount;
extern const char* gLangNames;
extern const char* gLangNames;
extern const char* gLangCodes;
extern const LANGID* GetLangIds();
extern bool IsLangRtl(int langIdx);
extern const char** GetOriginalStrings();
} // namespace trans

namespace trans {
Expand All @@ -29,16 +28,13 @@ struct Translation {
// translation of str/origStr in gCurrLangCode
// index points inside allTranslations
u16 idxTrans = 0;
// translation in WCHAR*, points inside allTranslationsW
u16 idxTransW = 0;
};

struct TranslationCache {
// english string from translations.txt file
// we lazily match it to origStr
str::Str allStrings;
str::Str allTranslations;
str::WStr allTranslationsW;
// TODO: maybe also cache str::WStr allTranslationsW
// we currently return a temp converted string but there's a chance their
// lifetime could survive temp allocator lifetime
Expand Down Expand Up @@ -109,7 +105,6 @@ static void ParseTranslationsTxt(const ByteSlice& d, const char* langCode) {
// "missing" value in transIdx
gTranslationCache->allStrings.AppendChar(' ');
gTranslationCache->allTranslations.AppendChar(' ');
gTranslationCache->allTranslationsW.AppendChar(' ');
auto c = gTranslationCache;
c->nTranslations = nStrings;
c->translations = AllocArray<Translation>(c->nTranslations);
Expand Down Expand Up @@ -145,20 +140,14 @@ static void ParseTranslationsTxt(const ByteSlice& d, const char* langCode) {
translation.idxStr = (u16)idxStr;
UnescapeStringIntoStr(orig, c->allStrings);
char* toConvert = c->allStrings.LendData() + idxStr; // after insertion because could re-allocate
if (trans) {
size_t idxTrans = c->allTranslations.size();
CrashIf(idxTrans > 64 * 1024);
translation.idxTrans = (u16)idxTrans;
UnescapeStringIntoStr(trans, c->allTranslations);
toConvert = c->allTranslations.LendData() + idxTrans; // after insertion because could re-allocate
if (!trans) {
continue;
}
// if we don't have a translation, we cache WCHAR* version from original string
auto ws = ToWStrTemp(toConvert);
size_t idxTransW = c->allTranslationsW.size();
CrashIf(idxTransW > 64 * 1024);
translation.idxTransW = (u16)idxTransW;
size_t wsLen = str::Len(ws);
c->allTranslationsW.Append(ws, wsLen + 1);
size_t idxTrans = c->allTranslations.size();
CrashIf(idxTrans > 64 * 1024);
translation.idxTrans = (u16)idxTrans;
UnescapeStringIntoStr(trans, c->allTranslations);
toConvert = c->allTranslations.LendData() + idxTrans; // after insertion because could re-allocate
}
CrashIf(nTrans != c->nTranslations);
if (c->nUntranslated > 0 && !str::Eq(langCode, "en:")) {
Expand All @@ -182,7 +171,7 @@ static Translation* FindTranslation(const char* s) {
}

// don't free
const char* GetTranslationA(const char* s) {
const char* GetTranslation(const char* s) {
if (gCurrLangIdx == 0) {
return s;
}
Expand All @@ -196,22 +185,6 @@ const char* GetTranslationA(const char* s) {
return gTranslationCache->allTranslations.LendData() + idx;
}

// don't free
const WCHAR* GetTranslation(const char* s) {
Translation* trans = FindTranslation(s);
// we don't have a translation for this string
if (!trans || trans->idxTransW == 0) {
logf("GetTranslation: didn't find translation for '%s'\n", s);
// shouldn't happen
// ReportIf(true);
// it's a mem leak but the contract is that those strings
// are not freed and survive long enough they can't be temp strings
return ToWstr(s);
}
auto idx = trans->idxTransW;
return gTranslationCache->allTranslationsW.LendData() + idx;
}

int GetLangsCount() {
return gLangsCount;
}
Expand Down Expand Up @@ -289,10 +262,6 @@ void Destroy() {

} // namespace trans

const WCHAR* _TR(const char* s) {
return trans::GetTranslation(s);
}

const char* _TRA(const char* s) {
return trans::GetTranslationA(s);
return trans::GetTranslation(s);
}
5 changes: 1 addition & 4 deletions src/Translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const char* GetCurrentLangCode();
void SetCurrentLangByCode(const char* langCode);
const char* ValidateLangCode(const char* langCode);

const WCHAR* GetTranslation(const char* s);
const char* GetTranslationA(const char* s);
const char* GetTranslation(const char* s);
const char* GetLangCodeByIdx(int idx);
const char* GetLangNameByIdx(int idx);
bool IsCurrLangRtl();
Expand All @@ -20,8 +19,6 @@ void Destroy();

} // namespace trans

// _TR() marks strings that need to be translated
const WCHAR* _TR(const char* s);
// _TRA() is like _TR() but returns Utf8 version
const char* _TRA(const char* s);
#define _TR_TODO(quote) L##quote
Expand Down

0 comments on commit 93f6738

Please sign in to comment.