Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2 #21

Merged
merged 38 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1d939fc
Update packages, remove redux
Nov 19, 2018
a4767e3
Add simple translate JSX function
Nov 19, 2018
09e72a4
Add translation function
Nov 19, 2018
056b1bf
Add plural translation functions
Nov 19, 2018
17bf62c
Split into multiple components
Nov 19, 2018
97f1c60
Clean up
Nov 19, 2018
a26e674
Add multiline and context translation and more cleanup
Nov 19, 2018
e47574e
Add support for translation options and fix multi-line
Nov 19, 2018
9e00624
Delete old code
Nov 19, 2018
628869d
Fix bug with regex on matching obj data
Nov 19, 2018
5a88135
Update packages
Dec 11, 2018
ddabed6
Callback test
Jan 23, 2019
062359c
Add poc for parsing dom-like elements in translations
Jan 30, 2019
f168a9d
Split parser into own folder
Jan 31, 2019
64d9a80
Messy redux-like store
Jan 31, 2019
6f129f1
Minor cleanups and refactor
Jan 31, 2019
c7e91b5
Split into more components and add withTranslate hoc"
Jan 31, 2019
21fc06d
Update packages and move files to main
Jan 31, 2019
2bb5e88
Update example package
Jan 31, 2019
739b072
Working-ish example project
Feb 1, 2019
da49baf
Move things around, add tests and whitelists
Feb 1, 2019
977f512
Improve documentation
Feb 1, 2019
82e1511
Minor fixes
Feb 1, 2019
aa6b53e
Merge branch 'master' into v2
Feb 1, 2019
622f1c9
Add support for more types
Feb 1, 2019
846ad19
Add type for with translate
Feb 1, 2019
70deeb2
Fix type for withTranslate HoC
Feb 4, 2019
e47d6a1
Simplify the Omit type
Feb 4, 2019
53e9fff
Use Provider instead of contextType to fix hoist issues
Feb 4, 2019
b6b1167
Fix example and other minor things
Feb 4, 2019
58bc9cf
Upgrade packages, remove void tags, add hook
Mar 4, 2019
b5e3768
Use next tag instead of local file
Mar 4, 2019
6a5723a
Remove example dir and add example to README
Aug 8, 2019
3c75329
Fix export script not working with this.props.x
Aug 8, 2019
f40b4f8
Fix translation type
Aug 8, 2019
b711283
Ensure subscriber exists before unsubscribing
Aug 8, 2019
85f5e78
Update snapshots
Aug 8, 2019
0d270bd
Bump next version
Aug 8, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# @connectedcars/react-i18n

Work in progress.

## Todo
- [ ] fuzzy translations
- [ ] improve documentation

## Installation

`npm install @connectedcars/react-i18n`
Expand All @@ -28,6 +22,42 @@ Translate text containing a variable:

`t('This {word} is in a translated sentence.', { word: 'Variable'})`

Translate plurals (`n` is injected into the data object, but can be overwritten):

`tn(count, '{n} time', '{n} times')`

Multiple lines are also supported using template strings (however, **DO NOT** use `${}` variables!):

```
t(`Foo
bar
baz`)
```

*Please note, however, that these translations will not be rendered on multiple lines in HTML!*

Translate with React:

```
// Use `tnx` for plurals with JSX!
tx(`
<p>Hello, <strong>{name}</strong><p>
<p>Today is {day}</p>
`, {
p: content => <p>{content}</p>,
strong: content => <strong>{content}</strong>,
name: 'John Doe',
day: 'Monday'
})
```

*Please note, by default translations are in strict mode which means an error will be thrown on undefined types*
*Please note, it is possible to add a whitelist/default by setting `jsxWhitelist` on the `I18nPovider`.*

You can also add a context to your translations:

`t('Hello', null, 'context here')`

## Import and Export

First you need to set up the import and export scripts in your `package.json`
Expand All @@ -41,6 +71,7 @@ First you need to set up the import and export scripts in your `package.json`

...
```

Then you can run the following commands:
* `npm run export` creates a POT file with the extracted translations in `./locales/template.pot`
* `npm run import` creates a json file containing your all your translations in `./src/translations.json`
Expand All @@ -50,4 +81,4 @@ Then you can run the following commands:
To get output for CI, add the following script:
```
"ci-translation": "i18n-translation-status"
```
```
38 changes: 32 additions & 6 deletions bin/export.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!/usr/bin/env node
const { GettextExtractor, JsExtractors, HtmlExtractors } = require('gettext-extractor')
const { GettextExtractor, JsExtractors } = require('gettext-extractor')

let extractor = new GettextExtractor()

const content = {
trimWhiteSpace: true,
preserveIndentation: false
}

extractor
.createJsParser([
JsExtractors.callExpression(['t', 'props.t', '[this].context.t'], {
arguments: {
text: 0,
// data
context: 2
}
context: 2,
},
content,
}),

JsExtractors.callExpression(['tn', 'props.tn', '[this].context.tn'], {
Expand All @@ -19,13 +25,33 @@ extractor
text: 1,
textPlural: 2,
// data
context: 4
}
context: 4,
},
content,
}),

JsExtractors.callExpression(['tx', 'props.tx', '[this].context.tx'], {
arguments: {
text: 0,
// data
context: 2,
},
content,
}),

JsExtractors.callExpression(['tnx', 'props.tnx', '[this].context.tnx'], {
arguments: {
// count
text: 1,
textPlural: 2,
// data
context: 4,
},
content,
})
])
.parseFilesGlob('./src/**/*.@(ts|js|tsx|jsx)')

extractor.savePotFile('./locales/template.pot')

extractor.printStats()

24 changes: 23 additions & 1 deletion example/.gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
/node_modules
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
48 changes: 39 additions & 9 deletions example/README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
# Example App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

This is a demo app showing how to use the `ReduxProvider` with `create-react-app`.
## Available Scripts

## Getting started
In the project directory, you can run:

```
$ cd example
$ npm install
$ npm start
```
### `npm start`

If your browser doesn't automatically open, then go to `localhost:3000`.
Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
25 changes: 10 additions & 15 deletions example/locales/da.po
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: da\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.5\n"
"X-Generator: Poedit 2.0.6\n"
"Last-Translator: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: da\n"

#: src/App.tsx:16
msgid "{n} day ago"
msgid_plural "{n} days ago"
msgstr[0] "{n} dag siden"
msgstr[1] "{n} dage siden"

#: src/App.tsx:30
msgid "Current language:"
msgstr "Valgt sprog:"

#: src/App.tsx:22
#: src/index.tsx:21
#: src/index.tsx:38
msgid "Hello {name}"
msgstr "Hej {name}"

#: src/index.tsx:45
msgid "Set language to <lang />"
msgstr "Skift sprog til <lang />"
15 changes: 5 additions & 10 deletions example/locales/template.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"

#: src/App.tsx:17
msgid "{n} day ago"
msgid_plural "{n} days ago"
msgstr[0] ""
msgstr[1] ""

#: src/App.tsx:30
msgid "Current language:"
#: src/index.tsx:21
#: src/index.tsx:38
msgid "Hello {name}"
msgstr ""

#: src/App.tsx:23
msgid "Hello {name}"
#: src/index.tsx:45
msgid "Set language to <lang />"
msgstr ""
Loading