Skip to content

Commit

Permalink
Create TranslationIO.yaml_load() method to load YAML with aliases u…
Browse files Browse the repository at this point in the history
…sing Psych 3 or 4 (cf. #47)
  • Loading branch information
MichaelHoste committed Apr 4, 2022
1 parent b0c56d9 commit 1bde6ef
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/translation.rb
Expand Up @@ -93,6 +93,15 @@ def normalize_path(relative_or_absolute_path)
File.expand_path(relative_or_absolute_path).gsub("#{Dir.pwd}/", '')
end

# Cf. https://github.com/translation/rails/issues/47
def yaml_load(source)
begin
YAML.load(source, :aliases => true) || {}
rescue ArgumentError
YAML.load(source) || {}
end
end

def version
Gem::Specification::find_by_name('translation').version.to_s
end
Expand Down
Expand Up @@ -43,7 +43,7 @@ def run

# To have a localization.xx.yml file during tests (without call to backend)
if TranslationIO.config.test
if YAML::load(yaml_data).present?
if TranslationIO.yaml_load(yaml_data).present?
File.open(yaml_path, 'wb') do |file|
file.write(self.class.top_comment)
file.write(yaml_data)
Expand All @@ -64,7 +64,7 @@ def run
yaml_path = File.join(@yaml_locales_path, "localization.#{target_locale}.yml")
yaml_data = parsed_response["yaml_data_#{target_locale}"]

if yaml_data.present? && YAML::load(yaml_data).present?
if yaml_data.present? && TranslationIO.yaml_load(yaml_data).present?
File.open(yaml_path, 'wb') do |file|
file.write(self.class.top_comment)
file.write(yaml_data)
Expand Down
Expand Up @@ -13,7 +13,7 @@ def run
@yaml_file_paths.each do |locale_file_path|
if locale_file_removable?(locale_file_path)
if File.exist?(locale_file_path)
content_hash = YAML::load(File.read(locale_file_path)) || {}
content_hash = TranslationIO.yaml_load(File.read(locale_file_path)) || {}
source_content_hash = content_hash.select { |k| k.to_s == @source_locale.to_s }

if source_content_hash.empty?
Expand Down
Expand Up @@ -18,7 +18,7 @@ def run(params)

@yaml_file_paths.each do |file_path|
TranslationIO.info file_path, 2, 2
file_translations = YAML::load(File.read(file_path))
file_translations = TranslationIO.yaml_load(File.read(file_path))

unless file_translations.blank?
all_translations = all_translations.deep_merge(file_translations)
Expand Down
Expand Up @@ -46,7 +46,7 @@ def reload_or_reuse_yaml_sources
if yaml_sources_reload_needed?
@yaml_sources = sort_by_project_locales_first(@yaml_file_paths).collect do |yaml_file_path|
yaml_content = File.read(yaml_file_path)
yaml_hash = YAML::load(yaml_content)
yaml_hash = TranslationIO.yaml_load(yaml_content)
yaml_flat_hash = FlatHash.to_flat_hash(yaml_hash)

{
Expand Down Expand Up @@ -138,7 +138,7 @@ def metadata_timestamp
timestamps = metadata_content.scan(/timestamp: (\d*)/).flatten.uniq.collect(&:to_i)
return timestamps.min || 0
else
return YAML::load(metadata_content)['timestamp'] rescue 0
return YAML.load(metadata_content)['timestamp'] rescue 0
end
else
return 0
Expand Down
Expand Up @@ -14,7 +14,7 @@ def run(params)

@yaml_file_paths.each do |file_path|
TranslationIO.info file_path, 2, 2
file_translations = YAML::load(File.read(file_path))
file_translations = TranslationIO.yaml_load(File.read(file_path))

unless file_translations.blank?
all_translations = all_translations.deep_merge(file_translations)
Expand Down
2 changes: 1 addition & 1 deletion lib/translation_io/yaml_conversion.rb
Expand Up @@ -26,7 +26,7 @@ def get_flat_translations_for_yaml_file(file_path)
end

def get_flat_translations_for_yaml_data(yaml_data)
translations = YAML::load(yaml_data)
translations = TranslationIO.yaml_load(yaml_data)

if translations
return FlatHash.to_flat_hash(translations)
Expand Down
2 changes: 1 addition & 1 deletion spec/translation/yaml_conversion_spec.rb
Expand Up @@ -96,7 +96,7 @@
result.should == {
"en.hello" => "Hello world",
"en.main.menu.stuff" => "This is stuff",
"en.other_main.menu.stuff" => "This is stuff", # alias in origin YAML
"en.other_main.menu.stuff" => "This is stuff", # alias in YAML
"en.bye" => "Good bye world",
"en.empty" => nil,
"en.empty_string" => "",
Expand Down

0 comments on commit 1bde6ef

Please sign in to comment.