Skip to content

Commit 977b131

Browse files
committed
Add YAML.safe_load config option
1 parent 4339d30 commit 977b131

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

lib/react_on_rails/configuration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Configuration
5252
:generated_assets_dirs, :generated_assets_dir, :components_subdirectory,
5353
:webpack_generated_files, :rendering_extension, :build_test_command,
5454
:build_production_command, :i18n_dir, :i18n_yml_dir, :i18n_output_format,
55+
:i18n_yml_safe_load_options,
5556
:server_render_method, :random_dom_id, :auto_load_bundle,
5657
:same_bundle_for_client_and_server, :rendering_props_extension,
5758
:make_generated_server_bundle_the_entrypoint,
@@ -69,7 +70,7 @@ def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender
6970
rendering_extension: nil, build_test_command: nil,
7071
build_production_command: nil, defer_generated_component_packs: nil,
7172
same_bundle_for_client_and_server: nil,
72-
i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil,
73+
i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, i18n_yml_safe_load_options: nil,
7374
random_dom_id: nil, server_render_method: nil, rendering_props_extension: nil,
7475
components_subdirectory: nil, auto_load_bundle: nil, force_load: nil)
7576
self.node_modules_location = node_modules_location.present? ? node_modules_location : Rails.root
@@ -80,6 +81,7 @@ def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender
8081
self.i18n_dir = i18n_dir
8182
self.i18n_yml_dir = i18n_yml_dir
8283
self.i18n_output_format = i18n_output_format
84+
self.i18n_yml_safe_load_options = i18n_yml_safe_load_options
8385

8486
self.random_dom_id = random_dom_id
8587
self.prerender = prerender

lib/react_on_rails/locales/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def generate_translations
115115
translations = {}
116116
defaults = {}
117117
locale_files.each do |f|
118-
translation = YAML.safe_load(File.open(f))
118+
safe_load_options = ReactOnRails.configuration.i18n_yml_safe_load_options || {}
119+
translation = YAML.safe_load(File.open(f), **safe_load_options)
119120
key = translation.keys[0]
120121
val = flatten(translation[key])
121122
translations = translations.deep_merge(key => val)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
de:
2+
:hello: "Hallo welt"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
en:
2+
:hello: "Hello world"
3+
:argument: "I am %{age} years old."
4+
:day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
5+
:blank:
6+
:number: 2
7+
:bool: true
8+
:float: 2.0

spec/react_on_rails/locales_to_js_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,25 @@ module ReactOnRails
9696

9797
it_behaves_like "locale to js"
9898
end
99+
100+
describe "with symbols in yaml" do
101+
before do
102+
ReactOnRails.configure do |config|
103+
config.i18n_yml_dir = File.expand_path("fixtures/i18n/locales_symbols", __dir__)
104+
config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
105+
end
106+
end
107+
108+
after do
109+
ReactOnRails.configure do |config|
110+
config.i18n_yml_dir = nil
111+
config.i18n_yml_safe_load_options = nil
112+
end
113+
end
114+
115+
it "handles locale loading" do
116+
expect { described_class.new }.not_to raise_error
117+
end
118+
end
99119
end
100120
end

0 commit comments

Comments
 (0)