Skip to content

Commit 4cdc5e8

Browse files
committed
Removed some stuff. Updated the readme.
1 parent 6e82401 commit 4cdc5e8

File tree

3 files changed

+38
-68
lines changed

3 files changed

+38
-68
lines changed

README.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
# LitElement Internationalisation
22
LitElement Internationalisation
33

4-
## Install
5-
```sh
6-
npm install --save lit-element-i18n
7-
```
8-
9-
## Resources
10-
/assets/locales/en/app.json
11-
```json
12-
{ "hi": "Hi" }
13-
```
14-
/assets/locales/sv/app.json
15-
```json
16-
{ "hi": "Hej" }
174
```
185
196
## Usage
207
```js
218
import { LitElement, html } from 'lit-element'
22-
import { i18nMixin, translate } from 'lit-element-i18n'
9+
import { i18nMixin } from 'lit-element-i18n'
2310
2411
class DemoElement extends i18nMixin(LitElement) {
2512
26-
constructor(){
27-
super();
28-
this.languageResources = '/assets/locales/{{lng}}/{{ns}}.json'
13+
firstUpdate() {
14+
super.firstUpdate();
15+
16+
// Initialize i18n mixin
17+
this.i18nInit({
18+
'en': {
19+
'app': {
20+
'hi': 'Hello US'
21+
}
22+
}, 'en-CA': {
23+
'app': {
24+
'hi': 'Hello CA'
25+
}
26+
}
27+
});
2928
}
3029
3130
render() {
3231
return html`
33-
<h1>${translate('app:hi')}</h1>
34-
32+
<h1>${this.translate('app:hi')}</h1>
33+
3534
<select @change='${this.changeLanguages}'>
36-
<option value='en'>EN</option>
37-
<option value='sv'>SV</option>
35+
<option value='en-US'>English (US)</option>
36+
<option value='en-CA'>English (CA)</option>
3837
</select>
3938
`
4039
}
@@ -45,4 +44,4 @@ class DemoElement extends i18nMixin(LitElement) {
4544
}
4645
4746
customElements.define('demo-element', DemoElement)
48-
```
47+
```

lit-element-i18n.js

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,51 @@ import i18next, { t as translate } from 'i18next';
22

33
export const i18nMixin = baseClass => class extends baseClass {
44

5-
/**
6-
* Add translation resources
7-
* @param {String} language - The language code.
8-
* @param {String} namespace - I18next namespace.
9-
* @param {Object} resources - i18next resources.
10-
*/
11-
addResources(language, namespace, resources) {
12-
i18next.addResources(language, namespace, resources);
13-
}
14-
15-
/**
16-
* Read in configuration files.
17-
* @param {Array} filenames - Configuration file names
18-
* @return {Object} Configuration objects associated with the files.
19-
*/
20-
async readFiles(filenames) {
21-
let configs = [];
22-
for (const filename of filenames) {
23-
24-
console.log('Reading config file ' + filename);
25-
const config = await import(filename);
26-
configs.push(config);
27-
}
28-
return configs;
29-
}
30-
31-
/**
32-
* Detect browser language settings.
33-
* @return {String} Language code used by browser.
34-
*/
35-
detectBrowserLanguage() {
36-
37-
const language = navigator.language;
38-
console.log('Detected browser language ' + language);
39-
return language;
40-
}
41-
425
/**
436
* Initialize i18nMixin.
447
*
458
* Call this function in the firstUpdated() { .. } callback to initialize the mixin.
469
* After that, the translation functionality will be applicable.
4710
*
48-
* @param {Object} resources - Options to configure mixin.
11+
* @param {Object} resources - I18next resources object. See https://www.i18next.com/overview/configuration-options.
4912
*/
50-
async i18nInit(resources) {
51-
52-
const language = this.detectBrowserLanguage();
13+
i18nInit(resources) {
5314

5415
if (!i18next.isInitialized) {
16+
17+
console.log('Initializing i18n');
5518
i18next.
5619
init({
57-
lng: language,
20+
lng: navigator.language,
5821
resources,
5922
defaultNS: 'translations',
6023
ns: ['translations'],
61-
fallbackLng: 'en-US'
24+
fallbackLng: 'en-CA'
6225
})
6326
}
6427

6528
i18next.on('initialized', options => {
6629
this.requestUpdate()
6730
})
6831
i18next.on('languageChanged', lng => {
69-
const language = this.detectBrowserLanguage();
70-
this.changeLanguage(language);
32+
this.changeLanguage(lng);
7133
this.requestUpdate();
7234
})
7335
}
7436

37+
/**
38+
* Change the language of i18next.
39+
* @param {String} lang - Language code to which to switch.
40+
*/
7541
changeLanguage(lang) {
76-
i18next.changeLanguage(lang)
42+
return i18next.changeLanguage(lang);
7743
}
7844

45+
/**
46+
* Apply the translation of key.
47+
* @param {String} key - i18next translation key
48+
* @returns {String} - Translated result.
49+
*/
7950
translate(key) {
8051
return translate(key)
8152
}

test/lit-element-i18n.test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)