build:ts success

This commit is contained in:
rinsuki 2018-06-17 19:09:24 +09:00
parent f19075c50a
commit 871f886702
8 changed files with 72 additions and 44 deletions

View file

@ -2,7 +2,7 @@
* Replace i18n texts
*/
import locale from '../../locales';
import locale, { isAvailableLanguage, LocaleObject, LocaleObjectChildren } from '../../locales';
export default class Replacer {
private lang: string;
@ -16,19 +16,19 @@ export default class Replacer {
this.replacement = this.replacement.bind(this);
}
private get(path: string, key: string) {
const texts = locale[this.lang];
if (texts == null) {
private get(path: string, key: string): string {
if (!isAvailableLanguage(this.lang)) {
console.warn(`lang '${this.lang}' is not supported`);
return key; // Fallback
}
let text = texts;
const texts = locale[this.lang];
let text: LocaleObjectChildren = texts;
if (path) {
if (text.hasOwnProperty(path)) {
text = text[path];
text = text[path] as LocaleObject;
} else {
console.warn(`path '${path}' not found in '${this.lang}'`);
return key; // Fallback
@ -38,7 +38,7 @@ export default class Replacer {
// Check the key existance
const error = key.split('.').some(k => {
if (text.hasOwnProperty(k)) {
text = text[k];
text = (text as LocaleObject)[k];
return false;
} else {
return true;
@ -48,12 +48,15 @@ export default class Replacer {
if (error) {
console.warn(`key '${key}' not found in '${path}' of '${this.lang}'`);
return key; // Fallback
} else if (typeof text !== "string") {
console.warn(`key '${key}' is not string in '${path}' of '${this.lang}'`);
return key; // Fallback
} else {
return text;
}
}
public replacement(match, key) {
public replacement(match: string, key: string) {
let path = null;
if (key.indexOf('|') != -1) {