Skip to content

Commit

Permalink
Merge branch 'genki'
Browse files Browse the repository at this point in the history
* genki:
  Updated localization of time and date
  Added some specs and support for localization of times and dates.
  Added a spec for domain specific translation.
  Added more specs to locale detector.
  Added specs for locale detection
  Implemented hierarchical localization.
  Removed core_ext and cleaned up entirely
  Return key if any localizations are not found
  Renamed country guesser to locale detector.
  Updated version to force github gererated gems.
  Fixed bug.
  Don't lookup ymls in Merb.root
  Changed hash to mash
  Added CountryGuesser.
  Updated Rakefile and incremented version number.

Conflicts:
	lib/merb_babel/m_locale.rb
  • Loading branch information
mattetti committed Jan 16, 2009
2 parents 7a0aafe + 366e689 commit ecbf11e
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 163 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
*/.DS_Store
*/.DS_Store
pkg
log
53 changes: 25 additions & 28 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
require 'rubygems'
require 'rake/gempackagetask'
require 'rubygems/specification'
require 'date'
require "spec/rake/spectask"

PLUGIN = "merb_babel"
NAME = "merb_babel"
GEM_VERSION = "0.1.0"
require 'merb-core'
require 'merb-core/tasks/merb'

GEM_NAME = "merb_babel"
GEM_VERSION = "0.1.1.2"
AUTHOR = "Matt Aimonetti"
EMAIL = "mattaimonetti@gmail.com"
HOMEPAGE = "http://github.com/mattetti/merb_babel/"
SUMMARY = "Merb plugin that provides simple localization/internationalisation"

spec = Gem::Specification.new do |s|
s.name = NAME
s.rubyforge_project = 'merb'
s.name = GEM_NAME
s.version = GEM_VERSION
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
s.extra_rdoc_files = ["README.markdown", "LICENSE", 'TODO']
s.summary = SUMMARY
s.description = s.summary
s.author = AUTHOR
s.email = EMAIL
s.homepage = HOMEPAGE
s.add_dependency('merb-core', '~> 1.0.0')
s.add_dependency('merb', '>= 1.0')
s.add_dependency('locale', '>= 0.9.0')
s.require_path = 'lib'
s.autorequire = PLUGIN
s.files = %w(LICENSE README.markdown Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end

desc "install the plugin locally"
task :install => [:package] do
sh %{sudo gem install pkg/#{NAME}-#{GEM_VERSION} --no-update-sources}
desc "install the plugin as a gem"
task :install do
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
end

desc "Uninstall the gem"
task :uninstall do
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
end

desc "create a gemspec file"
task :make_spec do
File.open("#{GEM}.gemspec", "w") do |file|
desc "Create a gemspec file"
task :gemspec do
File.open("#{GEM_NAME}.gemspec", "w") do |file|
file.puts spec.to_ruby
end
end

namespace :jruby do

desc "Run :package and install the resulting .gem with jruby"
task :install => :package do
sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
end

desc "Run specs"
task :spec do
sh "spec --color spec"
end

desc "Run the specs"
Spec::Rake::SpecTask.new("specs") do |t|
t.spec_opts = ["--format", "specdoc", "--colour"]
t.spec_files = Dir["spec/**/*_spec.rb"].sort
end
task :default => :spec
23 changes: 15 additions & 8 deletions lib/merb_babel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__) / "merb_babel" / "core_ext")
#require File.join(File.dirname(__FILE__) / "merb_babel" / "core_ext")

# make sure we're running inside Merb
if defined?(Merb::Plugins)
Expand All @@ -14,29 +14,36 @@
require File.join(File.dirname(__FILE__) / "merb_babel" / "m_locale")
require File.join(File.dirname(__FILE__) / "merb_babel" / "m_l10n")
require File.join(File.dirname(__FILE__) / "merb_babel" / "m_i18n")
require File.join(File.dirname(__FILE__) / "merb_babel" / 'locale_detector')
gem "locale"
require 'locale'

Merb::BootLoader.before_app_loads do
# require code that must be loaded before the application
module Merb
module GlobalHelpers

