Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YAML.safe_load config option #1668

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/react_on_rails/locales/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions spec/react_on_rails/fixtures/i18n/locales_symbols/de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
de:
:hello: "Hallo welt"
8 changes: 8 additions & 0 deletions spec/react_on_rails/fixtures/i18n/locales_symbols/en.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions spec/react_on_rails/locales_to_js_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading