@apitable/i18n
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -54,4 +54,8 @@ "use strict"; | ||
const text = languagePack[stringKey]; | ||
if (!text) | ||
throw new i18n_errors_1.StringNotFoundError(`Cannot find string key: ${stringKey}`); | ||
// if (!text) throw new StringNotFoundError(`Cannot find string key: ${stringKey}`); | ||
if (!text) { | ||
console.log(`Cannot find string key: ${stringKey}`); | ||
return ''; | ||
} | ||
; | ||
const str = options ? (0, lodash_1.template)(text)(options) : text; | ||
@@ -58,0 +62,0 @@ if (isPlural) { |
@@ -18,4 +18,2 @@ "use strict"; | ||
__exportStar(require("./i18n.class"), exports); | ||
__exportStar(require("./language_pack/loader.interface"), exports); | ||
__exportStar(require("./language_pack/packs.interface"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const i18n_class_1 = require("../lib/i18n.class"); | ||
const i18n_errors_1 = require("../lib/i18n.errors"); | ||
const lib_1 = require("../lib/"); | ||
const mockLanguagePacks = { | ||
@@ -16,3 +16,3 @@ 'en-US': { | ||
it('should get text ok', () => { | ||
const i18n = lib_1.I18N.createByLanguagePacks(mockLanguagePacks); | ||
const i18n = i18n_class_1.I18N.createByLanguagePacks(mockLanguagePacks); | ||
expect(i18n.getText('text1')).toBe('This is text 1'); | ||
@@ -27,16 +27,3 @@ i18n.setLanguage('zh-CN'); | ||
}); | ||
class CustomLoader { | ||
load(language) { | ||
return mockLanguagePacks[language]; | ||
} | ||
} | ||
it('should custom loader ok', () => { | ||
const i18n_error = lib_1.I18N.createByLoader(new CustomLoader(), 'any-any'); | ||
expect(() => i18n_error.getText('text1')).toThrow(i18n_errors_1.LanguagePackNotFoundError); | ||
const i18n = lib_1.I18N.createByLoader(new CustomLoader()); | ||
expect(i18n.getText('text1')).toBe('This is text 1'); | ||
i18n.setLanguage('zh-CN'); | ||
expect(i18n.getText('text1')).toBe('这是中文1'); | ||
}); | ||
}); | ||
//# sourceMappingURL=i18n.test.js.map |
{ | ||
"name": "@apitable/i18n", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "i18n Locales with APITable power.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/lib/index.js", |
123
README.md
@@ -1,2 +0,2 @@ | ||
# apitable-i18n-generator | ||
# apitable-i18n | ||
@@ -19,24 +19,23 @@ `APITable i18n` is a a simple / easy-to-use / lightweight internationalization tool. | ||
## Template | ||
## SDKs | ||
Imagine it, you have a `locales.json` file: | ||
You can get an APITable template that use for your i18n. | ||
https://apitable.com/template/i18n | ||
## Generator | ||
```bash | ||
npx apitable-i18-generator --url http://apitable.com/dstYOURSTRINGSTABLE --token ${YOUR_APITABLE_TOKEN} --output ./i18n-generated | ||
```json | ||
{ | ||
"en-US": { | ||
"text1": "This is text 1", | ||
"text2": "This is text 2" | ||
}, | ||
"zh-CN": { | ||
"text1": "这是中文1" | ||
} | ||
} | ||
``` | ||
Then you will get: | ||
- locales.json | ||
- locales.interface.ts | ||
Then: | ||
## SDKs | ||
### TypeScript | ||
Install APITable i18n generator: | ||
Install APITable i18n : | ||
```bash | ||
@@ -46,10 +45,12 @@ npm i --save @apitable/i18n | ||
Import to your project: | ||
```typescript | ||
import { I18N } from '@apitable/i18n'; | ||
const languagePacks = require('locales.json'); | ||
const i18n = I18N.createByLanguagePacks(languagePacks, getLanguage()); | ||
import { I18N, ILanguagePackerLoader, ILanguagePack } from '@apitable/i18n'; | ||
const languagePacks = require('./locales.json'); | ||
const i18n = I18N.createByLanguagePacks(languagePacks); | ||
i18n.setLanguage('zh-CN'); | ||
const res = i18n.getText('text1') | ||
console.log(i18n.getText("string:keyname")); | ||
console.log(res); | ||
// will print '这是中文1'; | ||
``` | ||
@@ -59,3 +60,3 @@ | ||
### Java | ||
### ~~Java~~ | ||
@@ -70,2 +71,78 @@ ```java | ||
## Generator | ||
If you want to integration `@apitable/i18n` with APITable. | ||
Go with [`APITable Settings Generator`](https://github.com/apitable/apitable-settings-generator). | ||
### TODO: 1. APITable Template | ||
You can get an APITable template that use for your i18n. | ||
https://apitable.com/template/i18n | ||
### 2. Generate settings | ||
Use [apitable-settings-generator](https://github.com/apitable/apitable-settings-generator) and the `column-files` format to generate i18n languages packs. | ||
The config.json: | ||
```json | ||
[ | ||
{ | ||
"dirName": "./generated", | ||
"fileName": "locales.*.json", | ||
"tables": { | ||
{ | ||
"datasheetId": "dstbUhd5coNXQoXFD8", | ||
"datasheetName": "strings", | ||
"format": "column-files", | ||
"params": {} | ||
} | ||
} | ||
} | ||
] | ||
``` | ||
Run to generate i18n strings: | ||
```bash | ||
# run in bash | ||
npx apitable-settings-generator --config config.json --token ${HERE_IS_YOUR_APITABLE_TOKEN} | ||
``` | ||
Generated settings locales.en_US.json: | ||
```json | ||
{ | ||
"strings": { | ||
"en_US": { | ||
"login_title": "Login APITable", | ||
"some text": "some text en_US" | ||
} | ||
} | ||
} | ||
``` | ||
Generated settings locales.zh_CN.json: | ||
```json | ||
{ | ||
"strings": { | ||
"zh_CN": { | ||
"login_title": "中文APITable", | ||
"some text": "some text zh_CN" | ||
} | ||
} | ||
} | ||
``` | ||
Then you will get: | ||
- locales.json | ||
- ~~locales.interface.ts(quicktype)~~ | ||
### TODO: 3. Use SDKs to read them | ||
## Development | ||
@@ -72,0 +149,0 @@ |
export * from './i18n.class'; | ||
export * from './language_pack/loader.interface'; | ||
export * from './language_pack/packs.interface'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21175
149
323