Module: LocaleNinja::LocaleHelper
- Defined in:
- app/helpers/locale_ninja/locale_helper.rb
Class Method Summary collapse
- .all_keys(github_service, branch: 'translations') ⇒ Object
- .all_keys_for_locales(github_service, locales, branch: 'translations') ⇒ Object
- .hash2keys(hash, parent_key = nil) ⇒ Object
- .keys2yml(translation_keys) ⇒ Object
- .locales(github_service) ⇒ Object
- .locales_count(github_service) ⇒ Object
- .missing_keys(locale, github_service, branch:) ⇒ Object
- .pull_one_locale(locale, github_service, branch: 'translations') ⇒ Object
- .traverse(hash, parent_key = nil) ⇒ Object
Class Method Details
.all_keys(github_service, branch: 'translations') ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 23 def self.all_keys(github_service, branch: 'translations') locales_yml = github_service.pull(branch:).transform_values { |file| YAML.load(file) } locales_list = locales_yml.values.map(&:keys).flatten.uniq locales_yml.flat_map do |path, file| path = path.gsub(/\b(#{locales_list.join('|')})\b/, '%<locale>s') hash2keys(file.values.first).map { |key| "#{path}$%<locale>s.#{key}" } end.uniq end |
.all_keys_for_locales(github_service, locales, branch: 'translations') ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 32 def self.all_keys_for_locales(github_service, locales, branch: 'translations') locales_yml = github_service.pull(branch:).transform_values { |file| YAML.load(file) } locales_list = locales_yml.values.map(&:keys).flatten.uniq locales_yml.transform_values! { |hash| [hash.keys.first, traverse(hash.values.first).to_h] } generic_keys = locales_yml.flat_map do |path, file| path = path.gsub(/\b(#{locales_list.join('|')})\b/, '%<locale>s') _, hash = file hash.map { |key, _hash| "#{path}$%<locale>s.#{key}" } end.uniq translations = locales_yml.flat_map do |path, (locale, hash)| hash.map do |key, value| ["#{path}$#{locale}.#{key}", value] end end.to_h locales.map do |locale| generic_keys.to_h do |key| locale_key = format(key, locale:) [locale_key, translations[locale_key]] end end end |
.hash2keys(hash, parent_key = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 82 def self.hash2keys(hash, parent_key = nil) keys = [] hash.each do |key, value| current_key = parent_key ? "#{parent_key}.#{key}" : key.to_s if value.is_a?(Hash) keys += hash2keys(value, current_key) else keys << current_key end end keys end |
.keys2yml(translation_keys) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 5 def self.keys2yml(translation_keys) files = translation_keys.group_by { |key, _| key.split('$').first }.transform_values(&:to_h) files.transform_values! { |translations| translations.transform_keys { |key| key.split('$').last } } files.transform_values! do |file| file.each_with_object({}) do |(key, value), hash| hash.deep_merge!(key.split('.').reverse.reduce(value) { |a, n| { n => a } }) end.to_yaml end end |
.locales(github_service) ⇒ Object
15 16 17 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 15 def self.locales(github_service) github_service.locale_files_path.map { |path| path.scan(/\w+(?=\.yml)/).first }.uniq end |
.locales_count(github_service) ⇒ Object
19 20 21 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 19 def self.locales_count(github_service) locales(github_service).count end |
.missing_keys(locale, github_service, branch:) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 55 def self.missing_keys(locale, github_service, branch:) generic_keys = all_keys(github_service, branch:) locale_yml = pull_one_locale(locale, github_service, branch:) locale_keys = locale_yml.flat_map do |path, file| hash2keys(file).map { |key| "#{path}$#{key}" } end.uniq generic_keys.map { |key| format(key, locale:) } - locale_keys end |
.pull_one_locale(locale, github_service, branch: 'translations') ⇒ Object
64 65 66 67 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 64 def self.pull_one_locale(locale, github_service, branch: 'translations') locale_files_path = github_service.locale_files_path(branch:).filter { |path| path.ends_with?("#{locale}.yml") } github_service.pull(locale_files_path, branch:).transform_values { |file| YAML.load(file) } end |
.traverse(hash, parent_key = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/helpers/locale_ninja/locale_helper.rb', line 69 def self.traverse(hash, parent_key = nil) path = [] hash.each do |key, value| current_key = parent_key ? "#{parent_key}.#{key}" : key.to_s if value.is_a?(Hash) path += traverse(value, current_key) else path << [current_key, value] end end path end |