-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update README also add examples for React and Vue
- Loading branch information
Ken Berkeley
committed
Jan 15, 2018
1 parent
1ef807d
commit 3d4f386
Showing
10 changed files
with
195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import App from './components/App' | ||
|
||
ReactDOM.render( | ||
<App />, | ||
document.getElementById('app') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { Component } from 'react' | ||
import LangSelect from './LangSelect' | ||
import t from '../../t' | ||
|
||
class App extends Component { | ||
|
||
componentDidMount () { | ||
console.log( | ||
t( | ||
'欢迎来到中国', | ||
'Welcome to the US', | ||
'ようこそ日本へ' | ||
) | ||
) | ||
} | ||
|
||
render () { | ||
return ( | ||
<div> | ||
<LangSelect /> | ||
|
||
{ t( | ||
'你好,世界', | ||
'Hello, world', | ||
'こんにちは、世界' | ||
) } | ||
</div> | ||
) | ||
} | ||
|
||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { Component } from 'react' | ||
import Cookie from 'js-cookie' | ||
import { locale, locales } from '../../t' | ||
|
||
class LangSelect extends Component { | ||
|
||
constructor () { | ||
super() | ||
this.state = { locale } | ||
this.handleChange = this.handleChange.bind(this) | ||
} | ||
|
||
handleChange (e) { | ||
Cookie.set('locale', e.target.value) | ||
location.reload() | ||
} | ||
|
||
render () { | ||
return ( | ||
<select value={this.state.locale} onChange={this.handleChange}> | ||
{ | ||
locales.map(lang => ( | ||
<option key={lang} value={lang}>{ lang }</option> | ||
)) | ||
} | ||
</select> | ||
) | ||
} | ||
|
||
} | ||
|
||
export default LangSelect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Cookie from 'js-cookie' | ||
import i18n from '../' | ||
|
||
export const locale = Cookie.get('locale') || 'en' | ||
export const locales = ['cn', 'en', 'ja'] | ||
|
||
const t = i18n({ | ||
locale, | ||
locales | ||
}) | ||
export default t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Vue from 'vue' | ||
import App from './components/App' | ||
import t from '../t' | ||
|
||
Vue.prototype.$t = t | ||
|
||
new Vue({ | ||
el: '#app', | ||
render: h => h(App) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<template> | ||
<div> | ||
<lang-select /> | ||
|
||
{{ $t( | ||
'你好,世界', | ||
'Hello, world', | ||
'こんにちは、世界' | ||
) }} | ||
</div> | ||
</template> | ||
<script> | ||
import LangSelect from './LangSelect' | ||
export default { | ||
components: { LangSelect }, | ||
mounted () { | ||
console.log( | ||
this.$t( | ||
'欢迎来到中国', | ||
'Welcome to the US', | ||
'ようこそ日本へ' | ||
) | ||
) | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<template> | ||
<select v-model="locale"> | ||
<option v-for="lang in locales" :key="lang" :value="lang"> | ||
{{ lang }} | ||
</option> | ||
</select> | ||
</template> | ||
<script> | ||
import Cookie from 'js-cookie' | ||
import { locale, locales } from '../../t' | ||
export default { | ||
data: () => ({ | ||
locale, | ||
locales | ||
}), | ||
watch: { | ||
locale (val) { | ||
Cookie.set('locale', val) | ||
location.reload() | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
{ | ||
"name": "simplest-i18n", | ||
"description": "The simplest universal i18n solution", | ||
"description": "The Simplest Universal i18n Solution", | ||
"author": "Ken Berkeley <[email protected]>", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"main": "i18n.min.js", | ||
"scripts": { | ||
"lint": "standard i18n.js", | ||
"prebuild": "npm run lint", | ||
"build": "uglifyjs i18n.js -c -m -o i18n.min.js", | ||
"pretest": "npm run lint", | ||
"test": "ava test.js" | ||
"test": "ava test.js", | ||
"vue": "poi examples/vue/app.js", | ||
"react": "poi --jsx react examples/react/app.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
|
@@ -30,6 +32,10 @@ | |
], | ||
"devDependencies": { | ||
"ava": "^0.24.0", | ||
"js-cookie": "^2.2.0", | ||
"poi": "^9.6.12", | ||
"react": "^16.2.0", | ||
"react-dom": "^16.2.0", | ||
"standard": "^10.0.3", | ||
"uglify-js": "^3.3.5" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters