Skip to content

Instantly share code, notes, and snippets.

@bdewater
Last active July 10, 2018 23:13
Show Gist options
  • Save bdewater/d944b3d1a108c57e1d33f7ac83c2a347 to your computer and use it in GitHub Desktop.
Save bdewater/d944b3d1a108c57e1d33f7ac83c2a347 to your computer and use it in GitHub Desktop.
Translate from CLDR
# frozen_string_literal: true
#
# Usage:
# $ LANGUAGE=de ruby currency.rb
require 'open-uri'
require 'nokogiri'
require 'active_support/time_with_zone'
CLDR_URI = "http://unicode.org/repos/cldr/trunk/common/main/#{ENV['LANGUAGE']}.xml"
CURRENCIES = %w(USD AED AFN ALL AMD ANG AOA ARS AUD AWG AZN BAM BBD BDT BGN BIF BMD BND BOB BRL BSD BWP BZD CAD CDF CHF CLP CNY COP CRC CVE CZK DJF DKK DOP DZD EGP ETB EUR FJD FKP GBP GEL GIP GMD GNF GTQ GYD HKD HNL HRK HTG HUF IDR ILS INR ISK JMD JPY KES KGS KHR KMF KRW KYD KZT LAK LBP LKR LRD LSL MAD MDL MGA MKD MMK MNT MOP MRO MUR MVR MWK MXN MYR MZN NAD NGN NIO NOK NPR NZD PAB PEN PGK PHP PKR PLN PYG QAR RON RSD RUB RWF SAR SBD SCR SEK SGD SHP SLL SOS SRD STD SVC SZL THB TJS TOP TRY TTD TWD TZS UAH UGX UYU UZS VND VUV WST XAF XCD XOF XPF YER ZAR ZMW)
def green(string)
puts "\e[32m#{string}\e[0m"
end
def red(string)
puts "\e[31m#{string}\e[0m"
end
parsed_xml = Nokogiri.parse(open(CLDR_URI))
translated_currencies = parsed_xml.css('ldml currencies currency').map.with_object({}) do |currency, hash|
hash[currency["type"]] = currency.css('displayname:not([count])').text
end
puts '=' * 80
CURRENCIES.each do |currency_code|
translation = %(#{currency_code}: "#{translated_currencies[currency_code]} (#{currency_code})")
translated_currencies.key?(currency_code) ? green(translation) : red(translation)
end
puts '=' * 80
# frozen_string_literal: true
#
# Usage:
# $ LANGUAGE=de ruby tz.rb
require 'open-uri'
require 'nokogiri'
require 'active_support/time_with_zone'
CLDR_URI = "http://unicode.org/repos/cldr/trunk/common/main/#{ENV['LANGUAGE']}.xml"
AS_CLDR_MAPPING = {
'America/Indiana/Indianapolis' => 'America/Indianapolis',
'America/Argentina/Buenos_Aires' => 'America/Buenos_Aires',
'Asia/Kathmandu' => 'Asia/Katmandu',
'Asia/Kolkata' => 'Asia/Calcutta',
}
def green(string)
puts "\e[32m#{string}\e[0m"
end
def red(string)
puts "\e[31m#{string}\e[0m"
end
parsed_xml = Nokogiri.parse(open(CLDR_URI))
cldr_timezones = parsed_xml.css('ldml dates timezonenames zone').map.with_object({}) do |tz, hash|
hash[tz["type"]] = tz.css('exemplarcity, long standard').text
end
as_timezones = ActiveSupport::TimeZone.all.map.with_object({}) { |tz, hash| hash[tz.tzinfo.name] = tz.to_s }
translated_timezones = as_timezones.map.with_object({}) do |(tz_id, tz_name), hash|
tz_cldr_id = AS_CLDR_MAPPING[tz_id] || tz_id
gmt_offset = tz_name.match(%r{(\(.*?\))})[0]
hash[tz_id] = [gmt_offset, cldr_timezones[tz_cldr_id]]
end
puts '=' * 80
translated_timezones.each do |tz_id, (gmt_offset, tz_city)|
translation = %(#{tz_id}: "#{gmt_offset} #{tz_city}")
tz_city.blank? ? red(translation) : green(translation)
end
puts '=' * 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment