Skip to content

Commit

Permalink
Automatically generate javascript files for react-intl when the serve…
Browse files Browse the repository at this point in the history
…r starts up.
  • Loading branch information
JasonYCHuang committed Dec 27, 2016
1 parent 6370447 commit fdff007
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ ReactOnRails.configure do |config|
config.server_renderer_pool_size = 1 # increase if you're on JRuby
config.server_renderer_timeout = 20 # seconds

################################################################################
# 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")
# Default locale
config.default_locale = "en"
# The location of rails locales
config.rails_locales_path = Rails.application.config.i18n.load_path

################################################################################
# MISCELLANEOUS OPTIONS
################################################################################
Expand Down
1 change: 1 addition & 0 deletions lib/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
require "react_on_rails/test_helper/webpack_assets_status_checker"
require "react_on_rails/test_helper/ensure_assets_compiled"
require "react_on_rails/test_helper/node_process_launcher"
require "react_on_rails/locales_to_js"
8 changes: 8 additions & 0 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def self.configuration
server_render_method: "",
symlink_non_digested_assets_regex: /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/,
npm_build_test_command: "",
i18n_dir: "",
default_locale: "",
rails_locales_path: "",
npm_build_production_command: ""
)
end
Expand All @@ -71,6 +74,7 @@ class Configuration
:skip_display_none, :generated_assets_dirs, :generated_assets_dir,
:webpack_generated_files, :rendering_extension, :npm_build_test_command,
:npm_build_production_command,
:i18n_dir, :default_locale, :rails_locales_path,
:server_render_method, :symlink_non_digested_assets_regex

def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
Expand All @@ -81,12 +85,16 @@ def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
generated_assets_dir: nil, webpack_generated_files: nil,
rendering_extension: nil, npm_build_test_command: nil,
npm_build_production_command: nil,
i18n_dir: nil, default_locale: nil, rails_locales_path: nil,
server_render_method: nil, symlink_non_digested_assets_regex: nil)
self.server_bundle_js_file = server_bundle_js_file
self.generated_assets_dirs = generated_assets_dirs
self.generated_assets_dir = generated_assets_dir
self.npm_build_test_command = npm_build_test_command
self.npm_build_production_command = npm_build_production_command
self.i18n_dir = i18n_dir
self.default_locale = default_locale
self.rails_locales_path = rails_locales_path

self.prerender = prerender
self.replay_console = replay_console
Expand Down
104 changes: 104 additions & 0 deletions lib/react_on_rails/locales_to_js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require "erb"

module ReactOnRails
class LocalesToJs
def initialize
@locale_files = set_locale_files
@default_locale = set_default_locale
@translations, @defaults = generate_translations
end

def convert
i18n_js_files.each do |f|
template = send("template_#{f}")
path = send("path_#{f}")
create_js_file(template, path)
end
end

private

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
ReactOnRails.configuration.i18n_dir
end

def set_locale_files
ReactOnRails.configuration.rails_locales_path
end

def set_default_locale
ReactOnRails.configuration.default_locale || "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
10 changes: 10 additions & 0 deletions spec/dummy/config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def self.custom_context(view_context)
config.server_renderer_pool_size = 1 # increase if you're on JRuby
config.server_renderer_timeout = 20 # seconds

################################################################################
# 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")
# Default locale
config.default_locale = "en"
# The location of rails locales
config.rails_locales_path = Rails.application.config.i18n.load_path

################################################################################
# MISCELLANEOUS OPTIONS
################################################################################
Expand Down

0 comments on commit fdff007

Please sign in to comment.