Created
October 25, 2021 17:35
-
-
Save westonganger/3a51a753288c8f76d132af8341061251 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### USE YAML BACKEND FOR THIS SCRIPT | |
I18n.backend = I18n::Backend::Simple.new | |
namespace :translations do | |
task yaml_export: :environment do | |
base_path = Rails.root.join("config/locales/export/") | |
FileUtils.mkdir_p(base_path) | |
FileUtils.rm_r(File.join(base_path, "*"), force: true) | |
I18n.available_locales.each do |locale| | |
TranslationKey::APP_NAMES.each do || | |
f = File.join(base_path, "#{app_name}.#{locale}.yml") | |
File.open(f, "wb") do |io| | |
str = TranslationKey.get_nested_tree(app_name: app_name)[locale.to_s].to_yaml.sub("---\n", "") | |
io.write(str) | |
end | |
end | |
end | |
end | |
task yaml_import_missing: :environment do | |
base_path = Rails.root.join("config/locales/import/") | |
FileUtils.mkdir_p(base_path) | |
I18n.load_path = [] ### Erase I18n load path completely | |
I18n.load_path << Dir[File.join(base_path, "*.yml")] | |
app_name = ENV['APP_NAME'] | |
if app_name.blank? | |
raise "Error: app_name argument not provided: rake yaml_import APP_NAME=app_name" | |
end | |
all_keys = TranslationsScriptHelper.fetch_default_locale_keys | |
key_records_by_key = TranslationKey.includes(:translation_values).index_by(&:key) | |
all_keys.each do |key| | |
if key_records_by_key[key].nil? | |
key_records_by_key[key] = TranslationKey.create!(key: key, app_name: app_name) | |
end | |
end | |
flat_translations_hash = TranslationsScriptHelper.get_flat_translations_hash | |
translation_values_to_import = [] | |
key_records_by_key.each do |key, key_record| | |
I18n.available_locales.each do |locale| | |
val = flat_translations_hash[locale.to_s][key] | |
if val.present? | |
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s } | |
if val_record.nil? | |
translation_values_to_import << key_record.translation_values.new(locale: locale, translation: val) | |
end | |
end | |
end | |
end | |
TranslationValue.import(translation_values_to_import, validate: true) | |
put "Import Succesful" | |
end | |
task yaml_import_fresh: :environment do | |
base_path = Rails.root.join("config/locales/import/") | |
FileUtils.mkdir_p(base_path) | |
I18n.load_path = [] ### Erase I18n load path completely | |
I18n.load_path << Dir[File.join(base_path, "*.yml")] | |
app_name = ENV['APP_NAME'] | |
if app_name.blank? | |
raise "Error: app_name argument not provided: rake yaml_import APP_NAME=app_name### Download yml translations files for import or external usage - https://fisheriesapp.vericatch.com/admin/translations | |
### USE YAML BACKEND FOR THIS SCRIPT | |
I18n.backend = I18n::Backend::Simple.new | |
namespace :translations do | |
task yaml_export: :environment do | |
base_path = Rails.root.join("config/locales/export/") | |
FileUtils.mkdir_p(base_path) | |
FileUtils.rm_r(File.join(base_path, "*"), force: true) | |
I18n.available_locales.each do |locale| | |
TranslationKey::APP_NAMES.each do || | |
f = File.join(base_path, "#{app_name}.#{locale}.yml") | |
File.open(f, "wb") do |io| | |
str = TranslationKey.get_nested_tree(app_name: app_name)[locale.to_s].to_yaml.sub("---\n", "") | |
io.write(str) | |
end | |
end | |
end | |
end | |
task yaml_import_missing: :environment do | |
base_path = Rails.root.join("config/locales/import/") | |
FileUtils.mkdir_p(base_path) | |
I18n.load_path = [] ### Erase I18n load path completely | |
I18n.load_path << Dir[File.join(base_path, "*.yml")]### Download yml translations files for import or external usage - https://fisheriesapp.vericatch.com/admin/translations | |
### USE YAML BACKEND FOR THIS SCRIPT | |
I18n.backend = I18n::Backend::Simple.new | |
namespace :translations do | |
task yaml_export: :environment do | |
base_path = Rails.root.join("config/locales/export/") | |
FileUtils.mkdir_p(base_path) | |
FileUtils.rm_r(File.join(base_path, "*"), force: true) | |
I18n.available_locales.each do |locale| | |
TranslationKey::APP_NAMES.each do || | |
f = File.join(base_path, "#{app_name}.#{locale}.yml") | |
File.open(f, "wb") do |io| | |
str = TranslationKey.get_nested_tree(app_name: app_name)[locale.to_s].to_yaml.sub("---\n", "") | |
io.write(str) | |
end | |
end | |
end | |
end | |
task yaml_import_missing: :environment do | |
base_path = Rails.root.join("config/locales/import/") | |
FileUtils.mkdir_p(base_path) | |
I18n.load_path = [] ### Erase I18n load path completely | |
I18n.load_path << Dir[File.join(base_path, "*.yml")] | |
app_name = ENV['APP_NAME'] | |
if app_name.blank? | |
raise "Error: app_name argument not provided: rake yaml_import APP_NAME=app_name" | |
end | |
all_keys = TranslationsScriptHelper.fetch_default_locale_keys | |
key_records_by_key = TranslationKey.includes(:translation_values).index_by(&:key) | |
all_keys.each do |key| | |
if key_records_by_key[key].nil? | |
key_records_by_key[key] = TranslationKey.create!(key: key, app_name: app_name) | |
end | |
end | |
flat_translations_hash = TranslationsScriptHelper.get_flat_translations_hash | |
translation_values_to_import = [] | |
key_records_by_key.each do |key, key_record| | |
I18n.available_locales.each do |locale| | |
val = flat_translations_hash[locale.to_s][key] | |
if val.present? | |
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s } | |
if val_record.nil? | |
translation_values_to_import << key_record.translation_values.new(locale: locale, translation: val) | |
end | |
end | |
end | |
end | |
TranslationValue.import(translation_values_to_import, validate: true) | |
put "Import Succesful" | |
end | |
task yaml_import_fresh: :environment do | |
base_path = Rails.root.join("config/locales/import/") | |
FileUtils.mkdir_p(base_path) | |
I18n.load_path = [] ### Erase I18n load path completely | |
I18n.load_path << Dir[File.join(base_path, "*.yml")] | |
app_name = ENV['APP_NAME'] | |
if app_name.blank? | |
raise "Error: app_name argument not provided: rake yaml_import APP_NAME=app_name" | |
end | |
puts "Are you sure you want to destroy all existing translations for app_name: #{app_name} ? (Y/n)" | |
input = STDIN.gets.chomp | |
if ["yes", "y"].exclude?(input.to_s.downcase) | |
raise "Canceled!" | |
end | |
TranslationKey.where(app_name: app_name).includes(:translation_values).destroy_all | |
all_keys = TranslationsScriptHelper.fetch_default_locale_keys | |
key_records_by_key = TranslationKey.includes(:translation_values).index_by(&:key) | |
all_keys.each do |key| | |
if key_records_by_key[key].nil? | |
key_records_by_key[key] = TranslationKey.create!(key: key, app_name: app_name) | |
end | |
end | |
flat_translations_hash = TranslationsScriptHelper.get_flat_translations_hash | |
translation_values_to_import = [] | |
key_records_by_key.each do |key, key_record| | |
I18n.available_locales.each do |locale| | |
val = flat_translations_hash[locale.to_s][key] | |
if val.present? | |
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s } | |
if val_record.nil? | |
translation_values_to_import << key_record.translation_values.new(locale: locale, translation: val) | |
elsif val_record.translation != val | |
val_record.update!(translation: val) | |
end | |
end | |
end | |
end | |
TranslationValue.import(translation_values_to_import, validate: true) | |
put "Import Succesful" | |
end | |
end | |
class TranslationsScriptHelper | |
def self.get_flat_translations_hash | |
### I18n method already handles converting "nested keys" to "string key with dot notation" | |
@@flat_tranlations_hash ||= I18n.backend.send(:translations) | |
end | |
def self.fetch_default_locale_keys | |
all_keys_dot_notation = [] | |
recursive_fetch_keys = ->(key:, val:, prev_key_str: nil){ | |
dot_notation_key = prev_key_str ? [prev_key_str, key].compact.join(".") : key | |
if val.is_a?(Hash) | |
val.each do |inner_key, inner_val| | |
recursive_fetch_keys.call(key: inner_key, val: inner_val, prev_key_str: dot_notation_key) | |
end | |
else | |
all_keys_dot_notation << dot_notation_key | |
end | |
} | |
default_locale_translations = self.get_flat_translations_hash[I18n.default_locale] | |
default_locale_translations.each do |k,v| | |
recursive_fetch_keys.call(key: k, val: v) | |
end | |
return all_keys_dot_notation | |
end | |
end | |
app_name = ENV['APP_NAME'] | |
if app_name.blank? | |
raise "Error: app_name argument not provided: rake yaml_import APP_NAME=app_name" | |
end | |
all_keys = TranslationsScriptHelper.fetch_default_locale_keys | |
key_records_by_key = TranslationKey.includes(:translation_values).index_by(&:key) | |
all_keys.each do |key| | |
if key_records_by_key[key].nil? | |
key_records_by_key[key] = TranslationKey.create!(key: key, app_name: app_name) | |
end | |
end | |
flat_translations_hash = TranslationsScriptHelper.get_flat_translations_hash | |
translation_values_to_import = [] | |
key_records_by_key.each do |key, key_record| | |
I18n.available_locales.each do |locale| | |
val = flat_translations_hash[locale.to_s][key] | |
if val.present? | |
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s } | |
if val_record.nil? | |
translation_values_to_import << key_record.translation_values.new(locale: locale, translation: val) | |
end | |
end | |
end | |
end | |
TranslationValue.import(translation_values_to_import, validate: true) | |
put "Import Succesful" | |
end | |
task yaml_import_fresh: :environment do | |
base_path = Rails.root.join("config/locales/import/") | |
FileUtils.mkdir_p(base_path) | |
I18n.load_path = [] ### Erase I18n load path completely | |
I18n.load_path << Dir[File.join(base_path, "*.yml")] | |
app_name = ENV['APP_NAME'] | |
if app_name.blank? | |
raise "Error: app_name argument not provided: rake yaml_import APP_NAME=app_name" | |
end | |
puts "Are you sure you want to destroy all existing translations for app_name: #{app_name} ? (Y/n)" | |
input = STDIN.gets.chomp | |
if ["yes", "y"].exclude?(input.to_s.downcase) | |
raise "Canceled!" | |
end | |
TranslationKey.where(app_name: app_name).includes(:translation_values).destroy_all | |
all_keys = TranslationsScriptHelper.fetch_default_locale_keys | |
key_records_by_key = TranslationKey.includes(:translation_values).index_by(&:key) | |
all_keys.each do |key| | |
if key_records_by_key[key].nil? | |
key_records_by_key[key] = TranslationKey.create!(key: key, app_name: app_name) | |
end | |
end | |
flat_translations_hash = TranslationsScriptHelper.get_flat_translations_hash | |
translation_values_to_import = [] | |
key_records_by_key.each do |key, key_record| | |
I18n.available_locales.each do |locale| | |
val = flat_translations_hash[locale.to_s][key] | |
if val.present? | |
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s } | |
if val_record.nil? | |
translation_values_to_import << key_record.translation_values.new(locale: locale, translation: val) | |
elsif val_record.translation != val | |
val_record.update!(translation: val) | |
end | |
end | |
end | |
end | |
TranslationValue.import(translation_values_to_import, validate: true) | |
put "Import Succesful" | |
end | |
end | |
class TranslationsScriptHelper | |
def self.get_flat_translations_hash | |
### I18n method already handles converting "nested keys" to "string key with dot notation" | |
@@flat_tranlations_hash ||= I18n.backend.send(:translations) | |
end | |
def self.fetch_default_locale_keys | |
all_keys_dot_notation = [] | |
recursive_fetch_keys = ->(key:, val:, prev_key_str: nil){ | |
dot_notation_key = prev_key_str ? [prev_key_str, key].compact.join(".") : key | |
if val.is_a?(Hash) | |
val.each do |inner_key, inner_val| | |
recursive_fetch_keys.call(key: inner_key, val: inner_val, prev_key_str: dot_notation_key) | |
end | |
else | |
all_keys_dot_notation << dot_notation_key | |
end | |
} | |
default_locale_translations = self.get_flat_translations_hash[I18n.default_locale] | |
default_locale_translations.each do |k,v| | |
recursive_fetch_keys.call(key: k, val: v) | |
end | |
return all_keys_dot_notation | |
end | |
end | |
" | |
end | |
puts "Are you sure you want to destroy all existing translations for app_name: #{app_name} ? (Y/n)" | |
input = STDIN.gets.chomp | |
if ["yes", "y"].exclude?(input.to_s.downcase) | |
raise "Canceled!" | |
end | |
TranslationKey.where(app_name: app_name).includes(:translation_values).destroy_all | |
all_keys = TranslationsScriptHelper.fetch_default_locale_keys | |
key_records_by_key = TranslationKey.includes(:translation_values).index_by(&:key) | |
all_keys.each do |key| | |
if key_records_by_key[key].nil? | |
key_records_by_key[key] = TranslationKey.create!(key: key, app_name: app_name) | |
end | |
end | |
flat_translations_hash = TranslationsScriptHelper.get_flat_translations_hash | |
translation_values_to_import = [] | |
key_records_by_key.each do |key, key_record| | |
I18n.available_locales.each do |locale| | |
val = flat_translations_hash[locale.to_s][key] | |
if val.present? | |
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s } | |
if val_record.nil? | |
translation_values_to_import << key_record.translation_values.new(locale: locale, translation: val) | |
elsif val_record.translation != val | |
val_record.update!(translation: val) | |
end | |
end | |
end | |
end | |
TranslationValue.import(translation_values_to_import, validate: true) | |
put "Import Succesful" | |
end | |
end | |
class TranslationsScriptHelper | |
def self.get_flat_translations_hash | |
### I18n method already handles converting "nested keys" to "string key with dot notation" | |
@@flat_tranlations_hash ||= I18n.backend.send(:translations) | |
end | |
def self.fetch_default_locale_keys | |
all_keys_dot_notation = [] | |
recursive_fetch_keys = ->(key:, val:, prev_key_str: nil){ | |
dot_notation_key = prev_key_str ? [prev_key_str, key].compact.join(".") : key | |
if val.is_a?(Hash) | |
val.each do |inner_key, inner_val| | |
recursive_fetch_keys.call(key: inner_key, val: inner_val, prev_key_str: dot_notation_key) | |
end | |
else | |
all_keys_dot_notation << dot_notation_key | |
end | |
} | |
default_locale_translations = self.get_flat_translations_hash[I18n.default_locale] | |
default_locale_translations.each do |k,v| | |
recursive_fetch_keys.call(key: k, val: v) | |
end | |
return all_keys_dot_notation | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment