Skip to content

Commit

Permalink
fix: Missing i18n partial translation prevents startup
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed Mar 8, 2023
1 parent e671048 commit 94b4cac
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"cursor-effects": "^1.0.8",
"emoji-mart": "^5.5.2",
"jsonfile": "^6.1.0",
"lodash": "^4.17.21",
"naive-ui": "^2.34.3",
"ora": "^6.1.2",
"party-js": "^2.2.0",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/__test__/i18n.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { beforeAll, describe, expect, it } from 'vitest';
import chalk from 'chalk';

import enPack from '../locale/en';
import zhPack from '../locale/zh-cn';

let languagePack;
describe('i18n', () => {
beforeAll(() => {
localStorage.setItem('language', 'zh');
});
it('localstorage work fine', () => {
expect(localStorage.getItem('name')).null;
localStorage.setItem('name', 'awesome');
expect(localStorage.getItem('name')).eq('awesome');
});

it('should return selected lang', async () => {
languagePack = (await import('../i18n')).default;
console.log(languagePack);
expect(languagePack['__FOR_TEST'].exist.child).eq(zhPack['__FOR_TEST'].exist.child);
});

it('should return default lang', () => {
expect(languagePack['__FOR_TEST'].notExist.child).eq(enPack['__FOR_TEST'].notExist.child);
});
});
53 changes: 53 additions & 0 deletions src/__test__/utils/common.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';
import { deepClone, deepCloneArr, deepCloneObj } from '../../utils/common';

const obj1 = {
name: 'John',
age: 30,
address: {
city: 'New York',
state: 'NY',
},
skills: [
'js',
'ts',
'py',
() => {
console.log(111);
},
],
};
const arr1 = [
{ name: 'John', age: 30 },
{ name: 'Mary', age: 25 },
{
name: 'Bob',
age: 40,
skill() {
console.log('js');
},
},
{
others: [
{ name: 'Mary', age: 25 },
{ name: 'Bob', age: 40 },
],
},
];

describe('common utils', () => {
it('placeholder', () => {
expect(1 + 1).toBe(2);
});
// it('clone object[]', () => {
// const arr2 = deepCloneArr(arr1);
// expect(arr1).not.toBe(arr2);
// expect(arr1).toEqual(arr2);
// });

// it('clone object', async () => {
// const obj2 = deepCloneObj(obj1);
// expect(obj1).not.toBe(obj2);
// expect(obj1).toEqual(obj2);
// });
});
5 changes: 4 additions & 1 deletion src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { merge } from 'lodash';
import en from './locale/en';
import zh from './locale/zh-cn';
class T {
Expand All @@ -13,7 +14,9 @@ class T {
}

get texts(): typeof this.all.en {
return this.all[this.lang];
const selectLangPack = this.all[this.lang];
const defaultLangPack = this.all['en'];
return merge(defaultLangPack, selectLangPack);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/locale/en.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export default {
__FOR_TEST: {
notExist: {
child: 'This is child',
},
exist: {
child: 'This is child',
},
},
menu: {
setBannerForCurrent: 'Set Random Banner For Current File',
setBannerForTheFolder: 'Set random banner for this path',
Expand Down
5 changes: 5 additions & 0 deletions src/locale/zh-cn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default {
__FOR_TEST: {
exist: {
child: '我是中文包',
},
},
menu: {
setBannerForCurrent: '切换当前文件banner图',
setBannerForTheFolder: '切换当前路径下文件的banner图',
Expand Down
21 changes: 21 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,24 @@ export function getNumberFromStr(str: string) {
const nums = str.match(/\d+(.\d+)?/g);
return nums?.map(num => parseFloat(num)) || [];
}

export function deepCloneObj(source) {
// TODO
// const target = {};
// for (const key in source) {
// if (typeof source[key] === 'object' && source[key] !== null) {
// target[key] = deepClone(source[key]);
// } else {
// target[key] = source[key];
// }
// }
// return target;
}

export function deepCloneArr(source) {
// TODO
}

export function deepClone(source) {
// TODO
}
5 changes: 5 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export default defineConfig({
}
return assetInfo.name || '';
},
// // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
globals: {
// vue: 'Vue',
},
},
external: [
'obsidian',
Expand Down Expand Up @@ -108,4 +112,5 @@ export default defineConfig({
},
],
},
logLevel: 'info',
});
7 changes: 7 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
environment: 'jsdom',
},
});

0 comments on commit 94b4cac

Please sign in to comment.