diff --git a/src/i18n/index.jsx b/src/i18n/index.jsx index 8dd1fc4..929ff4b 100644 --- a/src/i18n/index.jsx +++ b/src/i18n/index.jsx @@ -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,7 +80,9 @@ 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 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'; +// import ur from 'timeago.js/lib/lang/ur'; // Not currently supported by TimeAgo timeago.register('ar', ar); timeago.register('cs', cs); @@ -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 diff --git a/src/i18n/messages_zh_CN.json b/src/i18n/messages_zh_CN.json new file mode 100644 index 0000000..b1b9277 --- /dev/null +++ b/src/i18n/messages_zh_CN.json @@ -0,0 +1,10 @@ +{ + "Add a comment...": "添加评论...", + "Add a reply...": "添加回复...", + "Add tag...": "添加标签...", + "Cancel": "取消", + "Close": "关闭", + "Edit": "编辑", + "Delete": "删除", + "Ok": "确认" +} diff --git a/src/i18n/messages_zh_TW.json b/src/i18n/messages_zh_TW.json new file mode 100644 index 0000000..844d3f2 --- /dev/null +++ b/src/i18n/messages_zh_TW.json @@ -0,0 +1,10 @@ +{ + "Add a comment...": "添加評論...", + "Add a reply...": "添加回覆...", + "Add tag...": "添加標籤...", + "Cancel": "取消", + "Close": "關閉", + "Edit": "編輯", + "Delete": "刪除", + "Ok": "確認" +} diff --git a/src/utils/index.js b/src/utils/index.js index 22772a8..b201575 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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); }