-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjsi18n.js
153 lines (138 loc) · 3.5 KB
/
jsi18n.js
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
/**
jsI18n library.
Simple client side internationalization.
Version: 1.1.1
Copyright (c) Daniel Abrahamsson 2010
**/
function JsI18n()
{
this.locale = "" //Current locale
this.locales = new Array() //Available locales
/*
Method for automatically detecting the language, does not work in every browser.
*/
this.detectLanguage = function(successCB, errorCB) {
// Phonegap browser detection
if (navigator.globalization !== null && navigator.globalization !== undefined) {
navigator.globalization.getPreferredLanguage(
function (language) { successCB(language); },
function (error) { errorCB(error); }
);
//Normal browser detection
} else {
if(window.navigator.language !== null && window.navigator.language !== undefined) {
successCB(window.navigator.language);
}
}
}
/*
Helper for translating a node
and all its child nodes.
*/
this.processNode = function(node)
{
if(node != undefined)
{
if(node.nodeType == 1) //Element node
{
var key = node.attributes["data-trans"]
if(key != null)
translateTag(node, key.nodeValue)
}
//Process child nodes
var children = node.childNodes
for(var i = 0; i < children.length; i++)
this.processNode(children[i])
}
/*
Translates tag contents and
attributes depending on the
value of key.
*/
function translateTag(node, key)
{
if(key.indexOf("=") == -1) //Simple key
translateNodeContent(node, key)
else //Attribute/key pairs
{
var parts = key.toLowerCase().split(";")
for(var i = 0; i < parts.length; i++)
{
var pair = parts[i].split("=")
var attr = pair[0].replace(/\s*(\w+)\s*/gi, "$1") //trim
var k = pair[1].replace(/\s*(\.+)\s*/gi, "$1")
if(attr == "html")
translateNodeContent(node, k)
else
translateNodeContent(node.attributes[attr], k)
}
}
}
/**
Replace the content of the given node
if there is a translation for the given key.
**/
function translateNodeContent(node, key)
{
var translation = jsI18n.t(key) //Hack, "this" does not work
if(node != null && translation != undefined)
{
if(node.nodeType == 1) //Element
{
try {
node.innerHTML = translation
} catch(e) {
node.text = translation
}
}
else if(node.nodeType == 2) //Attribute
node.value = translation
}
}
}
}
/*
Adds a locale to the list,
replacing the translations
if the locale is already defined.
*/
JsI18n.prototype.addLocale = function(locale, translations)
{
this.locales[locale.toString()] = translations
}
/*
Sets the locale to use when translating.
*/
JsI18n.prototype.setLocale = function(locale)
{
this.locale = locale
}
/*
Fetches the translation associated with the given key.
*/
JsI18n.prototype.t = function(key)
{
var translations = this.locales[this.locale]
if(translations != undefined)
{
return translations[key.toString()]
}
return undefined;
}
/*
Alias for JsI18n.t
*/
JsI18n.prototype.translate = function(key)
{
this.t(key)
}
/**
Replaces the contents of all tags
that have the data-trans attribute set.
**/
JsI18n.prototype.processPage = function()
{
this.processNode(document.getElementsByTagName("html")[0])
}
//Global
jsI18n = new JsI18n