#!/usr/bin/env ruby #require 'pathname' #require 'active_support/all' #require 'yaml' require 'pry' #$root = Pathname.new File.expand_path('../../', __FILE__) class UserApp def write_output(locale) out_file = File.open "#{ENV['MOZO_PATH_USER']}/app/i18n/#{locale}.coffee", 'w+' out_file.puts "window.$translations ||= {}" out_file.puts "$translations.#{locale} =" Proc.new do |output| out_file.puts output end end def path Pathname.new File.expand_path('../../../mozo-user', __FILE__) end def user_translations Dir.glob(Rails.root.join('config/user_locales/**/*.yml')).each_with_object({}) do |user_yaml, obj| obj.deep_merge! YAML.load_file(user_yaml) end end def all_translations user_translations.deep_merge general_translations end def general_translations Rails.application.config.i18n.available_locales.each_with_object({}) do |locale, obj| obj[locale.to_s] = { models: I18n.t('activemodel.models', locale: locale), attributes: I18n.t('activemodel.attributes', locale: locale), helpers: I18n.t('helpers', locale: locale), pagination: I18n.t('views.pagination', locale: locale), errors: I18n.t('errors', locale: locale), date: {day_name: Hash[Date::DAYNAMES.map(&:downcase).zip(I18n.t('date.day_names', locale: locale))]} } end.deep_stringify_keys end def write_translations translations = all_translations # caching File.write "translations.yml", translations.to_yaml HashToCS.convert(translations['en'], 2, write_output('en')) HashToCS.convert(translations['nl'], 2, write_output('nl')) return File.open path.join('vendor/i18n/translations.js'), 'w+' do |f| f.puts "$translations = #{JSON.pretty_generate translations}" end end end # Franc Paul # # convert a ruby hash to coffescript object ## module HashToCS =begin usage: proc = Proc.new do |output| coffee_script_file.puts output end HashToCs.convert(ruby_hash, 2, proc) =end defaultProc = Proc.new do |output| print output end #input is a Ruby hash #spaces is an integer count of spaces to be used as a prefix string for whitespace significance #proc acts on the output def HashToCS.convert(input, spaces=0, proc=defaultProc) spaces = " " * spaces case input when String q = if input =~ /\n/ '"""' else '"' end proc.call spaces + q + input + q + "\n" when Array proc.call spaces + "[\n" input.each do |a| convert(a, spaces.size + 2, proc) end proc.call spaces + "]\n" when Hash #proc.call spaces + "{\n" input.each do |k, v| key_string = k =~ /\W/ ? "\"#{k}\"" : "#{k}" if v.is_a?(String) if v =~ /\n/ proc.call spaces + "#{key_string}: \"\"\"\n#{v}\"\"\"\n" else proc.call spaces + "#{key_string}: \"#{v}\"\n" end else proc.call spaces + "#{key_string}:\n" convert(v, spaces.size + 2, proc) end end #proc.call spaces + "}\n" else proc.call spaces + input.to_s + "\n" end end end UserApp.new.write_translations