Skip to content

Commit

Permalink
add locale.rake (#717)
Browse files Browse the repository at this point in the history
* add locale rake task
* update Procfile.dev.tt & react_on_rails.rb.tt
* update docs
* For testing and production deployment, automatically call to generate i18n locale files
  • Loading branch information
JasonYCHuang authored and justin808 committed Mar 1, 2017
1 parent 8e47c79 commit b966f8b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
17 changes: 5 additions & 12 deletions docs/basics/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/react_on_rails/test_helper/ensure_assets_compiled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/locale.rake
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/dummy/config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b966f8b

Please sign in to comment.