forked from ebidel/i18n-msg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n-msg.html
140 lines (108 loc) · 4.15 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
<link rel="import" href="i18n-msg-behavior.html">
<!-- Copyright Eric Bidelman <ebidel@gmail.com> -->
<!--
`<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>
### 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
}
},
/**
* 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;
},
_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 msg = this.locales[this.language][this.msgid];
if (msg && msg.message) {
this.msg = msg.message;
this.innerHTML = msg.message;
}
}
},
attached: function() {
var msg = this.getMsg();
if (msg) {
this.msg = msg;
this.innerHTML = msg;
}
}
});
})();
</script>