Skip to content

Commit

Permalink
Merge pull request #106 from brendan-lee/chore/chinese-translation
Browse files Browse the repository at this point in the history
Add Chinese translation
  • Loading branch information
rsimon authored Jul 23, 2022
2 parents 7ee6462 + c4ef4de commit e7d7cd1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/i18n/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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,
Expand All @@ -41,9 +43,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) => {
Expand Down Expand Up @@ -78,6 +84,8 @@ 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 ur from 'timeago.js/lib/lang/ur'; // Not currently supported by TimeAgo
import zh_CN from 'timeago.js/lib/lang/zh_CN';
import zh_TW from 'timeago.js/lib/lang/zh_TW';

timeago.register('ar', ar);
timeago.register('cs', cs);
Expand All @@ -97,6 +105,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
Expand Down
10 changes: 10 additions & 0 deletions src/i18n/messages_zh_CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Add a comment...": "添加评论...",
"Add a reply...": "添加回复...",
"Add tag...": "添加标签...",
"Cancel": "取消",
"Close": "关闭",
"Edit": "编辑",
"Delete": "删除",
"Ok": "确认"
}
10 changes: 10 additions & 0 deletions src/i18n/messages_zh_TW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Add a comment...": "添加評論...",
"Add a reply...": "添加回覆...",
"Add tag...": "添加標籤...",
"Cancel": "取消",
"Close": "關閉",
"Edit": "編輯",
"Delete": "刪除",
"Ok": "確認"
}
11 changes: 7 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
}
Expand Down

0 comments on commit e7d7cd1

Please sign in to comment.