From 9ef0558237246b1461e1939fd803b80cf882b890 Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Wed, 15 Feb 2017 12:34:11 +0100 Subject: [PATCH 1/4] add locale rake task --- lib/tasks/assets.rake | 2 +- lib/tasks/locale.rake | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/locale.rake diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index c1a255134..e62ae587b 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -50,7 +50,7 @@ Compile assets with webpack Uses command defined with ReactOnRails.configuration.npm_build_production_command sh "cd client && `ReactOnRails.configuration.npm_build_production_command`" DESC - task webpack: :environment do + task webpack: :locale do if ReactOnRails.configuration.npm_build_production_command.present? sh "cd client && #{ReactOnRails.configuration.npm_build_production_command}" end diff --git a/lib/tasks/locale.rake b/lib/tasks/locale.rake new file mode 100644 index 000000000..ec8b58c29 --- /dev/null +++ b/lib/tasks/locale.rake @@ -0,0 +1,14 @@ +require "react_on_rails/locales_to_js" + +namespace :react_on_rails do + desc <<-DESC +Generate i18n javascript files +This task generates javascript locale files: `translations.js` & `default.js` and places them in +the "ReactOnRails.configuration.i18n_dir". + DESC + task locale: :environment do + if ReactOnRails.configuration.i18n_dir.present? + ReactOnRails::LocalesToJs.new + end + end +end From 6a9c55fba518e400be77855191355d64dfc75b18 Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Mon, 27 Feb 2017 14:01:40 +0100 Subject: [PATCH 2/4] update Procfile.dev.tt & react_on_rails.rb.tt --- .../react_on_rails/templates/base/base/Procfile.dev.tt | 2 +- .../base/base/config/initializers/react_on_rails.rb.tt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt b/lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt index 4db8f5d86..c4a7691d8 100644 --- a/lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt +++ b/lib/generators/react_on_rails/templates/base/base/Procfile.dev.tt @@ -1,2 +1,2 @@ web: rails s -p 3000 -client: sh -c 'rm app/assets/webpack/* || true && cd client && yarn run build:development' +client: sh -c 'rm app/assets/webpack/* || true && cd client && bundle exec rake react_on_rails:locale && yarn run build:development' diff --git a/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt b/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt index ef5cfd834..cafd63bbe 100644 --- a/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt +++ b/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt @@ -63,7 +63,7 @@ ReactOnRails.configure do |config| # I18N OPTIONS ################################################################################ # Replace the following line to the location where you keep translation.js & default.js. - config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n") + # config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n") ################################################################################ # MISCELLANEOUS OPTIONS From 94e176e671cf6b29c567ce4ece2d8c10a15b8376 Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Tue, 21 Feb 2017 13:54:23 +0100 Subject: [PATCH 3/4] update docs --- CHANGELOG.md | 3 +++ docs/basics/i18n.md | 17 +++++------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d04c0e84a..1f31bd855 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com] ## [Unreleased] *Please add entries here for your pull requests.* +### Added +- Allow using rake task to generate javascript locale files. The test helper automatically creates the localization files when needed. [#717](https://github.com/shakacode/react_on_rails/pull/717) by [JasonYCHuang](https://github.com/JasonYCHuang). + ### Fixed - Upgrade Rails to 4.2.8 to fix security vulnerabilities in 4.2.5. [#735](https://github.com/shakacode/react_on_rails/pull/735) by [hrishimittal](https://github.com/hrishimittal). diff --git a/docs/basics/i18n.md b/docs/basics/i18n.md index 12a29f78f..b6448d378 100644 --- a/docs/basics/i18n.md +++ b/docs/basics/i18n.md @@ -4,7 +4,7 @@ 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) and [PR #340](https://github.com/shakacode/react-webpack-rails-tutorial/pull/340) for a complete example. -1. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && npm install`. +1. Add `react-intl` & `intl` to `client/package.json`, and remember to `bundle && yarn install`. ```js "dependencies": { @@ -44,18 +44,11 @@ You can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/rea 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. +4. Javascript locale files must be generated before `yarn build`. + + Once you setup `config.i18n_dir` as in the previous step, react_on_rails will help you to do this for testing and for production deployments. For development, you should adjust your Procfiles so that they run `bundle exec rake react_on_rails:locale` before running the `yarn run build:development` (webpack watch process) - ```js - module YourModule - class Application < Rails::Application - ... - config.after_initialize do - ReactOnRails::LocalesToJs.new - end - end - end - ``` + If you create your own Procfile files, you must add `bundle exec rake react_on_rails:locale` before `yarn build`. 5. In React, you need to initialize `react-intl`, and set parameters for it. From a10e4db97483ff5dc57fd41f8bca92dc1ba762cd Mon Sep 17 00:00:00 2001 From: jasonych99 Date: Tue, 28 Feb 2017 16:39:53 +0100 Subject: [PATCH 4/4] For testing, a call to generate i18n locale files --- lib/react_on_rails/test_helper/ensure_assets_compiled.rb | 2 ++ spec/dummy/config/initializers/react_on_rails.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/react_on_rails/test_helper/ensure_assets_compiled.rb b/lib/react_on_rails/test_helper/ensure_assets_compiled.rb index c6abdff23..e18d0730b 100644 --- a/lib/react_on_rails/test_helper/ensure_assets_compiled.rb +++ b/lib/react_on_rails/test_helper/ensure_assets_compiled.rb @@ -27,6 +27,8 @@ def call # Be sure we don't do this again. self.class.has_been_run = true + ReactOnRails::LocalesToJs.new if ReactOnRails.configuration.i18n_dir.present? + stale_gen_files = webpack_assets_status_checker.stale_generated_webpack_files # All done if no stale files! diff --git a/spec/dummy/config/initializers/react_on_rails.rb b/spec/dummy/config/initializers/react_on_rails.rb index b9bf8b0a8..2883b63b9 100644 --- a/spec/dummy/config/initializers/react_on_rails.rb +++ b/spec/dummy/config/initializers/react_on_rails.rb @@ -75,7 +75,7 @@ def self.custom_context(view_context) # I18N OPTIONS ################################################################################ # Replace the following line to the location where you keep translation.js & default.js. - config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n") + # config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n") ################################################################################ # MISCELLANEOUS OPTIONS