feat: add new i18n package

This commit is contained in:
lawvs
2022-09-06 14:25:15 +08:00
parent c734480f38
commit 3b06d4bff5
14 changed files with 307 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
{
"presets": [
[
"@nrwl/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": []
}
+18
View File
@@ -0,0 +1,18 @@
{
"extends": ["plugin:@nrwl/nx/react", "../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
+45
View File
@@ -0,0 +1,45 @@
# i18n
## Usages
```tsx
import { useTranslation } from '@toeverything/datasource/i18n';
// base.json
// {
// 'Text': 'some text',
// 'Switch to language': 'Switch to {language}',
// };
const App = () => {
const { t } = useTranslation();
const changeLanguage = (language: string) => {
i18n.changeLanguage(language);
};
return (
<div>
<div>{t('Text')}</div>
<button onClick={() => changeLanguage('en')}>
{t('Switch to language', { language: 'en' })}
</button>
<button onClick={() => changeLanguage('zh-Hans')}>
{t('Switch to language', { language: 'zh-Hans' })}
</button>
</div>
);
};
```
## TODO
- [ ] language detection
- [ ] storage
## References
- [i18next](https://www.i18next.com/)
- [react-i18next](https://react.i18next.com/)
- [Tolgee](https://tolgee.io/docs/)
+10
View File
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'datasource-i18n',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/libs/datasource/i18n',
};
+9
View File
@@ -0,0 +1,9 @@
{
"name": "@toeverything/datasource/i18n",
"version": "0.0.1",
"dependencies": {
"i18next": "^21.9.1",
"jotai": "^1.8.1",
"react-i18next": "^11.18.4"
}
}
+45
View File
@@ -0,0 +1,45 @@
{
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/datasource/i18n/src",
"projectType": "library",
"tags": ["datasource"],
"targets": {
"build": {
"executor": "@nrwl/web:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/datasource/i18n",
"tsConfig": "libs/datasource/i18n/tsconfig.lib.json",
"project": "libs/datasource/i18n/package.json",
"entryFile": "libs/datasource/i18n/src/index.ts",
"external": ["react/jsx-runtime"],
"rollupConfig": "@nrwl/react/plugins/bundle-rollup",
"compiler": "babel",
"assets": [
{
"glob": "libs/datasource/i18n/README.md",
"input": ".",
"output": "."
}
]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/datasource/i18n/**/*.{ts,tsx,js,jsx}"
]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/datasource/i18n"],
"options": {
"jestConfig": "libs/datasource/i18n/jest.config.ts",
"passWithNoTests": true
}
}
}
}
+24
View File
@@ -0,0 +1,24 @@
{
"Sync to Disk": "Sync to Disk",
"Share": "Share",
"WarningTips": {
"IsNotfsApiSupported": "Welcome to the AFFiNE demo. To begin saving changes you can SYNC DATA TO DISK with the latest version of Chromium based browser like Chrome/Edge",
"IsNotLocalWorkspace": "Welcome to the AFFiNE demo. To begin saving changes you can SYNC TO DISK.",
"DoNotStore": "AFFiNE is under active development and the current version is UNSTABLE. Please DO NOT store information or data"
},
"Layout": "Layout",
"Comment": "Comment",
"Settings": "Settings",
"ComingSoon": "Layout Settings Coming Soon...",
"Duplicate Page": "Duplicate Page",
"Copy Page Link": "Copy Page Link",
"Language": "Language",
"Clear Workspace": "Clear Workspace",
"Export As Markdown": "Export As Markdown",
"Export As HTML": "Export As HTML",
"Export As PDF (Unsupported)": "Export As PDF (Unsupported)",
"Import Workspace": "Import Workspace",
"Export Workspace": "Export Workspace",
"Last edited by": "Last edited by {{name}}",
"Logout": "Logout"
}
+24
View File
@@ -0,0 +1,24 @@
{
"Sync to Disk": "同步到磁盘",
"Share": "分享",
"WarningTips": {
"IsNotfsApiSupported": "欢迎来到AFFiNE 的演示界面。您可以使用最新版本的基于Chrome的浏览器(如Chrome/Edge)将数据同步到磁盘来进行保存",
"IsNotLocalWorkspace": "欢迎来到AFFiNE 的演示界面,您可以同步到磁盘来进行保存操作。",
"DoNotStore": "AFFINE 正在积极开发中,当前版本不稳定。请不要存储信息或数据。"
},
"ComingSoon": "布局设置即将到来",
"Layout": "布局",
"Comment": "评论",
"Settings": "设置",
"Duplicate Page": "复制页面",
"Copy Page Link": "复制页面链接",
"Language": "当前语言",
"Clear Workspace": "清空工作区域",
"Export As Markdown": "导出 markdown",
"Export As HTML": "导出 HTML",
"Export As PDF (Unsupported)": "导出 PDF (暂不支持)",
"Import Workspace": "导入 Workspace",
"Export Workspace": "导出 Workspace",
"Last edited by": "最后编辑者为 {{name}}",
"Logout": "退出登录"
}
+47
View File
@@ -0,0 +1,47 @@
import i18next, { Resource } from 'i18next';
import {
I18nextProvider,
initReactI18next,
useTranslation,
} from 'react-i18next';
import en_US from '../resources/en.json';
import zh_CN from '../resources/zh.json';
// See https://react.i18next.com/latest/typescript
declare module 'react-i18next' {
interface CustomTypeOptions {
// custom namespace type if you changed it
// defaultNS: 'ns1';
// custom resources type
resources: {
en: typeof en_US;
};
}
}
const LOCALES = [
{ value: 'en', text: 'English', res: en_US },
{ value: 'zh', text: '简体中文', res: zh_CN },
] as const;
const resources = LOCALES.reduce<Resource>(
(acc, { value, res }) => ({ ...acc, [value]: { translation: res } }),
{}
);
const i18n = i18next.createInstance();
i18n.use(initReactI18next).init({
// TODO auto detect
lng: LOCALES[0].value,
fallbackLng: LOCALES[0].value,
debug: process.env['NODE_ENV'] === 'development',
resources,
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
});
const I18nProvider = I18nextProvider;
export { i18n, useTranslation, I18nProvider, LOCALES };
+26
View File
@@ -0,0 +1,26 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictNullChecks": true,
"importsNotUsedAsValues": "error",
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
+23
View File
@@ -0,0 +1,23 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"types": ["node"]
},
"files": [
"../../../node_modules/@nrwl/react/typings/cssmodule.d.ts",
"../../../node_modules/@nrwl/react/typings/image.d.ts"
],
"exclude": [
"jest.config.ts",
"**/*.spec.ts",
"**/*.test.ts",
"**/*.spec.tsx",
"**/*.test.tsx",
"**/*.spec.js",
"**/*.test.js",
"**/*.spec.jsx",
"**/*.test.jsx"
],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
+20
View File
@@ -0,0 +1,20 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
+3
View File
@@ -74,6 +74,9 @@
"@toeverything/datasource/feature-flags": [
"libs/datasource/feature-flags/src/index.ts"
],
"@toeverything/datasource/i18n": [
"libs/datasource/i18n/src/index.ts"
],
"@toeverything/datasource/jwt": [
"libs/datasource/jwt/src/index.ts"
],
+1
View File
@@ -21,6 +21,7 @@
"datasource-commands": "libs/datasource/commands",
"datasource-db-service": "libs/datasource/db-service",
"datasource-feature-flags": "libs/datasource/feature-flags",
"datasource-i18n": "libs/datasource/i18n",
"datasource-jwt": "libs/datasource/jwt",
"datasource-jwt-rpc": "libs/datasource/jwt-rpc",
"datasource-remote-kv": "libs/datasource/remote-kv",