FullscreenUI: Make localizable

This commit is contained in:
Stenzek 2023-08-27 02:22:15 +10:00 committed by Connor McLaughlin
parent f8e1b9b11b
commit d00eb38bbc
3 changed files with 2268 additions and 1000 deletions

View File

@ -1075,12 +1075,11 @@ std::time_t GameList::GetCachedPlayedTimeForSerial(const std::string& serial)
std::string GameList::FormatTimestamp(std::time_t timestamp) std::string GameList::FormatTimestamp(std::time_t timestamp)
{ {
// TODO: All these strings should be translateable.
std::string ret; std::string ret;
if (timestamp == 0) if (timestamp == 0)
{ {
ret = "Never"; ret = TRANSLATE_STR("GameList", "Never");
} }
else else
{ {
@ -1097,12 +1096,12 @@ std::string GameList::FormatTimestamp(std::time_t timestamp)
if (ctime.tm_year == ttime.tm_year && ctime.tm_yday == ttime.tm_yday) if (ctime.tm_year == ttime.tm_year && ctime.tm_yday == ttime.tm_yday)
{ {
ret = "Today"; ret = TRANSLATE_STR("GameList", "Today");
} }
else if ((ctime.tm_year == ttime.tm_year && ctime.tm_yday == (ttime.tm_yday + 1)) || else if ((ctime.tm_year == ttime.tm_year && ctime.tm_yday == (ttime.tm_yday + 1)) ||
(ctime.tm_yday == 0 && (ctime.tm_year - 1) == ttime.tm_year)) (ctime.tm_yday == 0 && (ctime.tm_year - 1) == ttime.tm_year))
{ {
ret = "Yesterday"; ret = TRANSLATE_STR("GameList", "Yesterday");
} }
else else
{ {
@ -1125,22 +1124,22 @@ std::string GameList::FormatTimespan(std::time_t timespan, bool long_format)
if (!long_format) if (!long_format)
{ {
if (hours >= 100) if (hours >= 100)
ret = fmt::format("{}h {}m", hours, minutes); ret = fmt::format(TRANSLATE_FS("GameList", "{}h {}m"), hours, minutes);
else if (hours > 0) else if (hours > 0)
ret = fmt::format("{}h {}m {}s", hours, minutes, seconds); ret = fmt::format(TRANSLATE_FS("GameList", "{}h {}m {}s"), hours, minutes, seconds);
else if (minutes > 0) else if (minutes > 0)
ret = fmt::format("{}m {}s", minutes, seconds); ret = fmt::format(TRANSLATE_FS("GameList", "{}m {}s"), minutes, seconds);
else if (seconds > 0) else if (seconds > 0)
ret = fmt::format("{}s", seconds); ret = fmt::format(TRANSLATE_FS("GameList", "{}s"), seconds);
else else
ret = "None"; ret = "None";
} }
else else
{ {
if (hours > 0) if (hours > 0)
ret = fmt::format("{} hours", hours); ret = fmt::format(TRANSLATE_FS("GameList", "{} hours"), hours);
else else
ret = fmt::format("{} minutes", minutes); ret = fmt::format(TRANSLATE_FS("GameList", "{} minutes"), minutes);
} }
return ret; return ret;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,73 @@
import os
START_IDENT = "// TRANSLATION-STRING-AREA-BEGIN"
END_IDENT = "// TRANSLATION-STRING-AREA-END"
src_file = os.path.join(os.path.dirname(__file__), "..", "pcsx2", "ImGui", "FullscreenUI.cpp")
with open(src_file, "r") as f:
full_source = f.read()
strings = []
for token in ["FSUI_STR", "FSUI_CSTR", "FSUI_FSTR", "FSUI_NSTR", "FSUI_ICONSTR", "FSUI_ICONSTR_S"]:
token_len = len(token)
last_pos = 0
while True:
last_pos = full_source.find(token, last_pos)
if last_pos < 0:
break
if last_pos >= 8 and full_source[last_pos - 8:last_pos] == "#define ":
last_pos += len(token)
continue
if full_source[last_pos + token_len] == '(':
start_pos = last_pos + token_len + 1
end_pos = full_source.find("\")", start_pos)
s = full_source[start_pos:end_pos+1]
# remove "
pos = s.find('"')
new_s = ""
while pos >= 0:
if pos == 0 or s[pos - 1] != '\\':
epos = pos
while True:
epos = s.find('"', epos + 1)
assert epos > pos
if s[epos - 1] == '\\':
continue
else:
break
assert epos > pos
new_s += s[pos+1:epos]
cpos = s.find(',', epos + 1)
pos = s.find('"', epos + 1)
if cpos >= 0 and pos >= 0 and cpos < pos:
break
else:
pos = s.find('"', pos + 1)
assert len(new_s) > 0
#assert (end_pos - start_pos) < 300
#if (end_pos - start_pos) >= 300:
# print("WARNING: Long string")
# print(new_s)
if new_s not in strings:
strings.append(new_s)
last_pos += len(token)
print(f"Found {len(strings)} unique strings.")
start = full_source.find(START_IDENT)
end = full_source.find(END_IDENT)
assert start >= 0 and end > start
new_area = ""
for string in list(strings):
new_area += f"TRANSLATE_NOOP(\"FullscreenUI\", \"{string}\");\n"
full_source = full_source[:start+len(START_IDENT)+1] + new_area + full_source[end:]
with open(src_file, "w") as f:
f.write(full_source)