Skip to content

Commit

Permalink
fixed a bug not loading data
Browse files Browse the repository at this point in the history
  • Loading branch information
calidion committed May 10, 2019
1 parent 6d0185f commit 1198b70
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ const i18n = new I18n(options);
const i18n = new I18n(options, windows.localStorage);
```

## Init data

To use translator, data must be initialized at first:

```ts
await i18n.init();
```

Now you have the json data prepared for access.

## Get Locale

```ts
Expand Down Expand Up @@ -136,4 +146,4 @@ await i18n._('TITLE.SUBTITLE.SUBTITLE', 'ja');
i18n._sync('TITLE.SUBTITLE.SUBTITLE');
i18n._sync('TITLE', 'zh-CN');
i18n._sync('TITLE.SUBTITLE.SUBTITLE', 'ja');
```
```
8 changes: 8 additions & 0 deletions __tests__/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ test('Should translate with http loader', async () => {

const i18n: I18n = new I18n(options, storage);

await i18n.init();

let count = 0;
i18n.listen(() => {
count++;
Expand All @@ -171,6 +173,12 @@ test('Should translate with http loader', async () => {
i18n.listen(a);
i18n.listen(a);

const b = i18n._sync('b');
expect(b).toBe('bbbb');

await i18n.setLocale('ja');
expect(count).toBe(0);

await i18n.setLocale('zh');
expect(count).toBe(3);

Expand Down
5 changes: 3 additions & 2 deletions __tests__/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"a" : 100
}
"a": 100,
"b": "bbbb"
}
4 changes: 4 additions & 0 deletions __tests__/locales/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a": 100,
"b": "bbbb"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-i18n-core",
"version": "0.2.0",
"version": "0.2.1",
"description": "node-i18n-core",
"license": "MIT",
"repository": "calidion/node-i18n",
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class I18n {
}
}

public async init() {
await this.setLocale(this.options.current, this.storage);
}

public async _(key: string, locale?: string): Promise<string> {
if (!locale) {
locale = this.options.current;
Expand Down Expand Up @@ -73,6 +77,7 @@ export class I18n {
}

public async setLocale(locale: string, storage?: IStorable) {
await this.getJSON(locale);
if (locale !== this.options.current) {
const from = this.options.current;
this.options.current = locale;
Expand All @@ -81,7 +86,6 @@ export class I18n {
} else if (this.storage) {
this.storage.setItem(I18n.LOCALE, locale);
}
await this.getJSON(locale);
this.notify(from, locale);
}
}
Expand Down

0 comments on commit 1198b70

Please sign in to comment.