-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_CLocalizer.ahk
186 lines (173 loc) · 5.07 KB
/
class_CLocalizer.ahk
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
#include <class_CIniFile>
class CLocalizer
{
; The directory where all language files are stored
dir := ""
; Current language id
langId := ""
; Current language name
langName := ""
; Current localization strings
strings := []
; The file extension
FileExtenstion := "ini"
; Language id-name list
LanguageList := {}
__New(dir)
{
this.dir := dir
}
;
; Function: GetLanguageFileName
; Description:
; Cteates the language file name for the supplied language id.
; Syntax: CLocalizer.GetLanguageFileName(langId)
; Parameters:
; langId - The language id like "en-US".
; Return Value:
; Returns the language file name for the supplied language id.
;
GetLanguageFileName(langId)
{
fname := this.dir "\" langId "." this.FileExtenstion
return fname
}
;
; Function: ReadLanguageList
; Description:
; Reads language ids and names into LanguageList property of CLocalizer.
; Syntax: CLocalizer.ReadLanguageList()
; Return Value:
; Returns the language list.
;
; USES: CIniFile.Read.
; SIDE-EFFECT: Updates LanguageList.
ReadLanguageList()
{
result := {}
pattern := this.dir "\*." this.FileExtenstion
Loop, %pattern%
{
; OutputDebug CLocalizer.ReadLanguageNames: Looping thru %A_LoopFileFullPath%
SplitPath, A_LoopFileFullPath,,,,langId
ini := new CIniFile(A_LoopFileFullPath)
langName := ini.Read(langId, "_langName")
; OutputDebug CLocalizer.ReadLanguageNames: got langName = %langName%
if(langName <> ini.defaultValue)
result[langId] := langName
}
this.LanguageList := result
return result
}
;
; Function: CreateLanguageFile
; Description:
; Creates a language file for the specified language ID and name and writes supplied strings into it.
; Syntax: CLocalizer.CreateLanguageFile(langId, langName, strings)
; Parameters:
; langId - The language id like "en-US".
; langName - The language display name (spelled in the language itself).
; strings - An associative array of string IDs with the respective strings.
; Remarks:
; Sets ErrorLevel to 1 if an attempt to create the file failed.
;
CreateLanguageFile(langId, langName, strings)
{
; OutputDebug In CLocalizer.CreateLanguageFile(%langId%, %langName%, %strings%)
fname := this.GetLanguageFileName(langId)
; OutputDebug `tChecking file for existence = %fname%
ini := new CIniFile(fname)
if(!ini.Exists(fname))
{
if(!ini.CreateIfNotExists())
{
ErrorLevel := 1
return
}
}
strings["_langName"] := langName
strings["_langId"] := langId
ini.WriteSection(langId, strings)
}
;
; Function: LoadStrings
; Description:
; Loads strings for the specified language ID.
; Syntax: CLocalizer.LoadStrings(langId, defaultStrings)
; Parameters:
; langId - The language id like "en-US".
; defaultStrings - (Optional) An associative array of string IDs with the respective strings
; that will be used if some strings were not localised.
; Remarks:
; If any string ID is not localized, the respective default string remains as is.
; ErrorLevel is set to 1 on failure to write default strings into a new language file upon the first start.
;
LoadStrings(langId, defaultStrings = "")
{
; OutputDebug Loading strings for langId = %langId%
success := true
if(!IsObject(defaultStrings))
defaultStrings := {}
this.strings := defaultStrings
this.langId := langId
this.fileName := this.GetLanguageFileName(this.langId)
ini := new CIniFile(this.fileName)
if(!ini.GetSectionCount()) ; Also checks for existence
{
this.CreateLanguageFile(defaultStrings._langId, defaultStrings._langName, defaultStrings)
if ErrorLevel
success := false
}
; OutputDebug LoadStrings before read ErrorLevel = %ErrorLevel%
strings := ini.ReadSection(this.langId)
; OutputDebug LoadStrings after read ErrorLevel = %ErrorLevel%
for k, s in strings
{
; OutputDebug Trying string %k% = %s%
; Disallow any custom strings
if(defaultStrings.haskey(k))
{
this.strings[k] := s
; OutputDebug Loaded string %k% = %s%
}
}
if(!success)
ErrorLevel := 1
else if(ErrorLevel)
ErrorLevel := 2
; OutputDebug LoadStrings result ErrorLevel = %ErrorLevel%
}
;
; Function: GetLangIdByName
; Description:
; Gets the language ID for the specified language name.
; Syntax: CLocalizer.GetLangIdByName(langName)
; Parameters:
; langName - The language display name (spelled in the language itself).
; Return Value:
; Returns the language ID like "en-US".
;
; USES: ReadLanguageList to update the language list.
; SIDE-EFFECT: Updates LanguageList.
GetLangIdByName(langName)
{
this.ReadLanguageList()
result := ""
for langId, exlangName in this.LanguageList
{
; OutputDebug comparing %langName%, %exlangName%
if(String_IsEqual(langName, exlangName))
{
result := langId
break
}
}
; OutputDebug return %langId%
return langId
}
GetLanguageList()
{
this.ReadLanguageList()
return this.LanguageList
}
}