require 'yaml' require 'fileutils' def process_locale_file(output, file) ## print separator to see where another file begins output.write("\n\n" + '#' * 40 + "\n" + '### ' + file + "\n") output.write( # remove the toplevel "en" key YAML.dump(YAML.load_file(file)['en']).lines.map { |line| " #{line}" # << prefix all lines with two spaces (we're nested below "en:") }[ 1..-1 # << skip the first line (it's "---" and freaks out the parser if it sees it multiple times in a file) ].join ) end namespace :i18n do # # for coding, it helps to have the english strings in separate files. # for translating, it helps to have a single file. This action will combine # the small files into one big one. # # Also, transifex insists that the files be en_US and not just en. # grrr. transifex is configured to automatically update from the # file lib/en_US.yml. # desc "combine config/locales/*.en.yml to lib/en_US.yml" task :bundle do Dir.chdir(Rails.root) do en_yml = 'lib/en_US.yml' File.open(en_yml, 'w') do |output| output.write("en_US:\n\n"+ "### Do NOT edit this file directly, as all changes will be overwritten by `rake i18n:bundle`\n"+ "### Instead, make changes in the appropriate file in config/locales.\n"+ "### Source strings in transifex are automatically updated from this file (via github url)") Dir.chdir('config/locales/en') do Dir.glob('*.en.yml').sort.each do |file| process_locale_file(output, file) end end Dir.chdir('engines') do Dir.glob('*/config/locales/en.yml').sort.each do |file| process_locale_file(output, file) end end puts "You can now find the bundled locale yml in: #{en_yml}" end end end # # I have not been able to get fallback from :pt => :'pt-BR' working. # The rails guide implies that it does not work, even though I18n has # a fallback mechanism. # For now, just use a single language for ever locale. # LOCALE_ALIAS_MAP = { :nb => 'nb-no', :pt => 'pt-br' } desc "pull translations from transifex" task :download do Dir.chdir('config/') do if !File.exists?('transifex.netrc') puts "In order to download translations, you need a config/transifex.netrc file." puts "For example:" puts "machine www.transifex.com login yourusername password yourpassword" exit end APP_CONFIG[:available_locales].each do |lang| next if lang == :en puts puts "downloading #{lang}" lang_url_segment = LOCALE_ALIAS_MAP[lang] || lang url = "https://www.transifex.com/api/2/project/bitmask/resource/leap_web/translation/#{lang_url_segment}/?file" command = %[echo "#{lang}:" > locales/#{lang}.yml && ]+ %[curl -L --netrc-file transifex.netrc -X GET '#{url}' ]+ %[| tail -n +2 >> locales/#{lang}.yml] puts command `#{command}` end end end end