Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created December 18, 2017 02:22
Show Gist options
  • Save alpaca-tc/fd39abc697cb841fad01e472c587891d to your computer and use it in GitHub Desktop.
Save alpaca-tc/fd39abc697cb841fad01e472c587891d to your computer and use it in GitHub Desktop.
require 'csv'
translations = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
headers = ['key', 'ja', 'en', '...']
CSV.open('i18n.csv', 'r', col_sep: "\t", headers: true) do |csv|
csv.each do |row|
locales = csv.headers - ['key']
locales.each do |locale|
keys = [locale, row['key'].split('.')].flatten.map(&:to_sym)
last_key = keys.pop
translations.dig(*keys)[last_key] = row[locale]
end
end
end
translations.each do |locale, _translations|
File.write("config/locales/#{locale}.yml", _translations.to_yaml)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment