Skip to content

Commit

Permalink
feat: build internationalization config
Browse files Browse the repository at this point in the history
  • Loading branch information
richard1015 committed Apr 19, 2022
1 parent 56d1bf9 commit f50f8fb
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 173 deletions.
11 changes: 6 additions & 5 deletions jd/generate-nutui-taro-vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const package = require('../package.json');
const config = require('../src/config.json');
const path = require('path');
const fs = require('fs-extra');
let importStr = `//import { App } from 'vue';\n`;
let importStr = `import { App } from 'vue';
import Locale from './locale';\n`;
let importScssStr = `\n`;
const packages = [];
config.nav.map((item) => {
Expand All @@ -29,17 +30,17 @@ let installFunction = `function install(app: any) {
let fileStrBuild = `${importStr}
${installFunction}
const version = '${package.version}';
export { install, version };
export default { install, version};`;
export { install, version, Locale };
export default { install, version, Locale};`;

fs.outputFile(path.resolve(__dirname, '../src/packages/nutui.taro.vue.build.ts'), fileStrBuild, 'utf8', (error) => {
// logger.success(`${package_config_path} 文件写入成功`);
});
let fileStrDev = `${importStr}
${installFunction}
${importScssStr}
export { install, ${packages.join(',')} };
export default { install, version:'${package.version}'};`;
export { install, Locale, ${packages.join(',')} };
export default { install, version:'${package.version}', Locale};`;
fs.outputFile(path.resolve(__dirname, '../src/packages/nutui.taro.vue.ts'), fileStrDev, 'utf8', (error) => {
// logger.success(`${package_config_path} 文件写入成功`);
});
11 changes: 6 additions & 5 deletions jd/generate-nutui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const package = require('../package.json');
const config = require('../src/config.json');
const path = require('path');
const fs = require('fs-extra');
let importStr = `import { App } from 'vue';\n`;
let importStr = `import { App } from 'vue';
import Locale from './locale';\n`;
let importScssStr = `\n`;
const packages = [];
config.nav.map((item) => {
Expand All @@ -26,8 +27,8 @@ let installFunction = `function install(app: App) {
let fileStrBuild = `${importStr}
${installFunction}
const version = '${package.version}';
export { install, version, ${packages.join(',')}};
export default { install, version};`;
export { install, version, Locale, ${packages.join(',')}};
export default { install, version, Locale};`;

fs.outputFile(path.resolve(__dirname, '../src/packages/nutui.vue.build.ts'), fileStrBuild, 'utf8', (error) => {
// logger.success(`${package_config_path} 文件写入成功`);
Expand All @@ -37,8 +38,8 @@ let fileStrDev = `${importStr}
${installFunction}
${importScssStr}
export const testComponents = { ${packages.join(',')}};
export { install, ${packages.join(',')} };
export default { install, version:'${package.version}'};`;
export { install, Locale, ${packages.join(',')} };
export default { install, version:'${package.version}', Locale};`;
fs.outputFile(path.resolve(__dirname, '../src/packages/nutui.vue.ts'), fileStrDev, 'utf8', (error) => {
// logger.success(`${package_config_path} 文件写入成功`);
});
4 changes: 2 additions & 2 deletions jd/generate-types-taro.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const config = require('../src/config.json');
const path = require('path');
const fs = require('fs-extra');
let importStr = ``;
let importStr = `import Locale from '../packages/locale';\n`;
const packages = [];
config.nav.map((item) => {
item.packages.forEach((element) => {
Expand All @@ -15,7 +15,7 @@ config.nav.map((item) => {
});
});
let installFunction = `
export { ${packages.join(',')} };`;
export { Locale,${packages.join(',')} };`;
let fileStr = importStr + installFunction;
fs.outputFileSync(path.resolve(__dirname, '../dist/types/nutui.d.ts'), fileStr, 'utf8');
fs.outputFileSync(
Expand Down
4 changes: 2 additions & 2 deletions jd/generate-types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const config = require('../src/config.json');
const path = require('path');
const fs = require('fs-extra');
let importStr = ``;
let importStr = `import Locale from '../packages/locale';\n`;
const packages = [];
config.nav.map((item) => {
item.packages.forEach((element) => {
Expand All @@ -12,7 +12,7 @@ config.nav.map((item) => {
});
});
let installFunction = `
export { ${packages.join(',')} };`;
export { Locale,${packages.join(',')} };`;
let fileStr = importStr + installFunction;
fs.outputFileSync(path.resolve(__dirname, '../dist/types/nutui.d.ts'), fileStr, 'utf8');
fs.outputFileSync(
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"dev:taro:h5": "npm run createTaroConfig && npm run checked:taro:vue && cd src/sites/mobile-taro/vue/ && npm run dev:h5",
"build:site": "npm run checked && vite build",
"build:site:oss": "npm run checked && vite build --base=/nutui/3x/",
"build": "npm run checked && vite build --config vite.config.build.ts && vite build --config vite.config.build.disperse.ts && npm run generate:types && npm run generate:themes && vite build --config vite.config.build.css.ts",
"build:taro:vue": "npm run checked:taro:vue && vite build --config vite.config.build.taro.vue.ts && vite build --config vite.config.build.taro.vue.disperse.ts && npm run generate:types:taro && npm run generate:themes && vite build --config vite.config.build.css.ts",
"build": "npm run checked && vite build --config vite.config.build.ts && vite build --config vite.config.build.disperse.ts && npm run generate:types && npm run generate:themes && vite build --config vite.config.build.css.ts && vite build --config vite.config.build.locale.ts",
"build:taro:vue": "npm run checked:taro:vue && vite build --config vite.config.build.taro.vue.ts && vite build --config vite.config.build.taro.vue.disperse.ts && npm run generate:types:taro && npm run generate:themes && vite build --config vite.config.build.css.ts && vite build --config vite.config.build.locale.ts",
"serve": "vite preview",
"upload": "yarn build:site:oss && node ./jd/upload.js",
"add": "node jd/createComponentMode.js",
Expand Down
22 changes: 12 additions & 10 deletions src/packages/locale/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { ref, reactive } from 'vue';
import lang from './lang/zh-CN';
import defaultLang from './lang/zh-CN';
import enUSLang from './lang/en-US';
import ZhCNLang from './lang/zh-CN';
import EnUSLang from './lang/en-US';
// 组件默认语言设置
const currentLang = ref('zh-CN');

type lang = Record<string, any>;
const langs = reactive<lang>({
'zh-CN': defaultLang,
'en-US': enUSLang
export type Lang = Record<string, any>;
const langs = reactive<Lang>({
'zh-CN': new ZhCNLang(),
'en-US': new EnUSLang()
});
export class Locale {
static languages(): lang {
static languages(): Lang {
return langs[currentLang.value];
}
static use(lang: string) {
static use(lang: string, newLanguages?: any) {
if (newLanguages) {
langs[lang] = new newLanguages();
}
currentLang.value = lang;
}
}
Expand Down
47 changes: 24 additions & 23 deletions src/packages/locale/lang/baseLang.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,82 @@
export interface BaseLang {
save: string;
confirm: string;
cancel: string;
done: string;
noData: string;
placeholder: string;
select: string;
video: {
export abstract class BaseLang {
abstract save: string;
abstract confirm: string;
abstract cancel: string;
abstract done: string;
abstract noData: string;
abstract placeholder: string;
abstract select: string;
abstract video: {
errorTip: string;
clickRetry: string;
};
fixednav: {
abstract fixednav: {
activeText: string;
unActiveText: string;
};
pagination: {
abstract pagination: {
prev: string;
next: string;
};
calendaritem: {
abstract calendaritem: {
weekdays: Array<string>;
end: string;
start: string;
title: string;
monthTitle: Function;
today: string;
};
shortpassword: {
abstract shortpassword: {
title: string;
desc: string;
tips: string;
};
uploader: {
abstract uploader: {
ready: string;
readyUpload: string;
waitingUpload: string;
uploading: string;
success: string;
error: string;
};
countdown: {
abstract countdown: {
day: string;
hour: string;
minute: string;
second: string;
};
address: {
abstract address: {
selectRegion: string;
deliveryTo: string;
chooseAnotherAddress: string;
};
signature: {
abstract signature: {
reSign: string;
unSupportTpl: string;
};
ecard: {
abstract ecard: {
chooseText: string;
otherValueText: string;
placeholder: string;
};
timeselect: {
abstract timeselect: {
pickupTime: string;
};
sku: {
abstract sku: {
buyNow: string;
buyNumber: string;
addToCard: string;
};
skuheader: {
abstract skuheader: {
skuId: string;
};
addresslist: {
abstract addresslist: {
addAddress: string;
};
comment: {
abstract comment: {
complaintsText: string;
additionalReview: Function;
additionalImages: Function;
};
}
export default BaseLang;
80 changes: 40 additions & 40 deletions src/packages/locale/lang/en-US.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
import { BaseLang } from './baseLang';
const lang: BaseLang = {
save: 'Save',
confirm: 'Confirm',
cancel: 'Cancel',
done: 'Done',
noData: 'No Data',
placeholder: 'Placeholder',
select: 'Select',
video: {
class Lang extends BaseLang {
save = 'Save';
confirm = 'Confirm';
cancel = 'Cancel';
done = 'Done';
noData = 'No Data';
placeholder = 'Placeholder';
select = 'Select';
video = {
errorTip: 'Error Tip',
clickRetry: 'Click Retry'
},
fixednav: {
};
fixednav = {
activeText: 'Close Nav',
unActiveText: 'Open Nav'
},
pagination: {
};
pagination = {
prev: 'Previous',
next: 'Next'
},
calendaritem: {
};
calendaritem = {
weekdays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
end: 'End',
start: 'Start',
title: 'Calendar',
monthTitle: (year: number, month: number) => `${year}/${month}`,
today: 'Today'
},
shortpassword: {
};
shortpassword = {
title: 'Please input a password',
desc: 'Verify',
tips: 'Forget password'
},
uploader: {
};
uploader = {
ready: 'Ready',
readyUpload: 'Ready to upload',
waitingUpload: 'Waiting for upload',
uploading: 'Uploading',
success: 'Upload successful',
error: 'Upload failed'
},
countdown: {
};
countdown = {
day: ' Day ',
hour: ' Hour ',
minute: ' Minute ',
second: ' Second '
},
address: {
};
address = {
selectRegion: 'Select Region',
deliveryTo: 'Delivery To',
chooseAnotherAddress: 'Choose Another Address'
},
signature: {
};
signature = {
reSign: 'Re Sign',
unSupportTpl: `Sorry, the current browser doesn't support canvas, so we can't use this control!`
},
ecard: {
};
ecard = {
chooseText: 'Select',
otherValueText: 'Other Value',
placeholder: 'Placeholder'
},
timeselect: {
};
timeselect = {
pickupTime: 'Pickup Time'
},
sku: {
};
sku = {
buyNow: 'Buy Now',
buyNumber: 'Buy Number',
addToCard: 'Add to Card'
},
skuheader: {
};
skuheader = {
skuId: 'Sku Number'
},
addresslist: {
};
addresslist = {
addAddress: 'Add New Address'
},
comment: {
};
comment = {
complaintsText: 'I have a complaint',
additionalReview: (day: number) => `Review after ${day} days of purchase`,
additionalImages: (length: number) => `There are ${length} follow-up comments`
}
};
export default lang;
};
}
export default Lang;
Loading

0 comments on commit f50f8fb

Please sign in to comment.