# Used to translate words using localizations
def babelize(key, *args)
def babelize(*args)
begin
options = args.first
options = args.pop if args.last.kind_of?(Hash)
options ||= {}
options.merge!(:key => key)
options.merge!(:language => language) unless options.has_key?(:language)
options.merge!(:country => country) unless options.has_key?(:country)
MI18n.lookup(options)
options[:language] ||= language
options[:country] ||= country
case key = args.last
when Date, Time
format = MI18n.lookup(options.merge(:keys => args[0..-2]))
ML10n.localize_time(key, format, options)
else
MI18n.lookup(options.merge(:keys => args))
end
rescue
key.to_s
end
end
alias :translate :babelize
alias :t :babelize
alias :_ :babelize

end
end

Expand Down
29 changes: 0 additions & 29 deletions lib/merb_babel/core_ext.rb

This file was deleted.

80 changes: 80 additions & 0 deletions lib/merb_babel/locale_detector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module LocaleDetector
LANGUAGE_TO_COUNTRY = {
"nn"=>["NO"],
"uk"=>["UA"],
"it"=>["IT", "GI", "CH", "LY", "SM"],
"zh_TW"=>["TT", "VN", "HK"],
"no"=>["NO"],
"st"=>["ZA"],
"tk"=>["TM"],
"bn"=>["IN"],
"mn"=>["MN"],
"ja"=>["JP"],
"fr"=>["FR", "CD", "GG", "RW", "CG", "LU", "TT", "CH", "CI", "JE", "SC",
"VN", "BE", "MU", "DJ", "BI", "CA"],
"hi"=>["TT", "AE", "IN"],
"de"=>["DE", "LU", "CH", "LI", "AT", "BE", "RO"],
"ne"=>["NP"], "jw"=>["ID"],
"ta"=>["IN", "SG"],
"tl"=>["PH"],
"pt_PT"=>["PT", "GI"],
"hu"=>["HU", "SK", "RO"],
"sk"=>["SK"],
"fi"=>["FI"],
"sv"=>["SE", "FI"],
"iw"=>["IL"],
"az"=>["AZ"],
"zh"=>["TW"],
"ru"=>["RU", "KZ", "KG", "AZ", "LV", "UA", "TM", "AM", "UZ"],
"ky"=>["KG"],
"es"=>["ES", "HN", "CU", "PR", "TT", "PA", "VE", "DO", "BO", "GI", "PE",
"PY", "NI", "CL", "SV", "CO", "UY", "BZ", "CR", "EC", "GT", "AR", "MX"],
"ko"=>["KR"],
"pt_BR"=>["BR"],
"sw"=>["RW", "KE"],
"ga"=>["IE"],
"id"=>["ID"],
"eu"=>["ES"],
"gl"=>["ES"],
"xh"=>["ZA"],
"uz"=>["TM", "UZ"],
"mr"=>["IN"],
"fa"=>["AE"],
"zu"=>["LS", "ZA"],
"af"=>["NA", "ZA"],
"pl"=>["PL"],
"hy"=>["AM"],
"pa"=>["PK"],
"te"=>["IN"],
"ar"=>["AE", "LY", "SA", "DJ"],
"ms"=>["MY", "SG"],
"el"=>["GR"],
"ro"=>["RO"],
"mt"=>["MT"],
"ur"=>["AE", "PK"],
"da"=>["DK", "GL"],
"ca"=>["ES"],
"tr"=>["TR"],
"zh_CN"=>["SG"],
"nl"=>["NL", "BE", "ID"],
"vi"=>["VN"],
"lt"=>["LT", "LV"],
"th"=>["TH"],
"fo"=>["DK"],
"en"=>["US", "AS", "IE", "NP", "MY", "UK", "FM", "VC", "PR", "TT", "AU",
"PA", "NA", "GG", "LS", "RW", "VG", "AE", "GI", "VI", "AG", "TH", "CH",
"NF", "JE", "AI", "GL", "PH", "CK", "GM", "LY", "IN", "NZ", "NI", "SC",
"PK", "VN", "MS", "TO", "MT", "BE", "PN", "MU", "UG", "SG", "ZA", "BZ",
"SH", "MW", "FJ", "CR", "HK", "JM", "KE", "CA", "ID"],
"lv"=>["LV"]
}.freeze

module_function
def countries_from_language(language)
LANGUAGE_TO_COUNTRY[language]
end

def country_from_language(language)
(countries_from_language(language) || []).first
end
end
26 changes: 18 additions & 8 deletions lib/merb_babel/m_i18n.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
module MI18n

def self.lookup(options)
key = options[:key]
keys = [options[:keys].map{|key| key.to_s}].flatten
language = options[:language]
country = options[:country]

