Created
July 28, 2012 21:23
-
-
Save camertron/3194858 to your computer and use it in GitHub Desktop.
Creating custom units.yml from Twitter Translation Center
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
require 'rubygems' | |
require 'json' | |
require 'ya2yaml' | |
require 'twitter_cldr' | |
require 'fileutils' | |
ENDPOINT = "http://translate.twttr.com/api/2/twitter/phrase/{{phrase_id}}/translations.json" | |
merged = { :day => 19636, :hour => 19638, :second => 19639, :minute => 19634 }.inject({}) do |final_ret, (label, phrase_id)| | |
cur_data = JSON.parse(`curl #{ENDPOINT.gsub("{{phrase_id}}", phrase_id.to_s)} --silent`).inject({}) do |ret, (locale, trans)| | |
rules = TwitterCldr::Formatters::Plurals::Rules.all_for(locale.to_sym) | |
ret[locale] = rules.inject({}) do |locale_ret, rule| | |
locale_ret[label.to_s] ||= {} | |
locale_ret[label.to_s]["abbreviated"] ||= {} | |
locale_ret[label.to_s]["abbreviated"][rule.to_s] = trans.gsub("%{number}", "{0}") | |
locale_ret | |
end | |
ret | |
end | |
if final_ret.size == 0 | |
final_ret = cur_data | |
else | |
cur_data.each do |locale, hash| | |
final_ret[locale].merge!(hash) | |
end | |
end | |
final_ret | |
end | |
merged.each_pair do |locale_code, hash| | |
FileUtils.mkdir_p("/tmp/cldr/custom/locales/#{TwitterCldr.convert_locale(locale_code)}") | |
File.open("/tmp/cldr/custom/locales/#{TwitterCldr.convert_locale(locale_code)}/units.yml", "w:UTF-8") do |f| | |
f.write({ TwitterCldr.convert_locale(locale_code).to_s => { "units" => hash } }.ya2yaml) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment