Skip to content

Commit

Permalink
feat(i18n): complete i18n component
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuozhili committed Jul 1, 2020
1 parent ef849ab commit 0207b7b
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bigbear-ui",
"version": "0.2.5",
"version": "0.2.6",
"description": "Neumorphic component library for React;基于React制作的拟物风格组件库",
"keywords": [
"component",
Expand Down
4 changes: 2 additions & 2 deletions plop-template/componentStories.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meta, Story, Props ,Preview } from '@storybook/addon-docs/blocks';
import {{name}} from './{{lowerCase name}}';

<Meta title='DISPLAY |{{name}} ' component={{{name}}}/>
<Meta title='DISPLAY |{{name}} ' component={ {{name}}}/>

<br/>

Expand All @@ -21,4 +21,4 @@ import {{name}} from './{{lowerCase name}}';

## 属性详情

<Props of={{{name}}}/>
<Props of={ {{name}} }/>
1 change: 1 addition & 0 deletions src/components/I18n/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
49 changes: 49 additions & 0 deletions src/components/I18n/i18n.example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useContext } from "react";
import I18n, { Context } from "./i18n";
import Button from "../Button";
import Badge from "../Badge";

const zh = {
language: "语言",
apple: "苹果"
};
const en = {
language: "english language",
apple: "english apple"
};
const combinedLibrary = {
zh,
en
};
export default function I18nExampleApp() {
return (
<I18n defaultLang={"zh"} library={combinedLibrary}>
<Child></Child>
</I18n>
);
}
function Child() {
let { state, toggle } = useContext(Context);
return (
<div>
<Badge count={state.language}></Badge>
<Badge count={state.apple}></Badge>
<div>
<Button
onClick={() => {
toggle("zh");
}}
>
切换中文
</Button>
</div>
<Button
onClick={() => {
toggle("en");
}}
>
切换英文
</Button>
</div>
);
}
85 changes: 85 additions & 0 deletions src/components/I18n/i18n.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Meta, Story, Props ,Preview } from '@storybook/addon-docs/blocks';
import I18n from './i18n';
import I18nExampleApp from './i18n.example'

<Meta title='REACTION |I18n 国际化' component={I18n} />

<br/>

# I18n 国际化
<br/>

## 基本使用

传入默认语言和library即可。
把组件放在根节点上。
需要使用国际化地方直接useContext即可。
类组件使用Context.Consumer拿到Context上的state。

默认名需要等于combinedLibrary的key名。
进行切换时,传入的也是combinedLibrary的键名。





```jsx
const zh = {
language: "语言",
apple: "苹果"
};
const en = {
language: "english language",
apple: "english apple"
};
const combinedLibrary = {
zh,
en
};
export default function I18nExampleApp() {
return (
<I18n defaultLang={"zh"} library={combinedLibrary}>
<Child></Child>
</I18n>
);
}
function Child() {
let { state, toggle } = useContext(Context);
return (
<div>
<Badge count={state.language}></Badge>
<Badge count={state.apple}></Badge>
<div>
<Button
onClick={() => {
toggle("zh");
}}
>
切换中文
</Button>
</div>
<Button
onClick={() => {
toggle("en");
}}
>
切换英文
</Button>
</div>
);
}
```


<Preview>
<Story name='default'>
<I18nExampleApp ></I18nExampleApp>
</Story>
</Preview>




## 属性详情

<Props of={I18n}/>
38 changes: 38 additions & 0 deletions src/components/I18n/i18n.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { PropsWithChildren, useState, useMemo } from "react";

export interface I18nDataSource {
[key: string]: string;
}

export interface I18nCombinedSource {
[key: string]: I18nDataSource;
}

export interface contextType {
state: I18nDataSource;
toggle: (str: string) => void;
}

export let Context = React.createContext<contextType>({ state: {}, toggle: () => {} });

export interface I18nProps {
/** 默认语言*/
defaultLang: keyof I18nDataSource;
/** 语言库 */
library: I18nCombinedSource;
}

function I18n(props: PropsWithChildren<I18nProps>) {
const { defaultLang, library, children } = props;
const [state, setState] = useState<I18nDataSource>(library[defaultLang] || {});
let toggle = useMemo(() => {
return function(str: keyof I18nDataSource) {
if (library[str]) {
setState(library[str]);
}
};
}, [library]);
return <Context.Provider value={{ state, toggle }}>{children}</Context.Provider>;
}

export default I18n;
3 changes: 3 additions & 0 deletions src/components/I18n/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import I18n from "./i18n";

export default I18n;

0 comments on commit 0207b7b

Please sign in to comment.