raise ArgumentError, "You need to pass a language reference" unless language
raise ArgumentError, "You need to pass a localization key" unless key
raise ArgumentError, "language: #{language} not found" unless ML10n.localizations[language]
raise ArgumentError, "You need to pass a localization key" if keys.empty?
raise ArgumentError,
"language: #{language} not found" unless ML10n.localizations[language]

full_location = nil
full_location = lookup_with_full_locale(key, language, country) if country
full_location = lookup_with_full_locale(keys, language, country) if country

if full_location
return full_location
else
return ML10n.localizations[language][key]
return lookup_with_language(keys, language) || keys.last
end
end

def self.lookup_with_language(keys, language)
lookup_with_hash(keys, ML10n.localizations[language])
end

def self.lookup_with_full_locale(key, language, country)
def self.lookup_with_full_locale(keys, language, country)
if ML10n.localizations.has_key?(language)
ML10n.localizations[language].has_key?(country) ? ML10n.localizations[language][country][key] : nil
ML10n.localizations[language].has_key?(country) ?
lookup_with_hash(keys, ML10n.localizations[language][country]) : nil
else
nil
end
end

end
def self.lookup_with_hash(keys, l_hash)
keys.inject(l_hash){|h,k| h[k] rescue nil}
end
end
45 changes: 38 additions & 7 deletions lib/merb_babel/m_l10n.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ML10n
LANGUAGE_CODE_KEY = 'mloc_language_code'.freeze
COUNTRY_CODE_KEY = 'mloc_country_code'.freeze

class << self

Expand Down Expand Up @@ -45,7 +47,7 @@ def load_localization!
ML10n.reset_localization_files!
ML10n.find_localization_files.each do |l_file|
begin
l_hash = YAML.load_file(l_file).symbolize_keys
l_hash = YAML.load_file(l_file)#.symbolize_keys
rescue Exception => e
# might raise a real error here in the future
p e.inspect
Expand All @@ -60,7 +62,7 @@ def load_localization!
def find_localization_files
l_files = []
ML10n.localization_dirs.map do |l_dir|
Dir["#{l_dir}/*", '*.{yml,yaml}'].each do |file|
Dir["#{l_dir}/*"].each do |file|
l_files << file unless l_files.include?(file)
end
end
Expand All @@ -85,6 +87,10 @@ def reset_localization_files!
@@localization_files = nil
ML10n.find_localization_files
end

def reset_localizations!
@@localizations = {}
end

def reload_localization_files!
ML10n.reset_localization_files!
Expand All @@ -95,14 +101,39 @@ def reload_localization_files!
def localize(key, *options)
MI18n.localize(options.merge({:locale => locale}))
end

def localize_time(object, format, options)
table_for = proc do |table_name, key, default|
keys = ["DateFormat", table_name]
table = MI18n.lookup(options.merge(:keys => keys)) || {}
table[key] || default
end
format = format.to_s.dup
format.gsub!(/%a/) do
table_for["AbbrDayNames", object.wday, "%a"]
end
format.gsub!(/%A/) do
table_for["AbbrMonthNames", object.mon - 1, "%A"]
end
format.gsub!(/%B/) do
table_for["MonthNames", object.mon - 1, "%B"]
end
format.gsub!(/%p/) do
table_for["AmPm", object.hour < 12 ? 0 : 1, "%p"]
end if object.respond_to?(:hour)
format.gsub!(/%\{([a-zA-Z]\w*)\}/) do
object.send($1) rescue $1
end
object.strftime(format)
end

protected

def load_localization_hash(l_hash)
if l_hash.valid_localization_hash?
language = l_hash[:mloc_language_code]
if l_hash.localization_hash_with_locale?
country = l_hash[:mloc_country_code]
if l_hash.has_key?(LANGUAGE_CODE_KEY)
language = l_hash[LANGUAGE_CODE_KEY]
if l_hash.has_key?(COUNTRY_CODE_KEY)
country = l_hash[COUNTRY_CODE_KEY]
# load localization under the full locale namespace
ML10n.localizations[language] ||= {}
(ML10n.localizations[language][country] ||= {}).merge!(l_hash)
Expand All @@ -115,4 +146,4 @@ def load_localization_hash(l_hash)

end

end
end
Loading

1 comment on commit ecbf11e

@genki
Copy link
Contributor

@genki genki commented on ecbf11e Jan 16, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you ;-)

Please sign in to comment.