FIX: Theme components should work with empty locale files (#18167)

Theme components didn't work with empty locale files (just the locale key without any translations) that are created by translator-bot.
This commit is contained in:
Gerhard Schlager 2022-09-02 18:28:18 +02:00 committed by GitHub
parent 73a2d2e9ac
commit fd6109a6e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View File

@ -6,7 +6,7 @@ require 'json_schemer'
class Theme < ActiveRecord::Base class Theme < ActiveRecord::Base
include GlobalPath include GlobalPath
BASE_COMPILER_VERSION = 61 BASE_COMPILER_VERSION = 62
attr_accessor :child_components attr_accessor :child_components

View File

@ -10,7 +10,7 @@ class ThemeTranslationParser
end end
def self.check_contains_hashes(hash) def self.check_contains_hashes(hash)
hash.all? { |key, value| value.is_a?(String) || (value.is_a?(Hash) && self.check_contains_hashes(value)) } hash.all? { |_key, value| value.is_a?(String) || (value.is_a?(Hash) && self.check_contains_hashes(value)) }
end end
def load def load
@ -21,13 +21,18 @@ class ThemeTranslationParser
rescue Psych::SyntaxError, Psych::DisallowedClass => e rescue Psych::SyntaxError, Psych::DisallowedClass => e
raise InvalidYaml.new(e.message) raise InvalidYaml.new(e.message)
end end
raise InvalidYaml.new(I18n.t("themes.locale_errors.invalid_yaml")) unless parsed.is_a?(Hash) && ThemeTranslationParser.check_contains_hashes(parsed)
raise InvalidYaml.new(I18n.t("themes.locale_errors.top_level_locale")) unless parsed.keys.length == 1 && parsed.keys[0] == @setting_field.name
raise InvalidYaml.new(I18n.t("themes.locale_errors.invalid_yaml")) if !parsed.is_a?(Hash)
raise InvalidYaml.new(I18n.t("themes.locale_errors.top_level_locale")) if parsed.keys.length != 1 || parsed.keys.first != @setting_field.name
key = @setting_field.name.to_sym
parsed.deep_symbolize_keys! parsed.deep_symbolize_keys!
parsed[key] ||= {}
parsed[@setting_field.name.to_sym].slice!(*INTERNAL_KEYS) if @internal raise InvalidYaml.new(I18n.t("themes.locale_errors.invalid_yaml")) if !ThemeTranslationParser.check_contains_hashes(parsed)
parsed[@setting_field.name.to_sym].except!(*INTERNAL_KEYS) if !@internal
parsed[key].slice!(*INTERNAL_KEYS) if @internal
parsed[key].except!(*INTERNAL_KEYS) if !@internal
parsed parsed
end end

View File

@ -342,6 +342,14 @@ HTML
fr1.update(value: "fr: 'valuewithoutclosequote") fr1.update(value: "fr: 'valuewithoutclosequote")
expect { fr1.raw_translation_data }.to raise_error(ThemeTranslationParser::InvalidYaml) expect { fr1.raw_translation_data }.to raise_error(ThemeTranslationParser::InvalidYaml)
end end
it "works when locale file doesn't contain translations" do
fr1.update(value: "fr:")
expect(fr1.translation_data).to eq(
fr: {},
en: { somestring1: "helloworld", group: { key1: "enval1" } }
)
end
end end
describe "#translation_data" do describe "#translation_data" do