-
-
Notifications
You must be signed in to change notification settings - Fork 632
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
Automatically generate i18n javascript files for react-intl when the serve starts up. #642
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the I18n functionality. | ||
|
||
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. React on Rails will help you to generate or update `translations.js` & `default.js` automatically after you configured the following settings. | ||
|
||
> `translations.js`: All your locales in json format. | ||
> | ||
> `default.js`: Default settings in json format. | ||
> | ||
> You can add them to `.gitignore` and `.eslintignore`. | ||
|
||
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 | ||
``` | ||
|
||
5. In React, you need to initialize `react-intl`, and set parameters for it. | ||
|
||
```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,119 @@ | ||
require "erb" | ||
|
||
module ReactOnRails | ||
class LocalesToJs | ||
def initialize | ||
return unless obsolete? | ||
@translations, @defaults = generate_translations | ||
convert | ||
end | ||
|
||
private | ||
|
||
def obsolete? | ||
return true if exist_js_files.empty? | ||
js_files_are_outdated | ||
end | ||
|
||
def exist_js_files | ||
@exist_js_files ||= js_files.select(&File.method(:exist?)) | ||
end | ||
|
||
def js_files_are_outdated | ||
latest_yml = locale_files.map(&File.method(:mtime)).max | ||
earliest_js = exist_js_files.map(&File.method(:mtime)).min | ||
latest_yml > earliest_js | ||
end | ||
|
||
def js_file_names | ||
%w(translations default) | ||
end | ||
|
||
def js_files | ||
@js_files ||= js_file_names.map { |n| js_file(n) } | ||
end | ||
|
||
def js_file(name) | ||
"#{i18n_dir}/#{name}.js" | ||
end | ||
|
||
def locale_files | ||
@locale_files ||= Rails.application.config.i18n.load_path | ||
end | ||
|
||
def i18n_dir | ||
@i18n_dir ||= ReactOnRails.configuration.i18n_dir | ||
end | ||
|
||
def default_locale | ||
@default_locale ||= I18n.default_locale.to_s || "en" | ||
end | ||
|
||
def convert | ||
js_file_names.each do |name| | ||
template = send("template_#{name}") | ||
path = js_file(name) | ||
generate_js_file(template, path) | ||
end | ||
end | ||
|
||
def generate_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 template_translations | ||
<<-JS | ||
export const translations = #{@translations}; | ||
JS | ||
end | ||
|
||
def template_default | ||
<<-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,54 @@ | ||
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__) } | ||
let(:translations_path) { "#{i18n_dir}/translations.js" } | ||
let(:default_path) { "#{i18n_dir}/default.js" } | ||
let(:en_path) { "#{locale_dir}/en.yml" } | ||
|
||
before do | ||
allow_any_instance_of(ReactOnRails::LocalesToJs).to receive(:locale_files).and_return(Dir["#{locale_dir}/*"]) | ||
ReactOnRails.configure do |config| | ||
config.i18n_dir = i18n_dir | ||
end | ||
end | ||
|
||
context "with obsolete js files" do | ||
before do | ||
FileUtils.touch(translations_path, mtime: Time.now - 1.year) | ||
FileUtils.touch(en_path, mtime: Time.now - 1.month) | ||
end | ||
|
||
it "updates files" do | ||
ReactOnRails::LocalesToJs.new | ||
|
||
translations = File.read(translations_path) | ||
default = File.read(default_path) | ||
expect(translations).to include("{\"hello\":\"Hello world\"") | ||
expect(translations).to include("{\"hello\":\"Hallo welt\"") | ||
expect(default).to include("const defaultLocale = 'en';") | ||
expect(default).to include("{\"hello\":{\"id\":\"hello\",\"defaultMessage\":\"Hello world\"}}") | ||
|
||
expect(File.mtime(translations_path)).to be >= File.mtime(en_path) | ||
end | ||
end | ||
|
||
context "with up-to-date js files" do | ||
before do | ||
ReactOnRails::LocalesToJs.new | ||
end | ||
|
||
it "doesn't update files" do | ||
ref_time = Time.now - 1.minute | ||
FileUtils.touch(translations_path, mtime: ref_time) | ||
|
||
update_time = Time.now | ||
ReactOnRails::LocalesToJs.new | ||
expect(update_time).to be > File.mtime(translations_path) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonYCHuang We added this here, with an invalid directory, and later we abort deploying or testing when this directory does not exist.