i18n-js 3.7.1 → 4.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +4 -0
- data/.github/FUNDING.yml +3 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +38 -0
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/ruby-tests.yml +61 -0
- data/.gitignore +11 -7
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +12 -495
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +79 -0
- data/Gemfile +3 -0
- data/LICENSE.md +20 -0
- data/README.md +89 -903
- data/Rakefile +10 -20
- data/exe/i18n +5 -0
- data/i18n-js.gemspec +47 -29
- data/lib/guard/i18n-js.rb +78 -0
- data/lib/guard/i18n-js/templates/Guardfile +10 -0
- data/lib/guard/i18n-js/version.rb +13 -0
- data/lib/i18n-js.rb +66 -1
- data/lib/i18n-js/cli.rb +47 -0
- data/lib/i18n-js/cli/command.rb +43 -0
- data/lib/i18n-js/cli/export_command.rb +108 -0
- data/lib/i18n-js/cli/init_command.rb +50 -0
- data/lib/i18n-js/cli/ui.rb +30 -0
- data/lib/i18n-js/cli/version_command.rb +18 -0
- data/lib/i18n-js/listen.rb +80 -0
- data/lib/i18n-js/schema.rb +92 -0
- data/lib/i18n-js/version.rb +5 -0
- metadata +123 -190
- data/.editorconfig +0 -24
- data/.npmignore +0 -27
- data/.travis.yml +0 -39
- data/Appraisals +0 -44
- data/app/assets/javascripts/i18n.js +0 -1092
- data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
- data/app/assets/javascripts/i18n/shims.js +0 -240
- data/app/assets/javascripts/i18n/translations.js +0 -3
- data/gemfiles/i18n_0_6.gemfile +0 -7
- data/gemfiles/i18n_0_7.gemfile +0 -7
- data/gemfiles/i18n_0_8.gemfile +0 -7
- data/gemfiles/i18n_0_9.gemfile +0 -7
- data/gemfiles/i18n_1_0.gemfile +0 -7
- data/gemfiles/i18n_1_1.gemfile +0 -7
- data/gemfiles/i18n_1_2.gemfile +0 -7
- data/gemfiles/i18n_1_3.gemfile +0 -7
- data/gemfiles/i18n_1_4.gemfile +0 -7
- data/gemfiles/i18n_1_5.gemfile +0 -7
- data/gemfiles/i18n_1_6.gemfile +0 -7
- data/gemfiles/i18n_1_7.gemfile +0 -7
- data/gemfiles/i18n_1_8.gemfile +0 -7
- data/lib/i18n/js.rb +0 -259
- data/lib/i18n/js/dependencies.rb +0 -63
- data/lib/i18n/js/engine.rb +0 -87
- data/lib/i18n/js/fallback_locales.rb +0 -70
- data/lib/i18n/js/formatters/base.rb +0 -25
- data/lib/i18n/js/formatters/js.rb +0 -32
- data/lib/i18n/js/formatters/json.rb +0 -13
- data/lib/i18n/js/middleware.rb +0 -82
- data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
- data/lib/i18n/js/segment.rb +0 -80
- data/lib/i18n/js/utils.rb +0 -78
- data/lib/i18n/js/version.rb +0 -7
- data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
- data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
- data/lib/tasks/export.rake +0 -8
- data/package.json +0 -25
- data/spec/fixtures/custom_path.yml +0 -5
- data/spec/fixtures/default.yml +0 -5
- data/spec/fixtures/erb.yml +0 -5
- data/spec/fixtures/except_condition.yml +0 -7
- data/spec/fixtures/js_export_dir_custom.yml +0 -7
- data/spec/fixtures/js_export_dir_none.yml +0 -6
- data/spec/fixtures/js_extend_parent.yml +0 -6
- data/spec/fixtures/js_extend_segment.yml +0 -6
- data/spec/fixtures/js_file_per_locale.yml +0 -7
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
- data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
- data/spec/fixtures/js_file_with_namespace_prefix_and_pretty_print.yml +0 -9
- data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
- data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
- data/spec/fixtures/json_only.yml +0 -18
- data/spec/fixtures/locales.yml +0 -133
- data/spec/fixtures/merge_plurals.yml +0 -6
- data/spec/fixtures/merge_plurals_with_no_overrides.yml +0 -4
- data/spec/fixtures/merge_plurals_with_partial_overrides.yml +0 -4
- data/spec/fixtures/millions.yml +0 -4
- data/spec/fixtures/multiple_conditions.yml +0 -7
- data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
- data/spec/fixtures/multiple_files.yml +0 -7
- data/spec/fixtures/no_config.yml +0 -2
- data/spec/fixtures/no_scope.yml +0 -4
- data/spec/fixtures/simple_scope.yml +0 -5
- data/spec/js/currency.spec.js +0 -62
- data/spec/js/current_locale.spec.js +0 -19
- data/spec/js/dates.spec.js +0 -276
- data/spec/js/defaults.spec.js +0 -31
- data/spec/js/extend.spec.js +0 -110
- data/spec/js/interpolation.spec.js +0 -124
- data/spec/js/jasmine/MIT.LICENSE +0 -20
- data/spec/js/jasmine/jasmine-html.js +0 -190
- data/spec/js/jasmine/jasmine.css +0 -166
- data/spec/js/jasmine/jasmine.js +0 -2476
- data/spec/js/jasmine/jasmine_favicon.png +0 -0
- data/spec/js/locales.spec.js +0 -31
- data/spec/js/localization.spec.js +0 -78
- data/spec/js/numbers.spec.js +0 -170
- data/spec/js/placeholder.spec.js +0 -24
- data/spec/js/pluralization.spec.js +0 -219
- data/spec/js/prepare_options.spec.js +0 -41
- data/spec/js/require.js +0 -2083
- data/spec/js/specs.html +0 -49
- data/spec/js/specs_requirejs.html +0 -72
- data/spec/js/translate.spec.js +0 -299
- data/spec/js/translations.js +0 -172
- data/spec/js/utility_functions.spec.js +0 -20
- data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
- data/spec/ruby/i18n/js/segment_spec.rb +0 -219
- data/spec/ruby/i18n/js/utils_spec.rb +0 -106
- data/spec/ruby/i18n/js_spec.rb +0 -748
- data/spec/spec_helper.rb +0 -79
- data/yarn.lock +0 -131
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at me@fnando.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Contributing to i18n-js
|
2
|
+
|
3
|
+
👍🎉 First off, thanks for taking the time to contribute! 🎉👍
|
4
|
+
|
5
|
+
The following is a set of guidelines for contributing to this project. These are
|
6
|
+
mostly guidelines, not rules. Use your best judgment, and feel free to propose
|
7
|
+
changes to this document in a pull request.
|
8
|
+
|
9
|
+
## Code of Conduct
|
10
|
+
|
11
|
+
Everyone interacting in this project's codebases, issue trackers, chat rooms and
|
12
|
+
mailing lists is expected to follow the [code of conduct](https://github.com/fnando/i18n-js/blob/main/CODE_OF_CONDUCT.md).
|
13
|
+
|
14
|
+
## Reporting bugs
|
15
|
+
|
16
|
+
This section guides you through submitting a bug report. Following these
|
17
|
+
guidelines helps maintainers and the community understand your report, reproduce
|
18
|
+
the behavior, and find related reports.
|
19
|
+
|
20
|
+
- Before creating bug reports, please check the open issues; somebody may
|
21
|
+
already have submitted something similar, and you may not need to create a new
|
22
|
+
one.
|
23
|
+
- When you are creating a bug report, please include as many details as
|
24
|
+
possible, with an example reproducing the issue.
|
25
|
+
|
26
|
+
## Contributing with code
|
27
|
+
|
28
|
+
Before making any radicals changes, please make sure you discuss your intention
|
29
|
+
by [opening an issue on Github](https://github.com/fnando/i18n-js/issues).
|
30
|
+
|
31
|
+
When you're ready to make your pull request, follow checklist below to make sure
|
32
|
+
your contribution is according to how this project works.
|
33
|
+
|
34
|
+
1. [Fork](https://help.github.com/forking/) i18n-js
|
35
|
+
2. Create a topic branch - `git checkout -b my_branch`
|
36
|
+
3. Make your changes using [descriptive commit messages](#commit-messages)
|
37
|
+
4. Update CHANGELOG.md describing your changes by adding an entry to the
|
38
|
+
"Unreleased" section. If this section is not available, create one right
|
39
|
+
before the last version.
|
40
|
+
5. Push to your branch - `git push origin my_branch`
|
41
|
+
6. [Create a pull request](https://help.github.com/articles/creating-a-pull-request)
|
42
|
+
7. That's it!
|
43
|
+
|
44
|
+
## Styleguides
|
45
|
+
|
46
|
+
### Commit messages
|
47
|
+
|
48
|
+
- Use the present tense ("Add feature" not "Added feature")
|
49
|
+
- Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
|
50
|
+
- Limit the first line to 72 characters or less
|
51
|
+
- Reference issues and pull requests liberally after the first line
|
52
|
+
|
53
|
+
### Changelog
|
54
|
+
|
55
|
+
- Add a message describing your changes to the "Unreleased" section. The
|
56
|
+
changelog message should follow the same style as the commit message.
|
57
|
+
- Prefix your message with one of the following:
|
58
|
+
- `[Added]` for new features.
|
59
|
+
- `[Changed]` for changes in existing functionality.
|
60
|
+
- `[Deprecated]` for soon-to-be removed features.
|
61
|
+
- `[Removed]` for now removed features.
|
62
|
+
- `[Fixed]` for any bug fixes.
|
63
|
+
- `[Security]` in case of vulnerabilities.
|
64
|
+
|
65
|
+
### Ruby code
|
66
|
+
|
67
|
+
- This project uses [Rubocop](https://rubocop.org) to enforce code style. Before
|
68
|
+
submitting your changes, make sure your tests are passing and code conforms to
|
69
|
+
the expected style by running `rake`.
|
70
|
+
- Do not change the library version. This will be done by the maintainer
|
71
|
+
whenever a new version is about to be released.
|
72
|
+
|
73
|
+
### JavaScript code
|
74
|
+
|
75
|
+
- This project uses [ESLint](https://eslint.org) to enforce code style. Before
|
76
|
+
submitting your changes, make sure your tests are passing and code conforms to
|
77
|
+
the expected style by running `yarn test:ci`.
|
78
|
+
- Do not change the library version. This will be done by the maintainer
|
79
|
+
whenever a new version is about to be released.
|
data/Gemfile
CHANGED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Nando Vieira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,956 +1,142 @@
|
|
1
|
-
#
|
1
|
+
# i18n-js
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![
|
3
|
+
[![Tests](https://github.com/fnando/i18n-js/workflows/ruby-tests/badge.svg)](https://github.com/fnando/i18n-js)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/fnando/i18n-js/badges/gpa.svg)](https://codeclimate.com/github/fnando/i18n-js)
|
5
|
+
[![Gem](https://img.shields.io/gem/v/i18n-js.svg)](https://rubygems.org/gems/i18n-js)
|
6
|
+
[![Gem](https://img.shields.io/gem/dt/i18n-js.svg)](https://rubygems.org/gems/i18n-js)
|
6
7
|
|
7
|
-
[
|
8
|
-
|
8
|
+
Export [i18n](https://rubygems.org/gems/i18n) translations to JSON. A perfect
|
9
|
+
fit if you want to export translations to JavaScript.
|
9
10
|
|
10
|
-
|
11
|
+
Oh, you don't use Ruby? No problem! You can still use i18n-js and the
|
12
|
+
[companion JavaScript package](https://npmjs.com/package/i18n-js).
|
11
13
|
|
12
|
-
|
14
|
+
## Installation
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
Features:
|
17
|
-
|
18
|
-
- Pluralization
|
19
|
-
- Date/Time localization
|
20
|
-
- Number localization
|
21
|
-
- Locale fallback
|
22
|
-
- Asset pipeline support
|
23
|
-
- Lots more! :)
|
24
|
-
|
25
|
-
## Version Notice
|
26
|
-
The `master` branch (including this README) is for latest `3.0.0` instead of `2.x`.
|
27
|
-
|
28
|
-
|
29
|
-
## Usage
|
30
|
-
|
31
|
-
### Installation
|
32
|
-
|
33
|
-
#### Rails app
|
34
|
-
|
35
|
-
Add the gem to your Gemfile.
|
36
|
-
```ruby
|
37
|
-
gem "i18n-js"
|
38
|
-
```
|
39
|
-
|
40
|
-
#### Rails app with [Asset Pipeline](http://guides.rubyonrails.org/asset_pipeline.html)
|
41
|
-
|
42
|
-
If you're using the [asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html),
|
43
|
-
then you must add the following line to your `app/assets/javascripts/application.js`.
|
44
|
-
|
45
|
-
```javascript
|
46
|
-
//
|
47
|
-
// This is optional (in case you have `I18n is not defined` error)
|
48
|
-
// If you want to put this line, you must put it BEFORE `i18n/translations`
|
49
|
-
//= require i18n
|
50
|
-
// Some people even need to add the extension to make it work, see https://github.com/fnando/i18n-js/issues/283
|
51
|
-
//= require i18n.js
|
52
|
-
//
|
53
|
-
// This is a must
|
54
|
-
//= require i18n/translations
|
55
|
-
```
|
56
|
-
|
57
|
-
#### Rails app without [Asset Pipeline](http://guides.rubyonrails.org/asset_pipeline.html)
|
58
|
-
|
59
|
-
|
60
|
-
First, put this in your `application.html` (layout file).
|
61
|
-
Then get the JS files following the instructions below.
|
62
|
-
```erb
|
63
|
-
<%# This is just an example, you can put `i18n.js` and `translations.js` anywhere you like %>
|
64
|
-
<%# Unlike the Asset Pipeline example, you need to require both **in order** %>
|
65
|
-
<%= javascript_include_tag "i18n" %>
|
66
|
-
<%= javascript_include_tag "translations", skip_pipeline: true %>
|
67
|
-
```
|
68
|
-
|
69
|
-
**There are two ways to get `translations.js` (For Rails app without Asset Pipeline).**
|
70
|
-
|
71
|
-
1. This `translations.js` file can be automatically generated by the `I18n::JS::Middleware`.
|
72
|
-
Just add `config.middleware.use I18n::JS::Middleware` to your `config/application.rb` file.
|
73
|
-
2. If you can't or prefer not to generate this file,
|
74
|
-
you can move the middleware line to your `config/environments/development.rb` file
|
75
|
-
and run `rake i18n:js:export` before deploying.
|
76
|
-
This will export all translation files, including the custom scopes
|
77
|
-
you may have defined on `config/i18n-js.yml`.
|
78
|
-
If `I18n.available_locales` is set (e.g. in your Rails `config/application.rb` file)
|
79
|
-
then only the specified locales will be exported.
|
80
|
-
Current version of `i18n.js` will also be exported to avoid version mismatching by downloading.
|
81
|
-
|
82
|
-
#### Export Configuration (For translations)
|
83
|
-
|
84
|
-
Exported translation files generated by `I18n::JS::Middleware` or `rake i18n:js:export` can be customized with config file `config/i18n-js.yml`
|
85
|
-
(use `rails generate i18n:js:config` to create it).
|
86
|
-
You can even get more files generated to different folders and with different translations to best suit your needs.
|
87
|
-
The config file also affects developers using Asset Pipeline to require translations.
|
88
|
-
Except the option `file`, since all translations are required by adding `//= require i18n/translations`.
|
89
|
-
|
90
|
-
Examples:
|
91
|
-
```yaml
|
92
|
-
translations:
|
93
|
-
- file: 'public/javascripts/path-to-your-messages-file.js'
|
94
|
-
only: '*.date.formats'
|
95
|
-
- file: 'public/javascripts/path-to-your-second-file.js'
|
96
|
-
only: ['*.activerecord', '*.admin.*.title']
|
97
|
-
```
|
98
|
-
|
99
|
-
If `only` is omitted all the translations will be saved. Also, make sure you add that initial `*`; it specifies that all languages will be exported. If you want to export only one language, you can do something like this:
|
100
|
-
```yaml
|
101
|
-
translations:
|
102
|
-
- file: 'public/javascripts/en.js'
|
103
|
-
only: 'en.*'
|
104
|
-
- file: 'public/javascripts/pt-BR.js'
|
105
|
-
only: 'pt-BR.*'
|
106
|
-
```
|
107
|
-
|
108
|
-
Optionally, you can auto generate a translation file per available locale if you specify the `%{locale}` placeholder.
|
109
|
-
```yaml
|
110
|
-
translations:
|
111
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
112
|
-
only: '*'
|
113
|
-
- file: "public/javascripts/frontend/i18n/%{locale}.js"
|
114
|
-
only: ['*.frontend', '*.users.*']
|
115
|
-
```
|
116
|
-
|
117
|
-
You can also include ERB in your config file.
|
118
|
-
```yaml
|
119
|
-
translations:
|
120
|
-
<% Widgets.each do |widget| %>
|
121
|
-
- file: <%= "'#{widget.file}'" %>
|
122
|
-
only: <%= "'#{widget.only}'" %>
|
123
|
-
<% end %>
|
124
|
-
```
|
125
|
-
|
126
|
-
You are able to exclude certain phrases or whole groups of phrases by
|
127
|
-
specifying the YAML key(s) in the `except` configuration option. The outputted
|
128
|
-
JS translations file (exported or generated by the middleware) will omit any
|
129
|
-
keys listed in `except` configuration param:
|
130
|
-
|
131
|
-
```yaml
|
132
|
-
translations:
|
133
|
-
- except: ['*.active_admin', '*.ransack', '*.activerecord.errors']
|
134
|
-
```
|
135
|
-
|
136
|
-
|
137
|
-
#### Export Configuration (For other things)
|
138
|
-
|
139
|
-
- `I18n::JS.config_file_path`
|
140
|
-
Expected Type: `String`
|
141
|
-
Default: `config/i18n-js.yml`
|
142
|
-
Behaviour: Try to read the config file from that location
|
143
|
-
|
144
|
-
- `I18n::JS.export_i18n_js_dir_path`
|
145
|
-
Expected Type: `String`
|
146
|
-
Default: `public/javascripts`
|
147
|
-
Behaviour:
|
148
|
-
- Any `String`: considered as a relative path for a folder to `Rails.root` and export `i18n.js` to that folder for `rake i18n:js:export`
|
149
|
-
- Any non-`String` (`nil`, `false`, `:none`, etc): Disable `i18n.js` exporting
|
150
|
-
|
151
|
-
- `I18n::JS.sort_translation_keys`
|
152
|
-
Expected Type: `Boolean`
|
153
|
-
Default: `true`
|
154
|
-
Behaviour:
|
155
|
-
- Sets whether or not to deep sort all translation keys in order to generate identical output for the same translations
|
156
|
-
- Set to true to ensure identical asset fingerprints for the asset pipeline
|
157
|
-
|
158
|
-
- You may also set `export_i18n_js` and `sort_translation_keys` in your config file, e.g.:
|
159
|
-
|
160
|
-
```yaml
|
161
|
-
export_i18n_js: false
|
162
|
-
# OR
|
163
|
-
export_i18n_js: "my/path"
|
164
|
-
|
165
|
-
sort_translation_keys: false
|
166
|
-
|
167
|
-
translations:
|
168
|
-
- ...
|
169
|
-
```
|
170
|
-
|
171
|
-
To find more examples on how to use the configuration file please refer to the tests.
|
172
|
-
|
173
|
-
#### Fallbacks
|
174
|
-
|
175
|
-
If you specify the `fallbacks` option, you will be able to fill missing translations with those inside fallback locale(s).
|
176
|
-
Default value is `true`.
|
177
|
-
|
178
|
-
Examples:
|
179
|
-
```yaml
|
180
|
-
fallbacks: true
|
181
|
-
|
182
|
-
translations:
|
183
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
184
|
-
only: '*'
|
185
|
-
```
|
186
|
-
This will enable merging fallbacks into each file. (set to `false` to disable).
|
187
|
-
If you use `I18n` with fallbacks, the fallbacks defined there will be used.
|
188
|
-
Otherwise `I18n.default_locale` will be used.
|
189
|
-
|
190
|
-
```yaml
|
191
|
-
fallbacks: :de
|
192
|
-
|
193
|
-
translations:
|
194
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
195
|
-
only: '*'
|
196
|
-
```
|
197
|
-
Here, the specified locale `:de` will be used as fallback for all locales.
|
198
|
-
|
199
|
-
```yaml
|
200
|
-
fallbacks:
|
201
|
-
fr: ["de", "en"]
|
202
|
-
de: "en"
|
203
|
-
|
204
|
-
translations:
|
205
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
206
|
-
only: '*'
|
207
|
-
```
|
208
|
-
Fallbacks defined will be used, if not defined (e.g. `:pl`) `I18n.fallbacks` or `I18n.default_locale` will be used.
|
209
|
-
|
210
|
-
```yaml
|
211
|
-
fallbacks: :default_locale
|
212
|
-
|
213
|
-
translations:
|
214
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
215
|
-
only: '*'
|
216
|
-
```
|
217
|
-
Setting the option to `:default_locale` will enforce the fallback to use the `I18n.default_locale`, ignoring `I18n.fallbacks`.
|
218
|
-
|
219
|
-
Examples:
|
220
|
-
```yaml
|
221
|
-
fallbacks: false
|
222
|
-
|
223
|
-
translations:
|
224
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
225
|
-
only: '*'
|
226
|
-
```
|
227
|
-
You must disable this feature by setting the option to `false`.
|
228
|
-
|
229
|
-
To find more examples on how to use the configuration file please refer to the tests.
|
230
|
-
|
231
|
-
|
232
|
-
#### Namespace
|
233
|
-
|
234
|
-
Setting the `namespace` option will change the namespace of the output Javascript file to something other than `I18n`.
|
235
|
-
This can be useful in no-conflict scenarios. Example:
|
236
|
-
|
237
|
-
```yaml
|
238
|
-
translations:
|
239
|
-
- file: "public/javascripts/i18n/translations.js"
|
240
|
-
namespace: "MyNamespace"
|
241
|
-
```
|
242
|
-
|
243
|
-
will create:
|
244
|
-
|
245
|
-
```
|
246
|
-
MyNamespace.translations || (MyNamespace.translations = {});
|
247
|
-
MyNamespace.translations["en"] = { ... }
|
248
|
-
```
|
249
|
-
|
250
|
-
|
251
|
-
### Adding prefix & suffix to the translations file(s)
|
252
|
-
|
253
|
-
Setting the `prefix: "import I18n from 'i18n-js';\n"` option will add the line at the beginning of the resultant translation file.
|
254
|
-
This can be useful to use this gem with the [i18n-js](https://www.npmjs.com/package/i18n-js) npm package, which is quite useful to use it with webpack.
|
255
|
-
The user should provide the semi-colon and the newline character if needed.
|
256
|
-
|
257
|
-
For example:
|
258
|
-
|
259
|
-
```yaml
|
260
|
-
translations:
|
261
|
-
- file: "public/javascripts/i18n/translations.js"
|
262
|
-
prefix: "import I18n from 'i18n-js';\n"
|
16
|
+
```bash
|
17
|
+
gem install i18n-js
|
263
18
|
```
|
264
19
|
|
265
|
-
|
20
|
+
Or add the following line to your project's Gemfile:
|
266
21
|
|
22
|
+
```ruby
|
23
|
+
gem "i18n-js", "~> 4.0.0.alpha1"
|
267
24
|
```
|
268
|
-
import I18n from 'i18n-js';
|
269
|
-
I18n.translations || (I18n.translations = {});
|
270
|
-
```
|
271
|
-
|
272
|
-
|
273
|
-
`suffix` option is added in https://github.com/fnando/i18n-js/pull/561.
|
274
|
-
It's similar to `prefix` so won't explain it in details.
|
275
|
-
|
276
|
-
|
277
|
-
#### Pretty Print
|
278
25
|
|
279
|
-
|
280
|
-
|
281
|
-
```yaml
|
282
|
-
translations:
|
283
|
-
- file: "public/javascripts/i18n/translations.js"
|
284
|
-
pretty_print: true
|
285
|
-
```
|
26
|
+
## Usage
|
286
27
|
|
28
|
+
About patterns:
|
287
29
|
|
288
|
-
|
30
|
+
- Patterns can use `*` as a wildcard and can appear more than once.
|
31
|
+
- `*` will include everything
|
32
|
+
- `*.messages.*`
|
33
|
+
- Patterns starting with `!` are excluded.
|
34
|
+
- `!*.activerecord.*` will exclude all ActiveRecord translations.
|
289
35
|
|
290
|
-
|
291
|
-
files are deep-merged with any locale data already in memory. To disable this either globally or per-file,
|
292
|
-
set the `js_extend` option to false
|
36
|
+
The config file:
|
293
37
|
|
294
|
-
```
|
295
|
-
|
38
|
+
```yml
|
39
|
+
---
|
296
40
|
translations:
|
297
|
-
- file:
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
Just add the `i18n.js` file to your page. You'll have to build the translations object
|
305
|
-
by hand or using your favorite programming language. More info below.
|
306
|
-
|
307
|
-
|
308
|
-
#### Via NPM with webpack and CommonJS
|
309
|
-
|
310
|
-
|
311
|
-
Add the following line to your package.json dependencies
|
312
|
-
where version is the version you want
|
313
|
-
```javascript
|
314
|
-
"i18n-js": "{version_constraint}"
|
315
|
-
|
316
|
-
// Or if you want unreleased version
|
317
|
-
// npm install requires it to be the gzipped tarball, see [npm install](https://www.npmjs.org/doc/cli/npm-install.html)
|
318
|
-
"i18n-js": "https://github.com/fnando/i18n-js/archive/{tag_name_or_branch_name_or_commit_sha}.tar.gz"
|
319
|
-
```
|
320
|
-
Run npm install then use via
|
321
|
-
```javascript
|
322
|
-
var i18n = require("i18n-js");
|
323
|
-
```
|
41
|
+
- file: app/frontend/locales/en.json
|
42
|
+
patterns:
|
43
|
+
- "*"
|
44
|
+
- "!*.activerecord"
|
45
|
+
- "!*.errors"
|
46
|
+
- "!*.number.nth"
|
324
47
|
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
You **don't** need to set up a thing. The default settings will work just okay. But if you want to split translations into several files or specify specific contexts, you can follow the rest of this setting up section.
|
329
|
-
|
330
|
-
Set your locale is easy as
|
331
|
-
```javascript
|
332
|
-
I18n.defaultLocale = "pt-BR";
|
333
|
-
I18n.locale = "pt-BR";
|
334
|
-
I18n.currentLocale();
|
335
|
-
// pt-BR
|
336
|
-
```
|
337
|
-
|
338
|
-
**NOTE:** You can now apply your configuration **before I18n** is loaded like this:
|
339
|
-
```javascript
|
340
|
-
I18n = {} // You must define this object in top namespace, which should be `window`
|
341
|
-
I18n.defaultLocale = "pt-BR";
|
342
|
-
I18n.locale = "pt-BR";
|
343
|
-
|
344
|
-
// Load I18n from `i18n.js`, `application.js` or whatever
|
345
|
-
|
346
|
-
I18n.currentLocale();
|
347
|
-
// pt-BR
|
48
|
+
- file: app/frontend/locales/:locale.json
|
49
|
+
patterns:
|
50
|
+
- "*"
|
348
51
|
```
|
349
52
|
|
350
|
-
|
53
|
+
The Ruby API:
|
351
54
|
|
352
|
-
```
|
353
|
-
|
354
|
-
I18n.defaultLocale = "<%= I18n.default_locale %>";
|
355
|
-
I18n.locale = "<%= I18n.locale %>";
|
356
|
-
</script>
|
357
|
-
```
|
358
|
-
|
359
|
-
You can use translate your messages:
|
360
|
-
|
361
|
-
```javascript
|
362
|
-
I18n.t("some.scoped.translation");
|
363
|
-
// or translate with explicit setting of locale
|
364
|
-
I18n.t("some.scoped.translation", {locale: "fr"});
|
365
|
-
```
|
366
|
-
|
367
|
-
You can also interpolate values:
|
368
|
-
|
369
|
-
```javascript
|
370
|
-
// You need the `translations` object setup first
|
371
|
-
I18n.translations["en"] = {
|
372
|
-
greeting: "Hello %{name}"
|
373
|
-
}
|
374
|
-
|
375
|
-
I18n.t("greeting", {name: "John Doe"});
|
376
|
-
```
|
377
|
-
You can set default values for missing scopes:
|
378
|
-
```javascript
|
379
|
-
// simple translation
|
380
|
-
I18n.t("some.missing.scope", {defaultValue: "A default message"});
|
381
|
-
|
382
|
-
// with interpolation
|
383
|
-
I18n.t("noun", {defaultValue: "I'm a {{noun}}", noun: "Mac"});
|
384
|
-
```
|
385
|
-
|
386
|
-
You can also provide a list of default fallbacks for missing scopes:
|
387
|
-
|
388
|
-
```javascript
|
389
|
-
// As a scope
|
390
|
-
I18n.t("some.missing.scope", {defaults: [{scope: "some.existing.scope"}]});
|
391
|
-
|
392
|
-
// As a simple translation
|
393
|
-
I18n.t("some.missing.scope", {defaults: [{message: "Some message"}]});
|
394
|
-
```
|
395
|
-
|
396
|
-
Default values must be provided as an array of hashs where the key is the
|
397
|
-
type of translation desired, a `scope` or a `message`. The translation returned
|
398
|
-
will be either the first scope recognized, or the first message defined.
|
399
|
-
|
400
|
-
The translation will fallback to the `defaultValue` translation if no scope
|
401
|
-
in `defaults` matches and if no default of type `message` is found.
|
402
|
-
|
403
|
-
Translation fallback can be enabled by enabling the `I18n.fallbacks` option:
|
404
|
-
|
405
|
-
```erb
|
406
|
-
<script type="text/javascript">
|
407
|
-
I18n.fallbacks = true;
|
408
|
-
</script>
|
409
|
-
```
|
410
|
-
|
411
|
-
By default missing translations will first be looked for in less
|
412
|
-
specific versions of the requested locale and if that fails by taking
|
413
|
-
them from your `I18n.defaultLocale`.
|
414
|
-
|
415
|
-
```javascript
|
416
|
-
// if I18n.defaultLocale = "en" and translation doesn't exist
|
417
|
-
// for I18n.locale = "de-DE" this key will be taken from "de" locale scope
|
418
|
-
// or, if that also doesn't exist, from "en" locale scope
|
419
|
-
I18n.t("some.missing.scope");
|
420
|
-
```
|
421
|
-
|
422
|
-
Custom fallback rules can also be specified for a particular language. There
|
423
|
-
are three different ways of doing it so:
|
424
|
-
|
425
|
-
```javascript
|
426
|
-
I18n.locales.no = ["nb", "en"];
|
427
|
-
I18n.locales.no = "nb";
|
428
|
-
I18n.locales.no = function(locale){ return ["nb"]; };
|
429
|
-
```
|
430
|
-
|
431
|
-
By default a missing translation will be displayed as
|
432
|
-
|
433
|
-
[missing "name of scope" translation]
|
434
|
-
|
435
|
-
While you are developing or if you do not want to provide a translation
|
436
|
-
in the default language you can set
|
437
|
-
|
438
|
-
```javascript
|
439
|
-
I18n.missingBehaviour='guess';
|
440
|
-
```
|
441
|
-
|
442
|
-
this will take the last section of your scope and guess the intended value.
|
443
|
-
Camel case becomes lower cased text and underscores are replaced with space
|
444
|
-
|
445
|
-
questionnaire.whatIsYourFavorite_ChristmasPresent
|
446
|
-
|
447
|
-
becomes "what is your favorite Christmas present"
|
448
|
-
|
449
|
-
In order to still detect untranslated strings, you can
|
450
|
-
i18n.missingTranslationPrefix to something like:
|
451
|
-
```javascript
|
452
|
-
I18n.missingTranslationPrefix = 'EE: ';
|
453
|
-
```
|
454
|
-
|
455
|
-
And result will be:
|
456
|
-
```javascript
|
457
|
-
"EE: what is your favorite Christmas present"
|
458
|
-
```
|
459
|
-
|
460
|
-
This will help you doing automated tests against your localisation assets.
|
461
|
-
|
462
|
-
Some people prefer returning `null` for missing translation:
|
463
|
-
```javascript
|
464
|
-
I18n.missingTranslation = function () { return undefined; };
|
465
|
-
```
|
466
|
-
|
467
|
-
Pluralization is possible as well and by default provides English rules:
|
468
|
-
|
469
|
-
```javascript
|
470
|
-
I18n.t("inbox.counting", {count: 10}); // You have 10 messages
|
471
|
-
```
|
472
|
-
|
473
|
-
The sample above expects the following translation:
|
474
|
-
|
475
|
-
```yaml
|
476
|
-
en:
|
477
|
-
inbox:
|
478
|
-
counting:
|
479
|
-
one: You have 1 new message
|
480
|
-
other: You have {{count}} new messages
|
481
|
-
zero: You have no messages
|
482
|
-
```
|
483
|
-
|
484
|
-
**NOTE:** Rails I18n recognizes the `zero` option.
|
485
|
-
|
486
|
-
If you need special rules just define them for your language, for example Russian, just add a new pluralizer:
|
487
|
-
|
488
|
-
```javascript
|
489
|
-
I18n.pluralization["ru"] = function (count) {
|
490
|
-
var key = count % 10 == 1 && count % 100 != 11 ? "one" : [2, 3, 4].indexOf(count % 10) >= 0 && [12, 13, 14].indexOf(count % 100) < 0 ? "few" : count % 10 == 0 || [5, 6, 7, 8, 9].indexOf(count % 10) >= 0 || [11, 12, 13, 14].indexOf(count % 100) >= 0 ? "many" : "other";
|
491
|
-
return [key];
|
492
|
-
};
|
493
|
-
```
|
494
|
-
|
495
|
-
You can find all rules on <http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html>.
|
496
|
-
|
497
|
-
If you're using the same scope over and over again, you may use the `scope` option.
|
498
|
-
|
499
|
-
```javascript
|
500
|
-
var options = {scope: "activerecord.attributes.user"};
|
501
|
-
|
502
|
-
I18n.t("name", options);
|
503
|
-
I18n.t("email", options);
|
504
|
-
I18n.t("username", options);
|
505
|
-
```
|
506
|
-
|
507
|
-
You can also provide an array as scope.
|
508
|
-
|
509
|
-
```javascript
|
510
|
-
// use the greetings.hello scope
|
511
|
-
I18n.t(["greetings", "hello"]);
|
512
|
-
```
|
513
|
-
|
514
|
-
#### Number formatting
|
515
|
-
|
516
|
-
Similar to Rails helpers, you have localized number and currency formatting.
|
517
|
-
|
518
|
-
```javascript
|
519
|
-
I18n.l("currency", 1990.99);
|
520
|
-
// $1,990.99
|
521
|
-
|
522
|
-
I18n.l("number", 1990.99);
|
523
|
-
// 1,990.99
|
524
|
-
|
525
|
-
I18n.l("percentage", 123.45);
|
526
|
-
// 123.450%
|
527
|
-
```
|
528
|
-
|
529
|
-
To have more control over number formatting, you can use the
|
530
|
-
`I18n.toNumber`, `I18n.toPercentage`, `I18n.toCurrency` and `I18n.toHumanSize`
|
531
|
-
functions.
|
532
|
-
|
533
|
-
```javascript
|
534
|
-
I18n.toNumber(1000); // 1,000.000
|
535
|
-
I18n.toCurrency(1000); // $1,000.00
|
536
|
-
I18n.toPercentage(100); // 100.000%
|
537
|
-
```
|
538
|
-
|
539
|
-
The `toNumber` and `toPercentage` functions accept the following options:
|
540
|
-
|
541
|
-
- `precision`: defaults to `3`
|
542
|
-
- `separator`: defaults to `.`
|
543
|
-
- `delimiter`: defaults to `,`
|
544
|
-
- `strip_insignificant_zeros`: defaults to `false`
|
545
|
-
|
546
|
-
See some number formatting examples:
|
547
|
-
|
548
|
-
```javascript
|
549
|
-
I18n.toNumber(1000, {precision: 0}); // 1,000
|
550
|
-
I18n.toNumber(1000, {delimiter: ".", separator: ","}); // 1.000,000
|
551
|
-
I18n.toNumber(1000, {delimiter: ".", precision: 0}); // 1.000
|
552
|
-
```
|
553
|
-
|
554
|
-
The `toCurrency` function accepts the following options:
|
555
|
-
|
556
|
-
- `precision`: sets the level of precision
|
557
|
-
- `separator`: sets the separator between the units
|
558
|
-
- `delimiter`: sets the thousands delimiter
|
559
|
-
- `format`: sets the format of the output string
|
560
|
-
- `unit`: sets the denomination of the currency
|
561
|
-
- `strip_insignificant_zeros`: defaults to `false`
|
562
|
-
- `sign_first`: defaults to `true`
|
563
|
-
|
564
|
-
You can provide only the options you want to override:
|
565
|
-
|
566
|
-
```javascript
|
567
|
-
I18n.toCurrency(1000, {precision: 0}); // $1,000
|
568
|
-
```
|
569
|
-
|
570
|
-
The `toHumanSize` function accepts the following options:
|
571
|
-
|
572
|
-
- `precision`: defaults to `1`
|
573
|
-
- `separator`: defaults to `.`
|
574
|
-
- `delimiter`: defaults to `""`
|
575
|
-
- `strip_insignificant_zeros`: defaults to `false`
|
576
|
-
- `format`: defaults to `%n%u`
|
577
|
-
|
578
|
-
<!---->
|
579
|
-
|
580
|
-
```javascript
|
581
|
-
I18n.toHumanSize(1234); // 1KB
|
582
|
-
I18n.toHumanSize(1234 * 1024); // 1MB
|
583
|
-
```
|
584
|
-
|
585
|
-
|
586
|
-
#### Date formatting
|
587
|
-
|
588
|
-
```javascript
|
589
|
-
// accepted formats
|
590
|
-
I18n.l("date.formats.short", "2009-09-18"); // yyyy-mm-dd
|
591
|
-
I18n.l("time.formats.short", "2009-09-18 23:12:43"); // yyyy-mm-dd hh:mm:ss
|
592
|
-
I18n.l("time.formats.short", "2009-11-09T18:10:34"); // JSON format with local Timezone (part of ISO-8601)
|
593
|
-
I18n.l("time.formats.short", "2009-11-09T18:10:34Z"); // JSON format in UTC (part of ISO-8601)
|
594
|
-
I18n.l("date.formats.short", 1251862029000); // Epoch time
|
595
|
-
I18n.l("date.formats.short", "09/18/2009"); // mm/dd/yyyy
|
596
|
-
I18n.l("date.formats.short", (new Date())); // Date object
|
597
|
-
```
|
55
|
+
```ruby
|
56
|
+
require "i18n-js"
|
598
57
|
|
599
|
-
|
600
|
-
|
601
|
-
```javascript
|
602
|
-
I18n.translations["en"] = {
|
603
|
-
date: {
|
604
|
-
formats: {
|
605
|
-
ordinal_day: "%B %{day}"
|
606
|
-
}
|
607
|
-
}
|
608
|
-
}
|
609
|
-
I18n.l("date.formats.ordinal_day", "2009-09-18", { day: '18th' }); // Sep 18th
|
58
|
+
I18nJS.call(config_file: "config/i18n.yml")
|
59
|
+
I18nJS.call(config: config)
|
610
60
|
```
|
611
61
|
|
612
|
-
|
62
|
+
The CLI API:
|
613
63
|
|
614
|
-
```
|
615
|
-
|
616
|
-
|
617
|
-
I18n.toTime("date.formats.short", date);
|
618
|
-
I18n.strftime(date, "%d/%m/%Y");
|
619
|
-
```
|
620
|
-
|
621
|
-
The accepted formats for `I18n.strftime` are:
|
622
|
-
|
623
|
-
%a - The abbreviated weekday name (Sun)
|
624
|
-
%A - The full weekday name (Sunday)
|
625
|
-
%b - The abbreviated month name (Jan)
|
626
|
-
%B - The full month name (January)
|
627
|
-
%c - The preferred local date and time representation
|
628
|
-
%d - Day of the month (01..31)
|
629
|
-
%-d - Day of the month (1..31)
|
630
|
-
%H - Hour of the day, 24-hour clock (00..23)
|
631
|
-
%-H/%k - Hour of the day, 24-hour clock (0..23)
|
632
|
-
%I - Hour of the day, 12-hour clock (01..12)
|
633
|
-
%-I/%l - Hour of the day, 12-hour clock (1..12)
|
634
|
-
%m - Month of the year (01..12)
|
635
|
-
%-m - Month of the year (1..12)
|
636
|
-
%M - Minute of the hour (00..59)
|
637
|
-
%-M - Minute of the hour (0..59)
|
638
|
-
%p - Meridian indicator (AM or PM)
|
639
|
-
%P - Meridian indicator (am or pm)
|
640
|
-
%S - Second of the minute (00..60)
|
641
|
-
%-S - Second of the minute (0..60)
|
642
|
-
%w - Day of the week (Sunday is 0, 0..6)
|
643
|
-
%y - Year without a century (00..99)
|
644
|
-
%-y - Year without a century (0..99)
|
645
|
-
%Y - Year with century
|
646
|
-
%z/%Z - Timezone offset (+0545)
|
647
|
-
|
648
|
-
Check out `spec/*.spec.js` files for more examples!
|
649
|
-
|
650
|
-
#### Using pluralization and number formatting together
|
651
|
-
Sometimes you might want to display translation with formatted number, like adding thousand delimiters to displayed number
|
652
|
-
You can do this:
|
653
|
-
```json
|
654
|
-
{
|
655
|
-
"en": {
|
656
|
-
"point": {
|
657
|
-
"one": "1 Point",
|
658
|
-
"other": "{{formatted_number}} Points",
|
659
|
-
"zero": "0 Points"
|
660
|
-
}
|
661
|
-
}
|
662
|
-
}
|
663
|
-
```
|
664
|
-
```js
|
665
|
-
var point_in_number = 1000;
|
666
|
-
I18n.t('point', { count: point_in_number, formatted_number: I18n.toNumber(point_in_number) });
|
667
|
-
```
|
668
|
-
Output should be `1,000 points`
|
669
|
-
|
670
|
-
|
671
|
-
## Using multiple exported translation files on a page.
|
672
|
-
This method is useful for very large apps where a single contained translations.js file is not desirable. Examples would be a global translations file and a more specific route translation file.
|
673
|
-
|
674
|
-
### Rails without asset pipeline
|
675
|
-
1. Setup your `config/i18n-js.yml` to have multiple files and try to minimize any overlap.
|
676
|
-
|
677
|
-
```yaml
|
678
|
-
sort_translation_keys: true
|
679
|
-
fallbacks: false
|
680
|
-
|
681
|
-
translations:
|
682
|
-
+ file: "app/assets/javascript/nls/welcome.js"
|
683
|
-
only:
|
684
|
-
+ '*.welcome.*'
|
685
|
-
|
686
|
-
+ file: "app/assets/javascript/nls/albums.js"
|
687
|
-
only:
|
688
|
-
+ '*.albums.*'
|
689
|
-
|
690
|
-
+ file: "app/assets/javascript/nls/global.js"
|
691
|
-
only:
|
692
|
-
+ '*'
|
693
|
-
# Exempt any routes specific translations from being
|
694
|
-
# included in the global translation file
|
695
|
-
except:
|
696
|
-
+ '*.welcome.*'
|
697
|
-
+ '*.albums.*'
|
698
|
-
```
|
699
|
-
When `rake i18n:js:export` is executed it will create 3 translations files that can be loaded via the `javascript_include_tag`
|
700
|
-
|
701
|
-
2. Add the `javascript_include_tag` to your layout and to any route specific files that will require it.
|
702
|
-
```ruby
|
703
|
-
# views/layouts/application.html.erb
|
704
|
-
<%= javascript_include_tag(
|
705
|
-
"i18n"
|
706
|
-
"nls/global"
|
707
|
-
) %>
|
708
|
-
```
|
709
|
-
and in the route specific
|
710
|
-
|
711
|
-
```ruby
|
712
|
-
# views/welcome/index.html.erb
|
713
|
-
<%= javascript_include_tag(
|
714
|
-
"nls/welcome"
|
715
|
-
) %>
|
716
|
-
```
|
717
|
-
|
718
|
-
3. Make sure that you add these files to your `config/application.rb`
|
719
|
-
|
720
|
-
```ruby
|
721
|
-
config.assets.precompile += %w(
|
722
|
-
i18n
|
723
|
-
nls/*
|
724
|
-
)
|
725
|
-
```
|
726
|
-
|
727
|
-
### Using require.js / r.js
|
728
|
-
|
729
|
-
To use this with require.js we are only going to change a few things from above.
|
730
|
-
|
731
|
-
1. In your `config/i18n-js.yml` we need to add a better location for the i18n to be exported. You want to use this location so that it can be properly precompiled by r.js.
|
732
|
-
|
733
|
-
```yaml
|
734
|
-
export_i18n_js: "app/assets/javascript/nls"
|
735
|
-
```
|
736
|
-
|
737
|
-
2. In your `config/require.yml` we need to add a map, shim all the translations, and include them into the appropriate modules
|
738
|
-
|
739
|
-
```yaml
|
740
|
-
# In your maps add (if you do not have this you will need to add it)
|
741
|
-
map:
|
742
|
-
'*':
|
743
|
-
i18n: 'nls/i18n'
|
744
|
-
|
745
|
-
# In your shims
|
746
|
-
shims:
|
747
|
-
nls/welcome:
|
748
|
-
deps:
|
749
|
-
+ i18n
|
750
|
-
|
751
|
-
nls/global:
|
752
|
-
deps:
|
753
|
-
+ i18n
|
754
|
-
|
755
|
-
# Finally in your modules
|
756
|
-
modules:
|
757
|
-
+ name: 'application'
|
758
|
-
include:
|
759
|
-
+ i18n
|
760
|
-
+ 'nls/global'
|
761
|
-
|
762
|
-
+ name: 'welcome'
|
763
|
-
exclude:
|
764
|
-
+ application
|
765
|
-
include:
|
766
|
-
+ 'nls/welcome'
|
767
|
-
```
|
768
|
-
3. When `rake assets:precompile` is executed it will optimize the translations into the correct modules so they are loaded with their assigned module, and loading them with requirejs is as simple as requiring any other shim.
|
769
|
-
|
770
|
-
```javascript
|
771
|
-
define(['welcome/other_asset','nls/welcome'], function (otherAsset){
|
772
|
-
// ...
|
773
|
-
});
|
774
|
-
```
|
775
|
-
4. (optional) As an additional configuration we can make a task to be run before the requirejs optimizer. This will allow any automated scripts that run the requirejs optimizer to export the strings before we run r.js
|
776
|
-
|
777
|
-
```rake
|
778
|
-
# lib/tasks/i18n.rake
|
779
|
-
Rake::Task[:'i18n:js:export'].prerequisites.clear
|
780
|
-
task :'i18n:js:export' => :'i18n:js:before_export'
|
781
|
-
task :'requirejs:precompile:external' => :'i18n:js:export'
|
782
|
-
|
783
|
-
namespace :i18n do
|
784
|
-
namespace :js do
|
785
|
-
task :before_export => :'assets:environment' do
|
786
|
-
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{yml,rb}')]
|
787
|
-
I18n.backend.load_translations
|
788
|
-
end
|
789
|
-
end
|
790
|
-
end
|
791
|
-
```
|
792
|
-
|
793
|
-
## Using I18n.js with other languages (Python, PHP, ...)
|
794
|
-
|
795
|
-
The JavaScript library is language agnostic; so you can use it with PHP, Python, [your favorite language here].
|
796
|
-
The only requirement is that you need to set the `translations` attribute like following:
|
797
|
-
|
798
|
-
```javascript
|
799
|
-
I18n.translations = {};
|
800
|
-
|
801
|
-
I18n.translations["en"] = {
|
802
|
-
message: "Some special message for you"
|
803
|
-
}
|
804
|
-
|
805
|
-
I18n.translations["pt-BR"] = {
|
806
|
-
message: "Uma mensagem especial para você"
|
807
|
-
}
|
64
|
+
```console
|
65
|
+
$ i18n init --config config/i18n.yml
|
66
|
+
$ i18n export --config config/i18n.yml --require config/environment.rb
|
808
67
|
```
|
809
68
|
|
69
|
+
By default, `i18n` will use `config/i18n.yml` and `config/environment.rb` as the
|
70
|
+
configuration files. If you don't have these files, then you'll need to specify
|
71
|
+
both `--config` and `--require`.
|
810
72
|
|
811
|
-
##
|
73
|
+
## Automatically export translations
|
812
74
|
|
813
|
-
###
|
75
|
+
### Using guard
|
814
76
|
|
815
|
-
|
77
|
+
Install [guard](https://rubygems.org/packages/guard) and
|
78
|
+
[guard-compat](https://rubygems.org/packages/guard-compat). Then create a
|
79
|
+
Guardfile with the following configuration:
|
816
80
|
|
817
|
-
|
818
|
-
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
# Or, with older versions of Rails:
|
827
|
-
$ rake tmp:cache:clear
|
828
|
-
```
|
829
|
-
|
830
|
-
These commands will remove *all* fingerprinted assets, and you will have to recompile them with
|
831
|
-
|
832
|
-
```bash
|
833
|
-
$ rake assets:precompile
|
81
|
+
```ruby
|
82
|
+
guard(:"i18n-js",
|
83
|
+
run_on_start: true,
|
84
|
+
config_file: "./config/i18n.yml",
|
85
|
+
require_file: "./config/environment.rb") do
|
86
|
+
watch(%r{^(app|config)/locales/.+\.(yml|po)$})
|
87
|
+
watch(%r{^config/i18n.yml$})
|
88
|
+
watch("Gemfile")
|
89
|
+
end
|
834
90
|
```
|
835
91
|
|
836
|
-
|
837
|
-
|
838
|
-
2. You can change something in a different locale file.
|
839
|
-
|
840
|
-
3. Finally, you can change `config.assets.version`.
|
841
|
-
|
842
|
-
**Note:** See issue [#213](https://github.com/fnando/i18n-js/issues/213) for more details and discussion of this issue.
|
843
|
-
|
844
|
-
### Translations in JS are not updated when Sprockets not loaded before this gem
|
845
|
-
|
846
|
-
The "rails engine" declaration will try to detect existence of "sprockets" before adding the initailizer
|
847
|
-
If sprockets is loaded after this gem, the preprocessor for
|
848
|
-
making JS translations file cache to depend on content of locale files will not be hooked.
|
849
|
-
So ensure sprockets is loaded before this gem like moving entry of sprockets in Gemfile or adding "require" statements for sprockets somewhere.
|
850
|
-
|
851
|
-
**Note:** See issue [#404](https://github.com/fnando/i18n-js/issues/404) for more details and discussion of this issue.
|
92
|
+
If you files in a different location, the remember to configure file paths
|
93
|
+
accordingly.
|
852
94
|
|
853
|
-
|
95
|
+
Now you can run `guard start -i`.
|
854
96
|
|
855
|
-
|
856
|
-
Ref: http://2ality.com/2012/07/large-integers.html
|
97
|
+
### Using listen
|
857
98
|
|
858
|
-
|
859
|
-
|
860
|
-
### Only works with `Simple` backend
|
861
|
-
|
862
|
-
If you set `I18n.backend` to something other than the default `Simple` backend, you will likely get an exception like this:
|
863
|
-
|
864
|
-
```
|
865
|
-
Undefined method 'initialized?' for <your backend class>
|
866
|
-
```
|
867
|
-
|
868
|
-
For now, i18n-js is only compatible with the `Simple` backend.
|
869
|
-
If you need a more sophisticated backend for your rails application, like `I18n::Backend::ActiveRecord`, you can setup i18n-js to get translations from a separate `Simple` backend, by adding the following in an initializer:
|
99
|
+
Create a file under `config/initializers/i18n.rb` with the following content:
|
870
100
|
|
871
101
|
```ruby
|
872
|
-
|
873
|
-
I18n.backend = I18n::Backend::Chain.new(<your other backend(s)>, I18n.backend)
|
874
|
-
```
|
102
|
+
# frozen_string_literal: true
|
875
103
|
|
876
|
-
|
877
|
-
This means however, that only translations from your static locale files will be present in JavaScript.
|
104
|
+
require "i18n-js/listen"
|
878
105
|
|
879
|
-
|
880
|
-
|
881
|
-
```ruby
|
882
|
-
I18n::JS.backend = I18n::Backend::Simple.new
|
883
|
-
I18n.backend = <something different>
|
106
|
+
I18nJS.listen
|
884
107
|
```
|
885
108
|
|
886
|
-
|
887
|
-
|
888
|
-
|
109
|
+
The code above will watch for changes based on `config/i18n.yml` and
|
110
|
+
`config/locales`. You can customize these options with
|
111
|
+
`I18nJS.listen(config_file: "config/i18n.yml", locales_dir: "config/locales")`.
|
889
112
|
|
890
|
-
|
891
|
-
module I18n
|
892
|
-
def self.reload!
|
893
|
-
I18n::JS.backend.reload!
|
894
|
-
super
|
895
|
-
end
|
896
|
-
end
|
897
|
-
```
|
113
|
+
### Integrating with your frontend
|
898
114
|
|
899
|
-
|
115
|
+
You're done exporting files, now what? Well, go to
|
116
|
+
[i18n](https://github.com/fnando/i18n) to discover how to use the NPM package
|
117
|
+
that loads all the exported translation.
|
900
118
|
|
901
119
|
## Maintainer
|
902
120
|
|
903
|
-
- Nando Vieira
|
904
|
-
|
905
|
-
## Contributing
|
906
|
-
|
907
|
-
Once you've made your great commits:
|
121
|
+
- [Nando Vieira](https://github.com/fnando)
|
908
122
|
|
909
|
-
|
910
|
-
2. Create a branch with a clear name
|
911
|
-
3. Make your changes (Please also add/change spec, README and CHANGELOG if applicable)
|
912
|
-
4. Push changes to the created branch
|
913
|
-
5. [Create an Pull Request](http://github.com/fnando/i18n-js/pulls)
|
914
|
-
6. That's it!
|
123
|
+
## Contributors
|
915
124
|
|
916
|
-
|
917
|
-
And use 2 spaces, not tabs. And don't touch the versioning thing.
|
125
|
+
- https://github.com/fnando/i18n-js/contributors
|
918
126
|
|
919
|
-
##
|
920
|
-
|
921
|
-
You can run I18n tests using Node.js or your browser.
|
922
|
-
|
923
|
-
To use Node.js, install the `jasmine-node` library:
|
924
|
-
|
925
|
-
$ npm install jasmine-node
|
926
|
-
|
927
|
-
Then execute the following command from the lib's root directory:
|
127
|
+
## Contributing
|
928
128
|
|
929
|
-
|
129
|
+
For more details about how to contribute, please read
|
130
|
+
https://github.com/fnando/i18n-js/blob/main/CONTRIBUTING.md.
|
930
131
|
|
931
|
-
|
132
|
+
## License
|
932
133
|
|
933
|
-
|
134
|
+
The gem is available as open source under the terms of the
|
135
|
+
[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
|
136
|
+
found at https://github.com/fnando/i18n-js/blob/main/LICENSE.md.
|
934
137
|
|
935
|
-
##
|
138
|
+
## Code of Conduct
|
936
139
|
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
a copy of this software and associated documentation files (the
|
941
|
-
'Software'), to deal in the Software without restriction, including
|
942
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
943
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
944
|
-
permit persons to whom the Software is furnished to do so, subject to
|
945
|
-
the following conditions:
|
946
|
-
|
947
|
-
The above copyright notice and this permission notice shall be
|
948
|
-
included in all copies or substantial portions of the Software.
|
949
|
-
|
950
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
951
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
952
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
953
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
954
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
955
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
956
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
140
|
+
Everyone interacting in the i18n-js project's codebases, issue trackers, chat
|
141
|
+
rooms and mailing lists is expected to follow the
|
142
|
+
[code of conduct](https://github.com/fnando/i18n-js/blob/main/CODE_OF_CONDUCT.md).
|