-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically generate i18n javascript files for react-intl when the …
…serve starts up
- Loading branch information
1 parent
f929478
commit 093a996
Showing
11 changed files
with
267 additions
and
9 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
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,90 @@ | ||
# How to add I18n | ||
|
||
Here's a summary of adding I18n functionality with ReactOnRails. | ||
|
||
You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial) for a complete example. | ||
|
||
1. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && npm install`. | ||
|
||
```js | ||
"dependencies": { | ||
... | ||
"intl": "^1.2.5", | ||
"react-intl": "^2.1.5", | ||
... | ||
} | ||
``` | ||
|
||
2. In `client/webpack.client.base.config.js`, set `react-intl` as an entry point. | ||
|
||
```js | ||
module.exports = { | ||
... | ||
entry: { | ||
... | ||
vendor: [ | ||
... | ||
'react-intl', | ||
], | ||
... | ||
``` | ||
3. `react-intl` requires locale files in json format. ReactOnRails will create or update this transformation automatically after you finished the following settings. | ||
Update settings in `config/initializers/react_on_rails.rb` to what you need: | ||
```ruby | ||
# Replace the following line to the location where you keep translation.js & default.js. | ||
config.i18n_dir = Rails.root.join("PATH_TO", "YOUR_JS_I18N_FOLDER") | ||
``` | ||
Add following lines to `config/application.rb`, this will help you to generate `translations.js` & `default.js` automatically when you starts the server. | ||
```js | ||
module YourModule | ||
class Application < Rails::Application | ||
... | ||
config.after_initialize do | ||
ReactOnRails::LocalesToJs.new | ||
end | ||
end | ||
end | ||
``` | ||
4. In React, you need to initialize `react-intl`, and set parameters for it. | ||
> `translations.js`: All your locales in json format. | ||
> | ||
> `default.js`: [1] `defaultLocale` is your default locale, like "en". [2] `defaultMessages` is the place where you can get your local values with localeKeyInCamelForm, and it also contains fallback when something went wrong. | ||
> | ||
> There is no need to track and lint `translations.js` & `default.js`, and you can add them to `.gitignore` and `.eslintignore`. | ||
```js | ||
... | ||
import { addLocaleData } from 'react-intl'; | ||
import en from 'react-intl/locale-data/en'; | ||
import de from 'react-intl/locale-data/de'; | ||
import { translations } from 'path_to/i18n/translations'; | ||
import { defaultLocale } from 'path_to/i18n/default'; | ||
... | ||
// Initizalize all locales for react-intl. | ||
addLocaleData([...en, ...de]); | ||
... | ||
// set locale and messages for IntlProvider. | ||
const locale = method_to_get_current_locale() || defaultLocale; | ||
const messages = translations[locale]; | ||
... | ||
return ( | ||
<IntlProvider locale={locale} key={locale} messages={messages}> | ||
<CommentScreen {...{ actions, data }} /> | ||
</IntlProvider> | ||
) | ||
``` | ||
```js | ||
// In your component. | ||
import { defaultMessages } from 'path_to/i18n/default'; | ||
... | ||
return ( | ||
{ formatMessage(defaultMessages.yourLocaleKeyInCamelCase) } | ||
) | ||
``` |
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
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,115 @@ | ||
require "erb" | ||
|
||
module ReactOnRails | ||
class LocalesToJs | ||
def initialize | ||
return unless obsolete? | ||
@translations, @defaults = generate_translations | ||
convert | ||
end | ||
|
||
private | ||
|
||
def obsolete? | ||
@obsolete ||= (latest_file_names - i18n_js_files.map { |f| f.tr("_", ".") }).present? | ||
end | ||
|
||
def latest_file_names | ||
files = locale_files + i18n_js_files.map { |f| send("path_#{f}").to_s } | ||
.select { |f| File.exist?(f) } | ||
files = files.sort_by { |f| File.mtime(f) } | ||
files.last(2).map { |f| File.basename(f) } | ||
end | ||
|
||
def convert | ||
i18n_js_files.each do |f| | ||
template = send("template_#{f}") | ||
path = send("path_#{f}") | ||
create_js_file(template, path) | ||
end | ||
end | ||
|
||
def i18n_js_files | ||
%w(translations_js default_js) | ||
end | ||
|
||
def create_js_file(template, path) | ||
result = ERB.new(template).result() | ||
File.open(path, "w") do |f| | ||
f.write(result) | ||
end | ||
end | ||
|
||
def generate_translations | ||
translations = {} | ||
defaults = {} | ||
locale_files.each do |f| | ||
translation = YAML.load(File.open(f)) | ||
key = translation.keys[0] | ||
val = flatten(translation[key]) | ||
translations = translations.deep_merge(key => val) | ||
defaults = defaults.deep_merge(flatten_defaults(val)) if key == default_locale | ||
end | ||
[translations.to_json, defaults.to_json] | ||
end | ||
|
||
def format(input) | ||
input.to_s.tr(".", "_").camelize(:lower).to_sym | ||
end | ||
|
||
def flatten_defaults(val) | ||
flatten(val).each_with_object({}) do |(k, v), h| | ||
key = format(k) | ||
h[key] = { id: k, defaultMessage: v } | ||
end | ||
end | ||
|
||
def flatten(translations) | ||
translations.each_with_object({}) do |(k, v), h| | ||
if v.is_a? Hash | ||
flatten(v).map { |hk, hv| h["#{k}.#{hk}".to_sym] = hv } | ||
else | ||
h[k] = v | ||
end | ||
end | ||
end | ||
|
||
def i18n_dir | ||
@i18n_dir ||= ReactOnRails.configuration.i18n_dir | ||
end | ||
|
||
def locale_files | ||
@locale_files ||= Rails.application.config.i18n.load_path | ||
end | ||
|
||
def default_locale | ||
@default_locale ||= I18n.default_locale.to_s || "en" | ||
end | ||
|
||
def path_translations_js | ||
i18n_dir + "translations.js" | ||
end | ||
|
||
def path_default_js | ||
i18n_dir + "default.js" | ||
end | ||
|
||
def template_translations_js | ||
<<-JS | ||
export const translations = #{@translations}; | ||
JS | ||
end | ||
|
||
def template_default_js | ||
<<-JS | ||
import { defineMessages } from 'react-intl'; | ||
const defaultLocale = \'#{default_locale}\'; | ||
const defaultMessages = defineMessages(#{@defaults}); | ||
export { defaultMessages, defaultLocale }; | ||
JS | ||
end | ||
end | ||
end |
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,2 @@ | ||
de: | ||
hello: "Hallo welt" |
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,2 @@ | ||
en: | ||
hello: "Hello world" |
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,30 @@ | ||
require_relative "spec_helper" | ||
require "tmpdir" | ||
|
||
module ReactOnRails | ||
RSpec.describe LocalesToJs do | ||
let(:i18n_dir) { Pathname.new(Dir.mktmpdir) } | ||
let(:locale_dir) { File.expand_path("../fixtures/i18n/locales", __FILE__) } | ||
|
||
before do | ||
ReactOnRails::LocalesToJs.any_instance.stub(:locale_files).and_return(Dir["#{locale_dir}/*"]) | ||
ReactOnRails.configure do |config| | ||
config.i18n_dir = i18n_dir | ||
end | ||
end | ||
|
||
it "generates translations.js & default.js" do | ||
ReactOnRails::LocalesToJs.new | ||
|
||
files = Dir["#{i18n_dir.to_path}/*"].map { |p| Pathname.new(p).basename.to_s } | ||
expect(files).to include("translations.js", "default.js") | ||
|
||
result_translations = File.read("#{i18n_dir.to_path}/translations.js") | ||
result_default = File.read("#{i18n_dir.to_path}/default.js") | ||
expect(result_translations).to include("{\"hello\":\"Hello world\"") | ||
expect(result_translations).to include("{\"hello\":\"Hallo welt\"") | ||
expect(result_default).to include("const defaultLocale = 'en';") | ||
expect(result_default).to include("{\"hello\":{\"id\":\"hello\",\"defaultMessage\":\"Hello world\"}}") | ||
end | ||
end | ||
end |