0.6.0 • Published 1 year ago
mobx-i18n v0.6.0
MobX i18n
Responsive Translation utility based on TypeScript & MobX
Features
- Type hinting of Text keys
- Lambda Expression values
- Space utility for CJK & other characters
- Responsive re-rendering
- Async loading of Language packages
- support HTTP protocol for Server-side rendering
- support BOM/DOM language API for Client-side rendering
- Speech Synthesis API for Text-to-Speech (TTS)
Versions
| SemVer | branch | status | ES decorator | MobX | 
|---|---|---|---|---|
| >=0.5.0 | main | ✅developing | stage-3 | >=6.11 | 
| <0.5.0 | master | ❌deprecated | stage-2 | >=4 <6.11 | 
Text internationalization (React/Next.js example)
Original from https://github.com/kaiyuanshe/kaiyuanshe.github.io
Installation
npm i mobx mobx-react mobx-i18n next-ssr-middlewareConfiguration
tsconfig.json
{
    "compilerOptions": {
        "target": "ES6",
        "useDefineForClassFields": true,
        "experimentalDecorators": false
    }
}Translation
translation/zh-CN.ts
import { textJoin } from 'mobx-i18n';
export default {
    open_source: '开源',
    project: '项目',
    love: ({ a, b }: Record<'a' | 'b', string>) => textJoin(a, '爱', b)
} as const;translation/en-US.ts
import { textJoin } from 'mobx-i18n';
export default {
    open_source: 'Open Source',
    project: 'project',
    love: ({ a, b }: Record<'a' | 'b', string>) => textJoin(a, 'love', b)
} as const;Initialization
model/Translation.ts
export const i18n = new TranslationModel({
    'zh-CN': zhCN,
    'en-US': () => import('../translation/en-US')
});
export const LanguageName: Record<(typeof i18n)['currentLanguage'], string> = {
    'zh-CN': '简体中文',
    'en-US': 'English'
};pages/index.tsx
import { textJoin } from 'mobx-i18n';
import { compose, translator } from 'next-ssr-middleware';
import { Component } from 'react';
import { i18n, LanguageName } from '../model/Translation';
export const getServerSideProps = compose(translator(i18n));
@observer
export default class HomePage extends Component {
    render() {
        const { currentLanguage, t } = i18n;
        return (
            <>
                <select
                    value={currentLanguage}
                    onChange={({ currentTarget: { value } }) =>
                        i18n.changeLanguage(value as typeof currentLanguage)
                    }
                >
                    {Object.entries(LanguageName).map(([code, name]) => (
                        <option key={code} value={code}>
                            {name}
                        </option>
                    ))}
                </select>
                <p>
                    {t('love', {
                        a: '我',
                        b: textJoin(t('open_source'), t('project'))
                    })}
                </p>
            </>
        );
    }
}Text to Speech (WebCell example)
pages/article.tsx
import { component, observer } from 'web-cell';
import { SpeechSynthesisModel, SpeechSynthesisState } from 'mobx-i18n';
@component({ tagName: 'article-page' })
@observer
export class ArticlePage extends HTMLElement {
    storeTTS = new SpeechSynthesisModel();
    toggleSpeaking = () => {
        const { storeTTS } = this;
        if (storeTTS.state !== SpeechSynthesisState.Clear)
            return storeTTS.toggle();
        const text = SpeechSynthesisModel.getReadableText(
            this.querySelector('article')
        );
        storeTTS.speak(text);
    };
    render() {
        const speaking = this.storeTTS.state === SpeechSynthesisState.Speaking;
        return (
            <>
                <button
                    style={{ background: speaking ? 'red' : 'blue' }}
                    onClick={this.toggleSpeaking}
                >
                    {speaking ? '🔇' : '📢'}
                </button>
                <article>
                    <h1>The Four Freedoms</h1>
                    <ol>
                        <li>Freedom of speech and expression</li>
                        <li>Freedom of worship</li>
                        <li>Freedom from want</li>
                        <li>Freedom from fear</li>
                    </ol>
                </article>
            </>
        );
    }
}Inspired by
0.6.0
1 year ago
0.5.0
2 years ago
0.4.2
2 years ago
0.4.1
2 years ago
0.3.15
2 years ago
0.4.0-rc.1
2 years ago
0.4.0-rc.0
2 years ago
0.4.0
2 years ago
0.3.14
3 years ago
0.3.13
3 years ago
0.3.12
3 years ago
0.3.11
3 years ago
0.3.10
3 years ago
0.3.9
3 years ago
0.3.8
3 years ago
0.3.7
3 years ago
0.3.6
3 years ago
0.3.5
3 years ago
0.3.4
3 years ago
0.3.3
3 years ago
0.3.2
3 years ago
0.3.1
3 years ago
0.3.0
3 years ago
