forked from ebidel/i18n-msg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n-msg.html
203 lines (167 loc) · 6.45 KB
/
i18n-msg.html
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
<link rel="import" href="i18n-msg-behavior.html">
<!-- Copyright Eric Bidelman <[email protected]> -->
<!--
`<i18n-msg>` is a client-side solution for i18n message string replacement. It is modeled off the [Chrome extension i18n API](https://developer.chrome.com/extensions/i18n), which uses a `messages.json` file format.
The file contains localized strings for the language in question.
To set the language across your site, define `I18nMsg.lang` in your main page:
document.addEventListener('HTMLImportsLoaded', function() {
I18nMsg.lang = 'es';
// I18nMsg.url = 'locales'; // optionally use custom folder for locales.
Platform.performMicrotaskCheckpoint();
});
<b>Note:</b> this is done in `HTMLImportsLoaded` so `I18nMsg` will be defined by
the time you set `I18nMsg.lang`. This timing is necessary under the polyfill and is
<b>not needed if the browser supports HTML Imports</b>, natively.
<b>Note:</b> you must call `Platform.performMicrotaskCheckpoint()` whenever
making a change to the `I18nMsg` object. This is required due to the removal
of `Object.observe()`.
### Message files
Setting a language instructs `<i18n-msg>` to read message ids from a predefined
file on your server. For example, `I18nMsg.lang = 'es'` will use `locales/es.json`.
`I18nMsg.lang = 'fr'` would use message strings from `locales/fr.json`.
The directory name can be configured by using the `messagesUrl` property, but the file
cannot. `<i18n-msg>` will always attempt to use messages from `<messagesUrl>/<LANG_YOU_SET>.json`.
You can also set and/or change this location globally, at any time, by setting `I18nMsg.url`.
<b>Example</b> - using a custom locales folder, setting it globally for all `<i18n-msg>` instances:
I18nMsg.url = 'locales';
<b>Example</b> - set locales folder path on individual element:
<i18n-msg messages-url="path/to/locales">fallback text</i18n-msg>
<b>Note:</b> message files are read once and reused for all instances of `<i18n-msg>`.
### Fallback text
If an appropriate message file can't be found, the `textContent` of the element is used as fallback text.
<i18n-msg msgid="unknownmsgid">fallback text</i18n-msg>
### Placeholders
It's possible to insert text within the message which requires no translation (e.g: names, dates, numbers).
To make available the use of placeholders the message must contain placeholders in Chrome.i18n format ($name$) whenever a parameter
should be used, and to use these add the attribute "placeholders" having value array. Example:
"error": {
"message": "Error: $details$",
"description": "Generic error template. Expects error parameter to be passed in.",
"placeholders": {
"details": {
"content": "$1",
"example": "Failed to fetch RSS feed."
}
}
}
<i18-msg msgid="error" placeholders='["Failed to fetch data."]'></i18n-msg>
It's also possible to use {{}} and [[]] within the placeholders.
### Full example
<html lang="es">
...
<body>
<p>"Days" in Spanish is <i18n-msg msgid="days">days</i18n-msg>!</p>
<script>
I18nMsg.lang = document.documentElement.lang || 'en';
</script>
</body>
</html>
### Getting a message:
// No argument returns the instance's message:
document.querySelector('i18n-msg').getMsg();
// Get a message by an id:
document.querySelector('i18n-msg').getMsg('days');
@demo
-->
<dom-module id="i18n-msg"></dom-module>
<script>
(function () {
Polymer({
is: 'i18n-msg',
behaviors: [i18nMsgBehavior],
properties: {
/**
* The message id (key) for this message.
*/
msgid: {
type: String,
value: null,
observer: '_updateMessages'
},
/**
* The message in the current locale
*/
msg: {
type: String,
value: null,
notify: true
},
/**
* Placeholders used to insert within the message if it contains replacement
* patterns to switch with. To use this feature you must include the "placeholders"
* attribute and use '' for the array and "" for each element.
* Example: placeholders = '["John Doe","Seattle"]'
*/
placeholders: {
type: Array,
value: function() {
return [];
},
observer: '_placeholdersChanged'
},
},
/**
* Returns a message in the current locale (set by `window.I18nMsg.lang`).
* @method getMsg
* @param {string=} opt_msgId Optional message id to lookup. If left off,
* the instance's `msgid` property is used.
* @return {string|null} Translated message or `null` if not found.
*/
getMsg: function(opt_msgId) {
var msgId = opt_msgId || this.msgid;
var msg = this._parentGetMsg(msgId);
return msg;
},
_placeholdersChanged: function() {
if (this.language && this.locales[this.language] && this.placeholders) {
this._updateMessages(this);
}
},
_getInterpolatedMsg: function() {
if (this.locales && this.locales[this.language]) {
var msg = this.locales[this.language][this.msgid];
if (msg && msg.message) {
var message = msg.message;
var placeholders = msg.placeholders;
var phData = this.placeholders;
if ((placeholders) && (phData)) {
var content, pos;
for (var p in placeholders) {
content = placeholders[p].content;
if (content[0] == '$') {
pos = content.substring(1, content.length);
if (!isNaN(pos)) {
content = (phData.length < pos) ? '' : phData[pos - 1];
}
}
message = message.split('$' + p + '$').join(content);
}
}
return message;
}
}
return null;
},
_updateMessages: function() {
if (this.locales && this.locales[this.language]) {
if (!this.msgid in this.locales[this.language]) {
console.warn(this.localName + ': "' + this.msgid + '" message id was not found in ' + url);
return;
}
var message = this._getInterpolatedMsg();
if(message) {
this.msg = message;
this.innerHTML = message;
}
}
},
attached: function() {
var msg = this._getInterpolatedMsg();
if (msg) {
this.msg = msg;
this.innerHTML = msg;
}
}
});
})();
</script>