Skip to content

Commit c367290

Browse files
committed
Automatically generate javascript files for react-intl when the server starts up.
1 parent 6370447 commit c367290

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ ReactOnRails.configure do |config|
5959
config.server_renderer_pool_size = 1 # increase if you're on JRuby
6060
config.server_renderer_timeout = 20 # seconds
6161

62+
################################################################################
63+
# I18N OPTIONS
64+
################################################################################
65+
# Replace the following line to the location where you keep translation.js & default.js.
66+
config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n")
67+
# Default locale
68+
config.default_locale = "en"
69+
# The location of rails locales
70+
config.rails_locales_path = Rails.application.config.i18n.load_path
71+
6272
################################################################################
6373
# MISCELLANEOUS OPTIONS
6474
################################################################################

lib/react_on_rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
require "react_on_rails/test_helper/webpack_assets_status_checker"
1717
require "react_on_rails/test_helper/ensure_assets_compiled"
1818
require "react_on_rails/test_helper/node_process_launcher"
19+
require "react_on_rails/locales_to_js"

lib/react_on_rails/configuration.rb

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def self.configuration
5959
server_render_method: "",
6060
symlink_non_digested_assets_regex: /\.(png|jpg|jpeg|gif|tiff|woff|ttf|eot|svg|map)/,
6161
npm_build_test_command: "",
62+
i18n_dir: "",
63+
default_locale: "",
64+
rails_locales_path: "",
6265
npm_build_production_command: ""
6366
)
6467
end
@@ -71,6 +74,7 @@ class Configuration
7174
:skip_display_none, :generated_assets_dirs, :generated_assets_dir,
7275
:webpack_generated_files, :rendering_extension, :npm_build_test_command,
7376
:npm_build_production_command,
77+
:i18n_dir, :default_locale, :rails_locales_path,
7478
:server_render_method, :symlink_non_digested_assets_regex
7579

7680
def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
@@ -81,12 +85,16 @@ def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
8185
generated_assets_dir: nil, webpack_generated_files: nil,
8286
rendering_extension: nil, npm_build_test_command: nil,
8387
npm_build_production_command: nil,
88+
i18n_dir: nil, default_locale: nil, rails_locales_path: nil,
8489
server_render_method: nil, symlink_non_digested_assets_regex: nil)
8590
self.server_bundle_js_file = server_bundle_js_file
8691
self.generated_assets_dirs = generated_assets_dirs
8792
self.generated_assets_dir = generated_assets_dir
8893
self.npm_build_test_command = npm_build_test_command
8994
self.npm_build_production_command = npm_build_production_command
95+
self.i18n_dir = i18n_dir
96+
self.default_locale = default_locale
97+
self.rails_locales_path = rails_locales_path
9098

9199
self.prerender = prerender
92100
self.replay_console = replay_console

lib/react_on_rails/locales_to_js.rb

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require "erb"
2+
3+
module ReactOnRails
4+
class LocalesToJs
5+
def initialize
6+
@locale_files = set_locale_files
7+
@default_locale = set_default_locale
8+
@translations, @defaults = generate_translations
9+
end
10+
11+
def convert
12+
i18n_js_files.each do |f|
13+
template = send("template_#{f}")
14+
path = send("path_#{f}")
15+
create_js_file(template, path)
16+
end
17+
end
18+
19+
private
20+
21+
def i18n_js_files
22+
%w(translations_js default_js)
23+
end
24+
25+
def create_js_file(template, path)
26+
result = ERB.new(template).result()
27+
File.open(path, "w") do |f|
28+
f.write(result)
29+
end
30+
end
31+
32+
def generate_translations
33+
translations = {}
34+
defaults = {}
35+
@locale_files.each do |f|
36+
translation = YAML.load(File.open(f))
37+
key = translation.keys[0]
38+
val = flatten(translation[key])
39+
translations = translations.deep_merge(key => val)
40+
defaults = defaults.deep_merge(flatten_defaults(val)) if key == @default_locale
41+
end
42+
[translations.to_json, defaults.to_json]
43+
end
44+
45+
def format(input)
46+
input.to_s.tr(".", "_").camelize(:lower).to_sym
47+
end
48+
49+
def flatten_defaults(val)
50+
flatten(val).each_with_object({}) do |(k, v), h|
51+
key = format(k)
52+
h[key] = { id: k, defaultMessage: v }
53+
end
54+
end
55+
56+
def flatten(translations)
57+
translations.each_with_object({}) do |(k, v), h|
58+
if v.is_a? Hash
59+
flatten(v).map { |hk, hv| h["#{k}.#{hk}".to_sym] = hv }
60+
else
61+
h[k] = v
62+
end
63+
end
64+
end
65+
66+
def i18n_dir
67+
ReactOnRails.configuration.i18n_dir
68+
end
69+
70+
def set_locale_files
71+
ReactOnRails.configuration.rails_locales_path
72+
end
73+
74+
def set_default_locale
75+
ReactOnRails.configuration.default_locale || "en"
76+
end
77+
78+
def path_translations_js
79+
i18n_dir + "translations.js"
80+
end
81+
82+
def path_default_js
83+
i18n_dir + "default.js"
84+
end
85+
86+
def template_translations_js
87+
<<-JS
88+
export const translations = #{@translations};
89+
JS
90+
end
91+
92+
def template_default_js
93+
<<-JS
94+
import { defineMessages } from 'react-intl';
95+
96+
const defaultLocale = \'#{@default_locale}\';
97+
98+
const defaultMessages = defineMessages(#{@defaults});
99+
100+
export { defaultMessages, defaultLocale };
101+
JS
102+
end
103+
end
104+
end

spec/dummy/config/initializers/react_on_rails.rb

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ def self.custom_context(view_context)
7171
config.server_renderer_pool_size = 1 # increase if you're on JRuby
7272
config.server_renderer_timeout = 20 # seconds
7373

74+
################################################################################
75+
# I18N OPTIONS
76+
################################################################################
77+
# Replace the following line to the location where you keep translation.js & default.js.
78+
config.i18n_dir = Rails.root.join("client", "app", "libs", "i18n")
79+
# Default locale
80+
config.default_locale = "en"
81+
# The location of rails locales
82+
config.rails_locales_path = Rails.application.config.i18n.load_path
83+
7484
################################################################################
7585
# MISCELLANEOUS OPTIONS
7686
################################################################################

0 commit comments

Comments
 (0)