开发者

Synchronize locale yml files tool in Rails

I was wondering, is it possible to synchronize with any tool or gem or w/e 2 or more yml files?

eg. i have the Greek yml file called el.yml

el:
  layout:
    home: "Αρχική"

and the english one c开发者_Go百科alled en.yml

en:
  layout:
    home: "Home"
    category: "Category"

Is there any tool that based on a single yml file ie en.yml ( root ) that goes to the rest of the ymls and creates the missing translations with the default (en.yml) values?

After running such a tool i would expect to have the el.yml become likes this:

el:
  layout:
    home: "Αρχική"
    category: "Category"

I am using a similar tool in .NET RESX Synchronizer and it does exactly that, but for resx files.


I just wrote my own rake task for this:

LOCALES_PATH  = "#{Rails.root}/config/locales/*.yml"
MASTER_LOCALE = "#{Rails.root}/config/locales/en.yml"

class Hash
  def to_yaml( opts = {} )
    YAML::quick_emit( object_id, opts ) do |out|
      out.map( taguri, to_yaml_style ) do |map|
        sort.each do |k, v| # "sort" added
          map.add( k, v )
        end
      end
    end
  end
end

namespace :locales do
  task :merge do
    require 'yaml'
    master = YAML::load_file MASTER_LOCALE
    master_language_code = File.basename(MASTER_LOCALE, '.yml')
    Dir[LOCALES_PATH].each do |file_name|
      if file_name == MASTER_LOCALE
        puts "=> skipping master locale #{File.basename(MASTER_LOCALE)}"
        next
      end
      language_code = File.basename(file_name, '.yml')
      slave = YAML::load_file(file_name)
      unless slave[language_code]
        puts "-> ERROR on #{File.basename(file_name)}: can't find key '#{language_code}'!"
        next
      end
      merged = master[master_language_code].deep_merge(slave[language_code])
      final = { language_code => merged } # remove other keys
      File.open(file_name, 'w') do |file|
        file.write final.to_yaml.gsub(/\s+$/, '')
      end
      puts "+ merged #{File.basename(file_name)} with master"
    end
  end
end

It does a simple merge(). In your case it takes the en.yml and merges the el.yml into it and saves it as el.yml. The en.yml does not get touched.


It's not exactly what you need, but maybe checking out http://www.github.com/mynewsdesk/translate out can prove useful.

Quote:

This plugin provides a web interface for translating Rails I18n texts (requires Rails 2.2 or higher) from one locale to another. The plugin has been tested only with the simple I18n backend that ships with Rails. I18n texts are read from and written to YAML files under config/locales.


Didn't found either, so I wrote a lil gem: http://github.com/nofxx/i18n_sync


You can try locale_assistant gem. Pretty handy if you have more files in more directories.

https://github.com/jsaak/locale_assistant

(I am the creator of this gem)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