class WcaI18n::Translation
Attributes
data[RW]
locale[RW]
Public Class Methods
hash_translation(value)
click to toggle source
# File lib/wca_i18n/translation.rb, line 19 def self.hash_translation(value) # If the key is a pluralization, we use all the subkeys to compute the hash # Please see this wiki page explaining why we do this: https://github.com/thewca/worldcubeassociation.org/wikigTranslating-the-website#translations-status-internals to_digest = pluralization?(value) ? JSON.generate(value) : value original_str = Digest::SHA1.hexdigest(to_digest)[0..6] end
new(locale, file_content)
click to toggle source
# File lib/wca_i18n/translation.rb, line 10 def initialize(locale, file_content) self.locale = locale.to_s self.data = YAMLToEnrichedRubyHash.parse(file_content) end
pluralization?(node)
click to toggle source
# File lib/wca_i18n/translation.rb, line 80 def self.pluralization?(node) node.is_a?(Hash) && (node.keys & PLURALIZATION_KEYS).any? end
Public Instance Methods
compare_to(base)
click to toggle source
# File lib/wca_i18n/translation.rb, line 15 def compare_to(base) return diff_recursive(base.data[base.locale], self.data[self.locale], []) end
Private Instance Methods
diff_recursive(base, translation, context)
click to toggle source
# File lib/wca_i18n/translation.rb, line 26 def diff_recursive(base, translation, context) diff = { missing: [], unused: [], outdated: [] } base_leaf = base.is_a?(TranslatedLeaf) translation_leaf = translation.is_a?(TranslatedLeaf) if !base diff[:unused] += get_all_recursive(translation, context) elsif !translation diff[:missing] += get_all_recursive(base, context) elsif base_leaf && translation_leaf if translation.translated.nil? diff[:missing] << context elsif self.class.hash_translation(base.translated) != translation.original_hash diff[:outdated] << context end elsif base_leaf && !translation_leaf diff[:missing] << context diff[:unused] += get_all_recursive(translation, context) elsif !base_leaf && translation_leaf diff[:missing] += get_all_recursive(base, context) diff[:unused] << context else (base.keys | translation.keys).each do |key| merge_diffs!(diff, diff_recursive(base[key], translation[key], [*context, key])) end end diff end
extract_original_hash_from_comment(comment)
click to toggle source
# File lib/wca_i18n/translation.rb, line 69 def extract_original_hash_from_comment(comment) hashes = comment.scan(/original_hash:\s*(.+)/).flatten(1) if hashes.size == 0 nil elsif hashes.size == 1 hashes.first elsif hashes.size > 1 throw "Too many #{HASHTAG} occurrences in: #{comment}" end end
get_all_recursive(node, context)
click to toggle source
# File lib/wca_i18n/translation.rb, line 63 def get_all_recursive(node, context) return [ context ] if node.is_a?(TranslatedLeaf) node.map { |key, value| get_all_recursive(value, [*context, key]) }.flatten(1) end
merge_diffs!(diff, other_diff)
click to toggle source
# File lib/wca_i18n/translation.rb, line 56 def merge_diffs!(diff, other_diff) other_diff.each do |key, value| diff[key] += value end diff end