From 09858eb1d3ab1a0f19757eb527d3d4262ec6fe35 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Fri, 11 Dec 2020 10:07:07 +0800 Subject: [PATCH] docs: improve demo translating --- docs/site/use-translate.ts | 22 +++++++ jest.config.js | 3 + src/action-sheet/demo/index.vue | 65 ++++++++++--------- src/composables/use-translate.ts | 8 --- src/count-down/demo/index.vue | 55 ++++++++-------- src/dialog/demo/index.vue | 43 ++++++------ .../test/__snapshots__/demo.spec.js.snap | 2 +- src/image-preview/demo/index.vue | 55 ++++++++-------- src/pull-refresh/demo/index.vue | 37 ++++++----- src/rate/demo/index.vue | 48 +++++++------- src/search/demo/index.vue | 45 ++++++------- src/slider/demo/index.vue | 53 +++++++-------- src/swipe/demo/index.vue | 41 ++++++------ src/switch/demo/index.vue | 45 ++++++------- webpack.config.js | 7 ++ 15 files changed, 281 insertions(+), 248 deletions(-) create mode 100644 docs/site/use-translate.ts delete mode 100644 src/composables/use-translate.ts diff --git a/docs/site/use-translate.ts b/docs/site/use-translate.ts new file mode 100644 index 000000000..56c4dfbf3 --- /dev/null +++ b/docs/site/use-translate.ts @@ -0,0 +1,22 @@ +import Locale from '../../src/locale'; +import { camelize } from '../../src/utils/format/string'; +import { createTranslate } from '../../src/utils/create/translate'; + +let demoUid = 0; + +export function useTranslate(i18n: Record) { + const demoName = `demo-i18n-${demoUid++}`; + + if (i18n) { + const locales = {}; + const camelizedName = camelize(demoName); + + Object.keys(i18n).forEach((key) => { + locales[key] = { [camelizedName]: i18n[key] }; + }); + + Locale.add(locales); + } + + return createTranslate(demoName); +} diff --git a/jest.config.js b/jest.config.js index 741650f95..5048071b3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,4 +6,7 @@ module.exports = { '!**/test/**', '!**/lang/**', ], + moduleNameMapper: { + '^@demo(.*)$': '/docs/site$1', + }, }; diff --git a/src/action-sheet/demo/index.vue b/src/action-sheet/demo/index.vue index 6caf16a52..5792591c0 100644 --- a/src/action-sheet/demo/index.vue +++ b/src/action-sheet/demo/index.vue @@ -53,43 +53,43 @@