Skip to content

Commit b671127

Browse files
committed
Init
0 parents  commit b671127

11 files changed

+231
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- '6'

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict'
2+
3+
exports.Provider = require('./provider')
4+
exports.Translate = require('./translate')

license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Andrew Joslin <[email protected]> (ajoslin.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "react-nanotranslate",
3+
"main": "index.js",
4+
"version": "0.0.0",
5+
"description": "React context provider and component for nanotranslate",
6+
"license": "MIT",
7+
"repository": "ajoslin/react-nanotranslate",
8+
"author": {
9+
"name": "Andrew Joslin",
10+
"email": "[email protected]",
11+
"url": "ajoslin.com"
12+
},
13+
"scripts": {
14+
"test": "standard && tape test.js"
15+
},
16+
"keywords": [
17+
"react",
18+
"nanotranslate",
19+
"context",
20+
"localization",
21+
"tiny"
22+
],
23+
"devDependencies": {
24+
"enzyme": "^2.8.0",
25+
"jsdom": "^9.12.0",
26+
"nanotranslate": "^1.0.3",
27+
"react": "^15.4.2",
28+
"react-addons-test-utils": "^15.4.2",
29+
"react-hyperscript": "^3.0.0",
30+
"standard": "^7.0.0",
31+
"tape": "^4.0.0"
32+
},
33+
"peerDependencies": {
34+
"react": ">=15.0.0",
35+
"nanotranslate": ">=1.0.0"
36+
}
37+
}

provider.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
var React = require('react')
4+
var nanotranslate = require('nanotranslate')
5+
6+
module.exports = Provider
7+
8+
Provider.prototype = Object.create(React.Component.prototype)
9+
10+
function Provider (props) {
11+
React.Component.call(this, props)
12+
this.onUpdate(props.dictionary)
13+
}
14+
15+
Provider.childContextTypes = {
16+
translate: React.PropTypes.func.isRequired
17+
}
18+
19+
Provider.propTypes = {
20+
dictionary: React.PropTypes.shape({
21+
id: React.PropTypes.string.isRequired,
22+
values: React.PropTypes.object.isRequired
23+
})
24+
}
25+
26+
Provider.prototype.getChildContext = function () {
27+
return {
28+
translate: this.translate
29+
}
30+
}
31+
32+
Provider.prototype.componentWillReceiveProps = function (nextProps) {
33+
if (nextProps.dictionary === this.props.dictionary) return
34+
this.onUpdate(nextProps.dictionary)
35+
}
36+
37+
Provider.prototype.onUpdate = function (dictionary) {
38+
this.translate = nanotranslate(dictionary)
39+
}
40+
41+
Provider.prototype.render = function () {
42+
return React.Children.only(this.props.children)
43+
}

readme.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# react-nanotranslate [![Build Status](https://travis-ci.org/ajoslin/react-nanotranslate.svg?branch=master)](https://travis-ci.org/ajoslin/react-nanotranslate)
2+
3+
> React context provider and component for nanotranslate
4+
5+
6+
## Install
7+
8+
```
9+
$ npm install --save react-nanotranslate
10+
```
11+
12+
13+
## Usage
14+
15+
```js
16+
var reactNanotranslate = require('react-nanotranslate')
17+
18+
reactNanotranslate('input')
19+
//=> output
20+
```
21+
22+
## API
23+
24+
#### `reactNanotranslate(input, [options])` -> `output`
25+
26+
##### input
27+
28+
*Required*
29+
Type: `string`
30+
31+
Lorem ipsum.
32+
33+
##### options
34+
35+
###### foo
36+
37+
Type: `boolean`
38+
Default: `false`
39+
40+
Lorem ipsum.
41+
42+
43+
## License
44+
45+
MIT © [Andrew Joslin](http://ajoslin.com)

test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
global.document = require('jsdom').jsdom('<body></body>')
4+
global.window = document.defaultView
5+
global.navigator = window.navigator
6+
7+
var test = require('tape')
8+
var h = require('react-hyperscript')
9+
var Provider = require('./provider')
10+
var Translate = require('./translate')
11+
var {mount} = require('enzyme')
12+
13+
var dictionary = {
14+
id: 'en_US',
15+
values: {
16+
HELLO: 'Hello, {{name}}.'
17+
}
18+
}
19+
20+
test('it works', function (t) {
21+
var wrapper = mount(
22+
h(Provider, {dictionary}, [
23+
h(Translate, {
24+
id: 'HELLO',
25+
data: {name: 'robot'}
26+
})
27+
])
28+
)
29+
30+
t.equal(wrapper.html(), '<span>Hello, robot.</span>')
31+
t.end()
32+
})
33+
34+
test('fail without context', function (t) {
35+
t.throws(
36+
() => mount(h(Translate, {
37+
id: 'HELLO',
38+
data: {name: 'robot'}
39+
}))
40+
)
41+
t.end()
42+
})

translate.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
var React = require('react')
4+
5+
module.exports = Translate
6+
7+
Translate.contextTypes = {
8+
translate: React.PropTypes.func.isRequired
9+
}
10+
11+
Translate.propTypes = {
12+
id: React.PropTypes.string.isRequired,
13+
data: React.PropTypes.object
14+
}
15+
16+
function Translate (props, context) {
17+
return React.createElement(
18+
'span',
19+
null,
20+
context.translate(props.id, props.data)
21+
)
22+
}

0 commit comments

Comments
 (0)