Add Chinese translation

Simplified Chinese and Traditional Chinese
This commit is contained in:
Brendan Lee 2022-06-06 14:45:15 +08:00
parent cd4af6b523
commit f9b24ebd30
4 changed files with 39 additions and 6 deletions

View File

@ -20,6 +20,8 @@ import messages_sv from './messages_sv.json';
import messages_th from './messages_th.json';
import messages_tr from './messages_tr.json';
import messages_ur from './messages_ur.json';
import messages_zh_CN from './messages_zh_CN.json';
import messages_zh_TW from './messages_zh_TW.json';
const MESSAGES = {
ar: messages_ar,
@ -39,9 +41,13 @@ const MESSAGES = {
sv: messages_sv,
th: messages_th,
tr: messages_tr,
ur: messages_ur
ur: messages_ur,
'zh-CN': messages_zh_CN,
'zh-TW': messages_zh_TW
}
export const availableLocales = Object.keys(MESSAGES);
const i18n = new Polyglot({ allowMissing: true });
i18n.init = (lang, opt_messages) => {
@ -74,6 +80,8 @@ import ru from 'timeago.js/lib/lang/ru';
import sv from 'timeago.js/lib/lang/sv';
import th from 'timeago.js/lib/lang/th';
import tr from 'timeago.js/lib/lang/tr';
import zh_CN from 'timeago.js/lib/lang/zh_CN';
import zh_TW from 'timeago.js/lib/lang/zh_TW';
// import ur from 'timeago.js/lib/lang/ur'; // Not currently supported by TimeAgo
timeago.register('ar', ar);
@ -93,6 +101,8 @@ timeago.register('ru', ru);
timeago.register('sv', sv);
timeago.register('th', th);
timeago.register('tr', tr);
timeago.register('zh-CN', zh_CN);
timeago.register('zh-TW', zh_TW);
/**
* Helper function that allows plugins to register their

View File

@ -0,0 +1,10 @@
{
"Add a comment...": "添加评论...",
"Add a reply...": "添加回复...",
"Add tag...": "添加标签...",
"Cancel": "取消",
"Close": "关闭",
"Edit": "编辑",
"Delete": "删除",
"Ok": "确认"
}

View File

@ -0,0 +1,10 @@
{
"Add a comment...": "添加評論...",
"Add a reply...": "添加回覆...",
"Add tag...": "添加標籤...",
"Cancel": "取消",
"Close": "關閉",
"Edit": "編輯",
"Delete": "刪除",
"Ok": "確認"
}

View File

@ -1,4 +1,4 @@
import I18n from '../i18n';
import I18n, { availableLocales } from '../i18n';
/**
* Helper to init the i18n class with a pre-defined or auto-detected locale.
@ -8,11 +8,14 @@ export const setLocale = (locale, opt_messages) => {
const l = locale === 'auto' ?
window.navigator.userLanguage || window.navigator.language : locale;
try {
I18n.init(l.split('-')[0].toLowerCase(), opt_messages);
} catch (error) {
const fallback = l.split('-')[0].toLowerCase();
const foundLocale = [l, fallback].find(_l => availableLocales.includes(_l));
if (!foundLocale) {
console.warn(`Unsupported locale '${l}'. Falling back to default en.`);
}
I18n.init(foundLocale, opt_messages);
} else {
I18n.init(null, opt_messages);
}