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

add internationalization tooling #4233

Merged
merged 53 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
fc55554
add i18next
outofambit Jan 5, 2020
35b1dce
add i18next scanner grunt task
outofambit Jan 5, 2020
c651a82
Update Gruntfile.js
outofambit Jan 5, 2020
d3466c3
Create internationalization.js
outofambit Jan 6, 2020
a921d10
testt drive
outofambit Jan 6, 2020
aa8a42f
Revert "Update Gruntfile.js"
outofambit Jan 6, 2020
2d26d15
Revert "add i18next scanner grunt task"
outofambit Jan 6, 2020
2e45438
remove i18next-scanner
outofambit Jan 6, 2020
e985c1d
add babel-plugin-i18next-extract
outofambit Jan 6, 2020
4b4627b
add config for i18next-extract
outofambit Jan 6, 2020
501c64a
dummy translator
outofambit Jan 6, 2020
ebf9233
begin translation key extraction
outofambit Jan 6, 2020
98bb298
better i18n keys
outofambit Jan 6, 2020
2c93b35
less config needed
outofambit Jan 6, 2020
a804820
reorder
outofambit Jan 6, 2020
70ce0e2
Update translation.json
outofambit Jan 6, 2020
b355fc7
add nesting config
outofambit Jan 6, 2020
30173f1
Update translation.json
outofambit Jan 6, 2020
ce49e06
different syntax for file load errors
outofambit Jan 6, 2020
3b873ae
setup language detector
outofambit Jan 6, 2020
e505274
create translations index
outofambit Jan 6, 2020
d4b73cf
wire up translation loading
outofambit Jan 6, 2020
28160c3
Update translation.json
outofambit Jan 6, 2020
a5bb89b
Update internationalization.js
outofambit Jan 6, 2020
7facf08
simplify
outofambit Jan 6, 2020
f7a945c
remove acorn
outofambit Jan 6, 2020
ba5d09c
cleanup
outofambit Jan 6, 2020
e6ce5e9
arguments not args
outofambit Jan 6, 2020
e1e21c5
Update internationalization.js
outofambit Jan 6, 2020
1643935
rework i18next init
outofambit Feb 17, 2020
dd9d2c8
force loading translations before we init p5
outofambit Feb 17, 2020
210f428
fix translator import
outofambit Feb 19, 2020
a63cd45
expand i18n to fes log prefix
outofambit Feb 19, 2020
50ce2bd
add test
outofambit Feb 19, 2020
2dfa5ce
add autopolay error to i18n keys
outofambit Feb 24, 2020
68470e7
move server link into code, out of translations
outofambit Feb 24, 2020
6804957
more inline documentation
outofambit Feb 24, 2020
80c8dc9
Update internationalization.js
outofambit Feb 24, 2020
89df8cc
don't split up url string
outofambit Feb 24, 2020
51e8b8b
fix up autoplay message
outofambit Feb 24, 2020
4d47744
i18n more fes messages
outofambit Feb 24, 2020
03c3c28
add asterisk to pre
outofambit Feb 24, 2020
0ba6e0f
move file load error strings under fes key
outofambit Feb 24, 2020
92f59b1
exclude translations from mini build
outofambit Feb 24, 2020
68ea053
add initial stub translator fn
outofambit Feb 24, 2020
10741fa
flowers 🌸
outofambit Feb 25, 2020
9097ca3
add i18n contributor guide
outofambit Feb 25, 2020
c5a1e3b
another note
outofambit Feb 25, 2020
8d2cf3d
add autoplay message en español
outofambit Feb 25, 2020
286372f
tweak
outofambit Feb 25, 2020
7447e2d
fix typeof check
outofambit Feb 25, 2020
288e55e
disable interpolation and language list check
outofambit Feb 25, 2020
fbc8539
Merge branch 'master' into i18next
lmccart Feb 28, 2020
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
89 changes: 89 additions & 0 deletions contributor_docs/internationalization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# 🌐 Internationalization

[Internationalization](https://developer.mozilla.org/docs/Glossary/Internationalization_and_localization) (sometimes abbreviated "i18n") refers to supporting multiple languages in a software project. This often means maintaining translations of text strings used in the project and letting users choose which translation they receive (or detecting it from their browser settings).

p5.js uses internationalization in a bunch of areas (our contributor docs, [the official website](https://p5js.org), the reference, etc). We're expanding to include the console output of p5.js (which is primarily developer-facing error messages) in our internationalization efforts.

## Tooling

We've integrated [i18next](https://i18next.com) into the codebase. Currently we only use it in the un-minified build of p5.js. `p5.min.js` only includes the outer layer of our internationalization code and does not use it.

### Setup

We set up our i18next integration in `src/core/internationalization.js` and the translations are in `translations/`.

We set up the translation engine and autodetect the user's language from their browser settings before the p5 sketch is initialized. This ensures we can use translations for any errors we encounter in sketch `setup()` and `preload()`.

(If we encounter any errors in language autodetection, we fall back to English.)

#### No translations in `p5.min.js`

There is specific logic in the browserify build task and `src/core/init.js` to avoid loading or setting up translations in the minified build. Adding translations does not increase the size of the minified build.

### Using translations

To use translations include this line at the top of the file.

```js
import { translator } from './internationalization';
```

### Simple messages

Without internationalization you might log a message with the text inline.

```js
console.log('Loading your sketch right now!')
```

Instead, you use `translator`:

```js
console.log(translator('sketch.loading'))
```

This tells the translator to get the "`sketch.loading`" message in whatever language we've detected that the user prefers.

#### Dynamic messages

You can also inject variables into a translated message. For example,

```js
console.log('I couldnt find ' + file.name + '. Are you sure it's there?')
```

would become something like

```js
console.log(translator('fileLoading.notFound', { fileName: file.name }))
```

Translations like this expect the variables it uses to have a certain name, so make sure you use that. Check a translation file (look in translations/{YOUR_LANGUAGE}/) to see what the variable name is. You'll find the translation under the object path in the translation key.

"`fileLoading.notFound`" would be found at

```json
{
"fileLoading": {
"notFound": "I couldnt find {{fileName}}. Are you sure it's there?"
}
}
```

Variables are framed in "`{{` `}}`".

### Modifying translations

Simply open `translations/{YOUR_LANGUAGE}/translation.json`, find the translation with the key (like in the example just above) and edit away!

### Adding translations for new languages

The easiest way to do this is to add your language code (like "de" for German, "it" for Italian, etc) to the [locales list](/~https://github.com/processing/p5.js/blob/84bc1f92c89786f48e5d6fd1045feb649b932eea/package.json#L111-L114) in `package.json` and then from the terminal run '$ npm run build'.

This will generate you a fresh translations file in `translations/{LANGUAGE_CODE}/`! Now you can begin populating it with your fresh translations! 🥖

You'll also need to add an entry for it in `translations/index.js`. You can follow the pattern used in that file for `en` and `es`.

### Further Reading

See the [i18next translation function documentation](https://www.i18next.com/translation-function/essentials). Everything documented above is just a subset of their functionality.
Loading