-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGameLocale.lua
151 lines (118 loc) · 3.99 KB
/
GameLocale.lua
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
local GameLocale = { version = '0.8.1' }
local languageGroupPath = '/language'
local languageInterfaceVarName = 'OnScreen'
local languageSubtitleVarName = 'Subtitles'
local languageVoiceVarName = 'VoiceOver'
local defaultLanguage = 'en-us'
local currentLanguage = {
[languageInterfaceVarName] = defaultLanguage,
[languageSubtitleVarName] = defaultLanguage,
[languageVoiceVarName] = defaultLanguage,
}
local translationDir = 'data/lang'
local translationData = {}
---@type inkTextWidget
local dummyTextWidget
local function getLanguageFromSettings(languageVarName)
return Game.NameToString(Game.GetSettingsSystem():GetVar(languageGroupPath, languageVarName):GetValue())
end
local function getCurrentLanguage(languageVarName)
return currentLanguage[languageVarName] or defaultLanguage
end
local function updateCurrentLanguage(languageVarName)
currentLanguage[languageVarName] = getLanguageFromSettings(languageVarName)
end
local function loadTranslationData(targetLanguage)
local chunk = loadfile(translationDir .. '/' .. targetLanguage)
if chunk then
translationData[targetLanguage] = chunk()
else
translationData[targetLanguage] = {}
end
if not translationData[defaultLanguage] then
chunk = loadfile(translationDir .. '/' .. defaultLanguage)
translationData[defaultLanguage] = chunk and chunk() or {}
end
end
local function refreshTranslationData()
for translationLanguage, _ in pairs(translationData) do
if translationLanguage ~= defaultLanguage then
local isUsed = false
for _, activeLanguage in pairs(currentLanguage) do
if translationLanguage == activeLanguage then
isUsed = true
break
end
end
if not isUsed then
translationData[translationLanguage] = nil
end
end
end
end
local function getLocalizedText(key, targetLanguage)
if translationData[targetLanguage] == nil then
loadTranslationData(targetLanguage)
end
if translationData[targetLanguage][key] == nil then
local translation = Game.GetLocalizedText(key)
if translation ~= '' and translation ~= key then
translationData[targetLanguage][key] = translation
elseif targetLanguage ~= defaultLanguage then
translationData[targetLanguage][key] = getLocalizedText(key, defaultLanguage)
else
translationData[targetLanguage][key] = key
end
end
return translationData[targetLanguage][key]
end
local function getLocalizedDate(timestamp)
if not dummyTextWidget then
dummyTextWidget = inkTextWidget.new()
end
dummyTextWidget:SetDateTimeByTimestamp(timestamp)
return dummyTextWidget.text
end
function GameLocale.Initialize(translationDataDir)
if translationDataDir then
translationDir = translationDataDir
end
languageGroupPath = CName.new(languageGroupPath)
updateCurrentLanguage(languageInterfaceVarName)
updateCurrentLanguage(languageSubtitleVarName)
updateCurrentLanguage(languageVoiceVarName)
Observe('SettingsMainGameController', 'OnVarModified', function(_, groupPath, varName, _, reason)
if groupPath == languageGroupPath and reason == InGameConfigChangeReason.Accepted then
updateCurrentLanguage(Game.NameToString(varName))
refreshTranslationData()
end
end)
end
function GameLocale.GetInterfaceLanguage()
return getCurrentLanguage(languageInterfaceVarName)
end
function GameLocale.GetSubtitleLanguage()
return getCurrentLanguage(languageSubtitleVarName)
end
function GameLocale.GetAudioLanguage()
return getCurrentLanguage(languageVoiceVarName)
end
function GameLocale.Text(key)
return getLocalizedText(key, currentLanguage[languageInterfaceVarName])
end
function GameLocale.Subtitle(key)
return getLocalizedText(key, currentLanguage[languageSubtitleVarName])
end
function GameLocale.ActionHold(action)
return ('(%s) %s'):format(
getLocalizedText('Gameplay-Devices-Interactions-Helpers-Hold', currentLanguage[languageInterfaceVarName]),
getLocalizedText(action, currentLanguage[languageInterfaceVarName])
)
end
function GameLocale.Date(timestamp)
return getLocalizedDate(timestamp)
end
--function GameLocale.GetTranslator()
-- return getLocalizedText
--end
return GameLocale