-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostabee-element-mixin.html
168 lines (154 loc) · 5.17 KB
/
hostabee-element-mixin.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
<!--
@license
Copyright (c) 2019 Hostabee SAS.
This program is available under Apache License Version 2.0.
-->
<link rel="import" href="../polymer/lib/legacy/class.html">
<link rel="import" href="../polymer/lib/utils/mixin.html">
<link rel="import" href="../app-localize-behavior/app-localize-behavior.html">
<script>
(function() {
'user strict';
/**
* @namespace Hostabee
*/
window.Hostabee = window.Hostabee || {};
/**
* @mixinFunction
* @polymer
* @memberof Hostabee
*/
const ElementMixin = (superClass) => class extends Polymer.mixinBehaviors(
[Polymer.AppLocalizeBehavior], superClass) {
static get is() {
return 'hostabee-element';
}
static get properties() {
return {
/**
* The language used by the element. It defines which file to use for
* translation. The file format is the same specified by the Polymer
* [AppLocalizeBehavior](https://www.webcomponents.org/element/PolymerElements/app-localize-behavior/v2.0.2/behaviors/Polymer.AppLocalizeBehavior)
* behavior. If the language defined is regional and there is no
* associted file found, the element will try to load a more generic
* one. For example, the navigator language is "en-US" and there is no
* file "en-US.json" available. The element will try to load "en.json".
*
* @type {String}
* @default window.navigator.language
*/
language: {
type: String,
value: window.navigator.language,
},
/**
* Path to an external file containing locales. If the value is set, the
* target file will be used instead of the default one. The target file
* must be in JSON format. The file will be searched from the root of
* your app and must contain translations for all supported language.
*
* For example, if you app supports English and French the file looks like:
*
* ```json
* {
* "en": {
* "greetings": "Hello!"
* },
* "fr": {
* "greetings": "Bonjour !"
* }
* }
* ```
*
* For more detail about the file format, please read the
* [AppLocalizeBehavior](https://github.com/PolymerElements/app-localize-behavior)
* documentation.
*
* @type {String}
*/
localesFile: {
type: String,
observer: '_localesFileChanged',
},
/**
* Path of the folder where the locales files are. All supported
* language has it own file. For example, if you app supports English
* and French and the locales files folder is "locales", the "locales"
* folder contains 2 files:
*
* _en.json_
*
* ```json
* {
* "greetings": "Hello!"
* }
* ```
*
* and _fr.json_
*
* ```json
* {
* "greetings": "Bonjour !"
* }
* ```
*
* @type {String}
*/
localesFolder: {
type: String,
value: '.',
},
};
}
static get observers() {
return [
'_onLanguageChanged(language)',
];
}
/**
* Called every time the element is inserted into the DOM. Useful for
* running setup code, such as fetching resources or rendering.
* Generally, you should try to delay work until this time.
*/
connectedCallback() {
super.connectedCallback();
this.addEventListener('app-localize-resources-error',
this._unregionalize.bind(this));
}
/**
* Called every time the element is removed from the DOM. Useful for
* running clean up code (removing event listeners, etc.).
*/
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('app-localize-resources-error',
this._unregionalize.bind(this));
}
_onLanguageChanged(language) {
if (language) {
var url = this.resolveUrl(`${this.localesFolder}/${language}.json`);
this.loadResources(url, language, true);
}
}
/**
* Loads locales file for internationalization.
*/
_localesFileChanged() {
if (this.localesFile && this.localesFile.trim().length != 0) {
this.loadResources(this.resolveUrl(this.localesFile, window.location.origin));
}
}
/**
* Resets the language without the region information. In the case the
* resources load failed because of a file not found this brings one more
* chance to find translations more suitable to the user.
*/
_unregionalize() {
if (this.language.includes('-')) {
this.language = this.language.split('-')[0];
}
}
};
Hostabee.ElementMixin = Polymer.dedupingMixin(ElementMixin);
})();
</script>