Skip to content

Commit

Permalink
Improve i18n Integration
Browse files Browse the repository at this point in the history
* Better error message if the value of the i18n directory is invalid.
* Added test for configuration error message.
* DRY'd up code for checking if this config value is set.
* Improved comments in sample react_on_rails.rb config file
  • Loading branch information
justin808 committed Mar 4, 2017
1 parent 6e3edd1 commit c6e6911
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ ReactOnRails.configure do |config|
################################################################################
# I18N OPTIONS
################################################################################
# Replace the following line to the location where you keep translation.js & default.js.
# Replace the following line to the location where you keep translation.js & default.js for use
# by the npm packages react-intl. Be sure this directory exists!
# config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n")

################################################################################
Expand Down
10 changes: 10 additions & 0 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ def self.setup_config_values
configure_generated_assets_dirs_deprecation
ensure_generated_assets_dir_present
ensure_server_bundle_js_file_has_no_path
check_i18n_directory_exists
end

def self.check_i18n_directory_exists
return unless @configuration.i18n_dir.present?
return if Dir.exist?(@configuration.i18n_dir)

raise "Error configuring /config/react_on_rails.rb: invalid value for `config.i18n_dir`. "\
"Directory does not exist: #{@configuration.i18n_dir}. Set to value to nil or comment it "\
"out if not using this i18n with React on Rails."
end

def self.ensure_generated_assets_dir_present
Expand Down
4 changes: 3 additions & 1 deletion lib/react_on_rails/locales_to_js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module ReactOnRails
class LocalesToJs
def initialize
return unless i18n_dir.present?
return unless obsolete?
@translations, @defaults = generate_translations
convert
Expand Down Expand Up @@ -38,7 +39,8 @@ def js_file(name)
end

def locale_files
@locale_files ||= Rails.application.config.i18n.load_path
@locale_files ||=
(Rails.application && Rails.application.config.i18n.load_path).presence
end

def i18n_dir
Expand Down
4 changes: 1 addition & 3 deletions lib/react_on_rails/test_helper/ensure_assets_compiled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def call
# Be sure we don't do this again.
self.class.has_been_run = true

if ReactOnRails.configuration.i18n_dir.present? && Rails.application.present?
ReactOnRails::LocalesToJs.new
end
ReactOnRails::LocalesToJs.new

stale_gen_files = webpack_assets_status_checker.stale_generated_webpack_files

Expand Down
4 changes: 1 addition & 3 deletions lib/tasks/locale.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ This task generates javascript locale files: `translations.js` & `default.js` an
the "ReactOnRails.configuration.i18n_dir".
DESC
task locale: :environment do
if ReactOnRails.configuration.i18n_dir.present?
ReactOnRails::LocalesToJs.new
end
ReactOnRails::LocalesToJs.new
end
end
3 changes: 2 additions & 1 deletion spec/dummy/config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def self.custom_context(view_context)
################################################################################
# I18N OPTIONS
################################################################################
# Replace the following line to the location where you keep translation.js & default.js.
# Replace the following line to the location where you keep translation.js & default.js for use
# by the npm packages react-intl. Be sure this directory exists!
# config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n")

################################################################################
Expand Down
19 changes: 19 additions & 0 deletions spec/react_on_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

module ReactOnRails
RSpec.describe Configuration do
it "raises if the i18n directory does not exist" do
junk_name = "/XXXX/junkXXXX"
expect do
ReactOnRails.configure do |config|
config.i18n_dir = junk_name
end
end.to raise_error(/#{junk_name}/)
end

it "does not raises if the i18n directory does exist" do
dir = File.expand_path(File.dirname(__FILE__))
expect do
ReactOnRails.configure do |config|
config.i18n_dir = dir
end
end.to_not raise_error
expect(ReactOnRails.configuration.i18n_dir).to eq(dir)
end

it "be able to config default configuration of the gem" do
ReactOnRails.configure do |config|
config.server_bundle_js_file = "server.js"
Expand Down
6 changes: 6 additions & 0 deletions spec/react_on_rails/locales_to_js_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ module ReactOnRails
end
end

after do
ReactOnRails.configure do |config|
config.i18n_dir = nil
end
end

context "with obsolete js files" do
before do
FileUtils.touch(translations_path, mtime: Time.now - 1.year)
Expand Down

0 comments on commit c6e6911

Please sign in to comment.