From 977b131ef9cdf4ff511ef417999bcc1eb35198cb Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Mon, 30 Dec 2024 20:40:32 +0200 Subject: [PATCH] Add YAML.safe_load config option --- lib/react_on_rails/configuration.rb | 4 +++- lib/react_on_rails/locales/base.rb | 3 ++- .../fixtures/i18n/locales_symbols/de.yml | 2 ++ .../fixtures/i18n/locales_symbols/en.yml | 8 ++++++++ spec/react_on_rails/locales_to_js_spec.rb | 20 +++++++++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml create mode 100644 spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml diff --git a/lib/react_on_rails/configuration.rb b/lib/react_on_rails/configuration.rb index 2a468c413..a26971c30 100644 --- a/lib/react_on_rails/configuration.rb +++ b/lib/react_on_rails/configuration.rb @@ -52,6 +52,7 @@ class Configuration :generated_assets_dirs, :generated_assets_dir, :components_subdirectory, :webpack_generated_files, :rendering_extension, :build_test_command, :build_production_command, :i18n_dir, :i18n_yml_dir, :i18n_output_format, + :i18n_yml_safe_load_options, :server_render_method, :random_dom_id, :auto_load_bundle, :same_bundle_for_client_and_server, :rendering_props_extension, :make_generated_server_bundle_the_entrypoint, @@ -69,7 +70,7 @@ def initialize(node_modules_location: nil, server_bundle_js_file: nil, prerender rendering_extension: nil, build_test_command: nil, build_production_command: nil, defer_generated_component_packs: nil, same_bundle_for_client_and_server: nil, - i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, + i18n_dir: nil, i18n_yml_dir: nil, i18n_output_format: nil, i18n_yml_safe_load_options: nil, random_dom_id: nil, server_render_method: nil, rendering_props_extension: nil, components_subdirectory: nil, auto_load_bundle: nil, force_load: nil) 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 self.i18n_dir = i18n_dir self.i18n_yml_dir = i18n_yml_dir self.i18n_output_format = i18n_output_format + self.i18n_yml_safe_load_options = i18n_yml_safe_load_options self.random_dom_id = random_dom_id self.prerender = prerender diff --git a/lib/react_on_rails/locales/base.rb b/lib/react_on_rails/locales/base.rb index f434d777e..868f7e3e1 100644 --- a/lib/react_on_rails/locales/base.rb +++ b/lib/react_on_rails/locales/base.rb @@ -115,7 +115,8 @@ def generate_translations translations = {} defaults = {} locale_files.each do |f| - translation = YAML.safe_load(File.open(f)) + safe_load_options = ReactOnRails.configuration.i18n_yml_safe_load_options || {} + translation = YAML.safe_load(File.open(f), **safe_load_options) key = translation.keys[0] val = flatten(translation[key]) translations = translations.deep_merge(key => val) diff --git a/spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml b/spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml new file mode 100644 index 000000000..fc9fa1250 --- /dev/null +++ b/spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml @@ -0,0 +1,2 @@ +de: + :hello: "Hallo welt" diff --git a/spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml b/spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml new file mode 100644 index 000000000..ecd24dd18 --- /dev/null +++ b/spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml @@ -0,0 +1,8 @@ +en: + :hello: "Hello world" + :argument: "I am %{age} years old." + :day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] + :blank: + :number: 2 + :bool: true + :float: 2.0 diff --git a/spec/react_on_rails/locales_to_js_spec.rb b/spec/react_on_rails/locales_to_js_spec.rb index e33e3d2b5..85e42d02c 100644 --- a/spec/react_on_rails/locales_to_js_spec.rb +++ b/spec/react_on_rails/locales_to_js_spec.rb @@ -96,5 +96,25 @@ module ReactOnRails it_behaves_like "locale to js" end + + describe "with symbols in yaml" do + before do + ReactOnRails.configure do |config| + config.i18n_yml_dir = File.expand_path("fixtures/i18n/locales_symbols", __dir__) + config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] } + end + end + + after do + ReactOnRails.configure do |config| + config.i18n_yml_dir = nil + config.i18n_yml_safe_load_options = nil + end + end + + it "handles locale loading" do + expect { described_class.new }.not_to raise_error + end + end end end