-
Notifications
You must be signed in to change notification settings - Fork 3
/
Localizer.cfc
executable file
·355 lines (328 loc) · 14.9 KB
/
Localizer.cfc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<cfcomponent output="false">
<cffunction name="init" access="public" output="false" returntype="any">
<cfset this.version = "0.9.3,1.0,1.1">
<cfreturn this />
</cffunction>
<cffunction name="l" returntype="any" output="false" access="public" hint="Shortcut method to 'localize'">
<cfargument name="text" type="string" required="true" hint="Text to localize" />
<cfreturn localize(arguments.text) />
</cffunction>
<cffunction name="localize" returnType="any" output="false" access="public">
<cfargument name="text" type="string" required="true" hint="Text to localize" />
<cfscript>
var loc = {};
arguments.source = $captureTemplateAndLineNumber();
loc.result = "";
loc.textContainsDynamicText = (arguments.text CONTAINS "{" AND arguments.text CONTAINS "}");
// get the text from the repo for the local specified
loc.localizedText = $getLocalizedText();
if (loc.textContainsDynamicText)
{
loc.textBetweenDynamicText = REMatch("{(.*?)}", arguments.text);
loc.iEnd = ArrayLen(loc.textBetweenDynamicText);
for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
arguments.text = Replace(arguments.text, loc.textBetweenDynamicText[loc.i], "{variable}", "all");
}
// Return the localized text in the current locale
loc.translation = $findLocalizedText(text=arguments.text, struct=loc.localizedText, source=arguments.source);
if (ListFindNoCase("design,development", get("environment")))
{
if (Len(loc.translation))
{
loc.result = loc.translation;
}
else
{
$writeTextIntoLocalizationRepository(text=arguments.text, source=arguments.source);
loc.result = arguments.text;
}
}
else if (Len(loc.translation))
{
loc.result = loc.translation;
}
if (loc.textContainsDynamicText)
{
// Go through the array and replace the "{variable}" back with the respective values
loc.iEnd = ArrayLen(loc.textBetweenDynamicText);
for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
loc.result = Replace(loc.result, "{variable}", loc.textBetweenDynamicText[loc.i]);
// Remove all "{}"
loc.result = ReplaceList(loc.result, "{,}", "");
}
</cfscript>
<cfreturn loc.result>
</cffunction>
<cffunction name="getLocaleCode" returnType="string" hint="Since ColdFusion has several names for locales and not the standard 5 letters code, this function will enforce the return of the locale in the 5 letter code">
<cfscript>
var loc = {};
loc.currentLocale = getLocale();
// translate coldfusion locales to their corresponding codes
if (Len(loc.currentLocale) gt 5)
{
switch (loc.currentLocale)
{
case "Chinese (China)": { loc.currentLocale = "zh_CN"; break; }
case "Chinese (Hong Kong)": { loc.currentLocale = "zh_HK"; break; }
case "Chinese (Taiwan)": { loc.currentLocale = "zh_TW"; break; }
case "Dutch (Belgian)": { loc.currentLocale = "nl_BE"; break; }
case "Dutch (Standard)": { loc.currentLocale = "nl_NL"; break; }
case "English (Australian)": { loc.currentLocale = "en_AU"; break; }
case "English (Canadian)": { loc.currentLocale = "en_CA"; break; }
case "English (New Zealand)": { loc.currentLocale = "en_NZ"; break; }
case "English (UK)": { loc.currentLocale = "en_GB"; break; }
case "English (US)": { loc.currentLocale = "en_US"; break; }
case "French (Belgian)": { loc.currentLocale = "fr_BE"; break; }
case "French (Canadian)": { loc.currentLocale = "fr_CA"; break; }
case "French (Standard)": { loc.currentLocale = "fr_FR"; break; }
case "French (Swiss)": { loc.currentLocale = "fr_CH"; break; }
case "German (Austrian)": { loc.currentLocale = "de_AT"; break; }
case "German (Standard)": { loc.currentLocale = "de_DE"; break; }
case "German (Swiss)": { loc.currentLocale = "de_CH"; break; }
case "Italian (Standard)": { loc.currentLocale = "it_IT"; break; }
case "Italian (Swiss)": { loc.currentLocale = "it_CH"; break; }
case "Japanese": { loc.currentLocale = "ja_JP"; break; }
case "Korean": { loc.currentLocale = "ko_KR"; break; }
case "Norwegian (Bokmal)": { loc.currentLocale = "no_NO"; break; }
case "Norwegian (Nynorsk)": { loc.currentLocale = "no_NO"; break; }
case "Portuguese (Brazilian)": { loc.currentLocale = "pt_BR"; break; }
case "Portuguese (Standard)": { loc.currentLocale = "pt_PT"; break; }
case "Spanish (Mexican)": { loc.currentLocale = "es_MX"; break; }
case "Spanish (Modern)": { loc.currentLocale = "es_US"; break; }
case "Spanish (Standard)": { loc.currentLocale = "es_ES"; break; }
case "Swedish": { loc.currentLocale = "sv_SE"; break; }
}
}
</cfscript>
<cfreturn loc.currentLocale>
</cffunction>
<cffunction name="$$populateRepository" returntype="void" output="false" access="public">
<cfargument name="folderList" type="string" required="false" default="controllers,models,views" />
<cfscript>
var loc = {};
if (!ListFindNoCase("design,development", get("environment")))
$throw(type="Wheels.Localizer.AccessDenied", message="The method `$$populateRepository` may only be run is design or development modes.");
loc.iEnd = ListLen(arguments.folderList);
for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
{
// get our directory from the list
loc.relativeDir = ListGetAt(arguments.folderList, loc.i);
// decide how we should filter the files
if (ListFindNoCase("controllers,models", loc.relativeDir))
loc.filter = "*.cfc";
else
loc.filter = "*.cfm";
loc.files = $directory(action="list", type="file", recurse=true, filter=loc.filter, listInfo="name", directory=ExpandPath(loc.relativeDir));
loc.xEnd = loc.files.RecordCount;
for (loc.x = 1; loc.x lte loc.xEnd; loc.x++)
{
loc.file = loc.relativeDir & "/" & loc.files.name[loc.x];
loc.fileReader = CreateObject("java", "java.io.FileReader").init(ExpandPath(loc.file));
loc.lineReader = CreateObject("java","java.io.LineNumberReader").init(loc.fileReader);
loc.line = loc.lineReader.readLine();
loc.lineCount = 1;
while (StructKeyExists(loc, "line"))
{
loc.matches = REMatch("([^a-zA-Z0-9]l|[^a-zA-Z0-9]localize)[[:space:]]?(\([[:space:]]?['""](.*?)['""][[:space:]]?)\)", loc.line);
loc.mEnd = ArrayLen(loc.matches);
for (loc.m = 1; loc.m lte loc.mEnd; loc.m++)
{
// for each match we have for the line, write it to the repo
loc.matches[loc.m] = REReplace(loc.matches[loc.m], "([^a-zA-Z0-9]l|[^a-zA-Z0-9]localize)[[:space:]]?(\([[:space:]]?['""])", "", "all");
loc.matches[loc.m] = REReplace(loc.matches[loc.m], "(['""][[:space:]]?)\)", "", "all");
loc.source = {};
loc.source.template = loc.file;
loc.source.line = loc.lineCount;
$writeTextIntoLocalizationRepository(text=loc.matches[loc.m], source=loc.source);
}
// do something here with the data in variable line
loc.line = loc.lineReader.readLine();
loc.lineCount++;
}
}
}
</cfscript>
</cffunction>
<cffunction name="$findLocalizedText" returntype="string" output="false" access="public">
<cfargument name="text" type="string" required="true" />
<cfargument name="struct" type="struct" required="true" />
<cfargument name="source" type="struct" required="true" />
<cfscript>
var loc = {};
loc.returnValue = "";
if (StructKeyExists(arguments.struct, arguments.source.template))
if (StructKeyExists(arguments.struct[arguments.source.template], arguments.source.line))
if (StructKeyExists(arguments.struct[arguments.source.template][arguments.source.line], arguments.text))
loc.returnValue = arguments.struct[arguments.source.template][arguments.source.line][arguments.text];
</cfscript>
<cfreturn loc.returnValue />
</cffunction>
<cffunction name="$getLocalizedText" returnType="struct" output="false" access="public">
<cfargument name="fromRepository" type="boolean" required="false" default="false" />
<cfscript>
var loc = {};
loc.texts = {}; // initialize this value in case we write a file
if (!arguments.fromRepository)
loc.currentLocale = getLocaleCode();
else
loc.currentLocale = "repository";
loc.includePath = LCase("locales/#loc.currentLocale#.cfm");
loc.filePath = LCase("plugins/localizer/locales/#loc.currentLocale#.cfm");
if (FileExists(ExpandPath(loc.filePath)))
loc.texts = $includeRepository(loc.includePath);
else
$file(action="write", file=ExpandPath(loc.filePath), output="");
</cfscript>
<cfreturn loc.texts>
</cffunction>
<cffunction name="$writeTextIntoLocalizationRepository" returnType="void" output="false" access="public" hint="Writes the text to the localization repository">
<cfargument name="text" type="string" required="true" hint="Text to write" />
<cfargument name="source" type="struct" required="true" hint="Source of the text to write" />
<cfset var loc = {} />
<!---
save texts with a greater nesting to make sure same texts with different casings in different files get saved
we might have some dups but it will save on the head scratching wondering how something became lower case
i think this helps make the repository file more self descriptive and the struct if you dump it out
more nesting is also faster as there are less items per structure
--->
<cfsavecontent variable="loc.text"><cfoutput>[cfset loc["#arguments.source.template#"]["#arguments.source.line#"]["#arguments.text#"] = "#arguments.text#"]]</cfoutput></cfsavecontent>
<cfscript>
if (!StructKeyExists(request, "localizer") or !StructKeyExists(request.localizer, "writes"))
request.localizer.writes = {};
loc.repo = $getLocalizedText(fromRepository=true);
// Check first if the variable is written already
loc.repoString = $findLocalizedText(text=arguments.text, struct=loc.repo, source=arguments.source);
loc.inRequest = $findLocalizedText(text=arguments.text, struct=request.localizer.writes, source=arguments.source);
if (!Len(loc.repoString) && !Len(loc.inRequest))
{
// transform file output
loc.text = ReplaceList(loc.text, "[cfset,]]", "<cfset, />");
$file(action="append", file=ExpandPath("plugins/localizer/locales/repository.cfm"), output=loc.text);
// when we have template caching turned on in coldfusion, the first version of the template is the one that will be retrieved for the rest of the request, not good
request.localizer.writes[arguments.source.template][arguments.source.line][arguments.text] = arguments.text;
}
</cfscript>
</cffunction>
<cffunction name="$captureTemplateAndLineNumber" output="false" access="public" returntype="struct">
<cfscript>
var loc = {};
loc.ret = $getCallingTemplateInfo();
// change template name from full to relative path
loc.ret.template = ListChangeDelims(ReplaceNoCase(loc.ret.template, ExpandPath(application.wheels.webpath), ""), "/", "\/"); // instead of RemoveChars, lets replace the path since replace will cause less issues
</cfscript>
<cfreturn loc.ret />
</cffunction>
<cffunction name="$getCallingTemplateInfo" output="false" access="public" returntype="struct">
<cfscript>
var loc = {};
loc.returnValue = { template = "unknown", line = "unknown"};
loc.stackTrace = CreateObject("java", "java.lang.Throwable").getStackTrace();
loc.iEnd = ArrayLen(loc.stackTrace);
for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
{
loc.fileName = loc.stackTrace[loc.i].getFileName();
if (StructKeyExists(loc, "fileName") && !FindNoCase(".java", loc.fileName) && !FindNoCase("Localizer.cfc", loc.fileName) && !FindNoCase("<generated>", loc.fileName))
{
loc.returnValue.template = loc.fileName;
loc.returnValue.line = loc.stackTrace[loc.i].getLineNumber();
break;
}
}
</cfscript>
<cfreturn loc.returnValue />
</cffunction>
<cffunction name="$includeRepository" output="false" access="public" returntype="struct">
<cfargument name="template" type="string" required="true" />
<cfset var loc = {} />
<cfif !StructKeyExists(request, "localizer") or !StructKeyExists(request.localizer, "cache")>
<cfset request.localizer.cache = {} />
</cfif>
<cfif StructKeyExists(request.localizer.cache, arguments.template)>
<cfreturn request.localizer.cache[arguments.template] />
</cfif>
<cfinclude template="#arguments.template#" />
<cfset request.localizer.cache[arguments.template] = Duplicate(loc) />
<cfreturn loc />
</cffunction>
<cffunction name="distanceOfTimeInWords" returntype="string" access="public" output="false">
<cfargument name="fromTime" type="date" required="true" hint="Date to compare from.">
<cfargument name="toTime" type="date" required="true" hint="Date to compare to.">
<cfargument name="includeSeconds" type="boolean" required="false" hint="Whether or not to include the number of seconds in the returned string.">
<cfscript>
var loc = {};
$args(name="distanceOfTimeInWords", args=arguments);
loc.minuteDiff = DateDiff("n", arguments.fromTime, arguments.toTime);
loc.secondDiff = DateDiff("s", arguments.fromTime, arguments.toTime);
loc.hours = 0;
loc.days = 0;
loc.returnValue = "";
if (loc.minuteDiff <= 1)
{
if (loc.secondDiff < 60)
loc.returnValue = l("less than a minute");
else
loc.returnValue = l("1 minute");
if (arguments.includeSeconds)
{
if (loc.secondDiff < 5)
loc.returnValue = l("less than 5 seconds");
else if (loc.secondDiff < 10)
loc.returnValue = l("less than 10 seconds");
else if (loc.secondDiff < 20)
loc.returnValue = l("less than 20 seconds");
else if (loc.secondDiff < 40)
loc.returnValue = l("half a minute");
}
}
else if (loc.minuteDiff < 45)
{
loc.returnValue = l("{#loc.minuteDiff#} minutes");
}
else if (loc.minuteDiff < 90)
{
loc.returnValue = l("about 1 hour");
}
else if (loc.minuteDiff < 1440)
{
loc.hours = Ceiling(loc.minuteDiff/60);
loc.returnValue = l("about {#loc.hours#} hours");
}
else if (loc.minuteDiff < 2880)
{
loc.returnValue = l("1 day");
}
else if (loc.minuteDiff < 43200)
{
loc.days = Int(loc.minuteDiff/1440);
loc.returnValue = l("{#loc.days#} days");
}
else if (loc.minuteDiff < 86400)
{
loc.returnValue = l("about 1 month");
}
else if (loc.minuteDiff < 525600)
{
loc.months = Int(loc.minuteDiff/43200);
loc.returnValue = l("{#loc.months#} months");
}
else if (loc.minuteDiff < 657000)
{
loc.returnValue = l("about 1 year");
}
else if (loc.minuteDiff < 919800)
{
loc.returnValue = l("over 1 year");
}
else if (loc.minuteDiff < 1051200)
{
loc.returnValue = l("almost 2 years");
}
else if (loc.minuteDiff >= 1051200)
{
loc.years = Int(loc.minuteDiff/525600);
loc.returnValue = l("over {#loc.years#} years");
}
</cfscript>
<cfreturn loc.returnValue>
</cffunction>
</cfcomponent>