From 4a566f71700a3e29de457ee399be673bf036f691 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 18 Nov 2024 13:01:05 +0800 Subject: [PATCH] feat: add i18n supports --- build.gradle | 2 +- packages/comment-widget/lit-localize.json | 19 +++ packages/comment-widget/package.json | 2 + .../comment-widget/src/base-comment-item.ts | 3 +- packages/comment-widget/src/base-form.ts | 33 +++-- packages/comment-widget/src/comment-form.ts | 11 +- packages/comment-widget/src/comment-item.ts | 3 +- .../comment-widget/src/comment-pagination.ts | 5 +- .../comment-widget/src/comment-replies.ts | 5 +- packages/comment-widget/src/comment-widget.ts | 5 +- packages/comment-widget/src/emoji-button.ts | 19 ++- .../src/generated/locale-codes.ts | 27 ++++ .../src/generated/locales/es.ts | 40 ++++++ .../src/generated/locales/zh-CN.ts | 40 ++++++ .../src/generated/locales/zh-TW.ts | 40 ++++++ packages/comment-widget/src/index.ts | 1 + packages/comment-widget/src/locale/index.ts | 34 +++++ packages/comment-widget/src/reply-form.ts | 11 +- packages/comment-widget/src/reply-item.ts | 3 +- packages/comment-widget/src/utils/date.ts | 13 +- packages/comment-widget/xliff/es.xlf | 115 +++++++++++++++++ packages/comment-widget/xliff/zh-CN.xlf | 115 +++++++++++++++++ packages/comment-widget/xliff/zh-TW.xlf | 115 +++++++++++++++++ pnpm-lock.yaml | 119 ++++++++++++++++++ 24 files changed, 746 insertions(+), 34 deletions(-) create mode 100644 packages/comment-widget/lit-localize.json create mode 100644 packages/comment-widget/src/generated/locale-codes.ts create mode 100644 packages/comment-widget/src/generated/locales/es.ts create mode 100644 packages/comment-widget/src/generated/locales/zh-CN.ts create mode 100644 packages/comment-widget/src/generated/locales/zh-TW.ts create mode 100644 packages/comment-widget/src/locale/index.ts create mode 100644 packages/comment-widget/xliff/es.xlf create mode 100644 packages/comment-widget/xliff/zh-CN.xlf create mode 100644 packages/comment-widget/xliff/zh-TW.xlf diff --git a/build.gradle b/build.gradle index 2f2a1e0..c820687 100644 --- a/build.gradle +++ b/build.gradle @@ -41,6 +41,6 @@ build { } halo { - version = "2.18.0" + version = "2.20" debug = true } diff --git a/packages/comment-widget/lit-localize.json b/packages/comment-widget/lit-localize.json new file mode 100644 index 0000000..6d04a27 --- /dev/null +++ b/packages/comment-widget/lit-localize.json @@ -0,0 +1,19 @@ +{ + "$schema": "https://raw.githubusercontent.com/lit/lit/main/packages/localize-tools/config.schema.json", + "sourceLocale": "en", + "targetLocales": [ + "es", + "zh-CN", + "zh-TW" + ], + "tsConfig": "./tsconfig.json", + "output": { + "mode": "runtime", + "outputDir": "./src/generated/locales", + "localeCodesModule": "./src/generated/locale-codes.ts" + }, + "interchange": { + "format": "xliff", + "xliffDir": "./xliff/" + } +} \ No newline at end of file diff --git a/packages/comment-widget/package.json b/packages/comment-widget/package.json index 3d40b22..bcd10e7 100644 --- a/packages/comment-widget/package.json +++ b/packages/comment-widget/package.json @@ -36,6 +36,7 @@ "@emoji-mart/data": "^1.1.2", "@halo-dev/api-client": "^2.14.0", "@lit/context": "^1.1.0", + "@lit/localize": "^0.12.2", "dayjs": "^1.11.10", "emoji-mart": "^5.5.2", "javascript-time-ago": "^2.5.9", @@ -43,6 +44,7 @@ "lodash-es": "^4.17.21" }, "devDependencies": { + "@lit/localize-tools": "^0.8.0", "@types/lodash-es": "^4.17.12", "lit-analyzer": "^2.0.3" } diff --git a/packages/comment-widget/src/base-comment-item.ts b/packages/comment-widget/src/base-comment-item.ts index 0a01e09..f902a31 100644 --- a/packages/comment-widget/src/base-comment-item.ts +++ b/packages/comment-widget/src/base-comment-item.ts @@ -4,6 +4,7 @@ import { property } from 'lit/decorators.js'; import { formatDate, timeAgo } from './utils/date'; import baseStyles from './styles/base'; import varStyles from './styles/var'; +import { msg } from '@lit/localize'; export class BaseCommentItem extends LitElement { @property({ type: String }) @@ -45,7 +46,7 @@ export class BaseCommentItem extends LitElement {
${timeAgo(this.creationTime)}
- ${!this.approved ? html`
审核中
` : ''} + ${!this.approved ? html`
${msg('Reviewing')}
` : ''}
diff --git a/packages/comment-widget/src/base-form.ts b/packages/comment-widget/src/base-form.ts index 460b92a..df8b75b 100644 --- a/packages/comment-widget/src/base-form.ts +++ b/packages/comment-widget/src/base-form.ts @@ -19,6 +19,7 @@ import './icons/icon-loading'; import { ToastManager } from './lit-toast'; import baseStyles from './styles/base'; import varStyles from './styles/var'; +import { msg } from '@lit/localize'; export class BaseForm extends LitElement { @consume({ context: baseUrlContext }) @@ -99,7 +100,7 @@ export class BaseForm extends LitElement { const response = await fetch(`/apis/api.commentwidget.halo.run/v1alpha1/captcha/-/generate`); if (!response.ok) { - this.toastManager?.error('获取验证码失败'); + this.toastManager?.error(msg('Failed to obtain verification code')); return; } @@ -111,7 +112,13 @@ export class BaseForm extends LitElement { } async handleLogout() { - if (window.confirm('点击确定将跳转至退出登录页面,请确保正在编辑的内容已保存。')) { + if ( + window.confirm( + msg( + 'Click OK to jump to the logout page, Please make sure the content being edited has been saved.' + ) + ) + ) { try { window.location.href = '/logout'; } catch (error) { @@ -125,7 +132,7 @@ export class BaseForm extends LitElement { ${this.currentUser?.spec.avatar ? html`` : ''} ${this.currentUser?.spec.displayName || this.currentUser?.metadata.name}
`; } @@ -169,7 +176,7 @@ export class BaseForm extends LitElement {