Class: Rubi18n::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/rubi18n/locale.rb

Overview

Information about locale (language, country and other special variant preferences). Locale was named by RFC 3066. For example locale for French speaking people in Canada will be fr_CA.

Locale files is placed in rubi18n/locales/ dir in YAML files.

Usage

Set system locale to Russian

Rubi18n::Locale.current = "ru"

Get locale for USA English and print it title

locale = Rubi18n::Locale.new("en_US")
puts locale["title"]

Set locale to user system locale (in UNIX-like OS) and print it title

Rubi18n::Locale.current = ENV["LANG"]
puts "Your locale is #{Rubi18n::Locale.current['code']}"

Formatting

You can print number and float according to the rules of the locale by to_ls method. See Rubi18n::Formatters.

For translate month and week day names in Time and Date see Translation.strftime method.

Constant Summary collapse

LOCALES_DIR =
Pathname(__FILE__).dirname.expand_path + "../../locales/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Locale

Load locale by in code (RFC 3066)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rubi18n/locale.rb', line 90

def initialize(code)
  code.delete!("/", "\\")
  
  @locale = {}
  while code
    file = File.join(LOCALES_DIR, "#{code}.yml")
    raise "Locale #{code} isn't exists" if not File.exists? file
    loaded = YAML.load_file(file)
    @locale = loaded.merge @locale
    code = loaded["include"]
  end
  
  eval("def pluralize(n); #{@locale["pluralization"]}; end", binding)
end

Class Method Details

.currentObject

Return system locale



62
63
64
# File 'lib/rubi18n/locale.rb', line 62

def self.current
  @@current
end

.current=(locale) ⇒ Object

Set system locale to use in to_ls methods in another classes



54
55
56
57
58
59
# File 'lib/rubi18n/locale.rb', line 54

def self.current=(locale)
  if String == locale.class
    locale = Locale.find(locale)
  end
  @@current = locale
end

.exists?(locale) ⇒ Boolean

Is locale has info file

Returns:

  • (Boolean)


74
75
76
# File 'lib/rubi18n/locale.rb', line 74

def self.exists?(locale)
  File.exists?(File.join(LOCALES_DIR, locale + ".yml"))
end

.find(locales) ⇒ Object

Find and load first available locale from locales



79
80
81
82
83
84
85
86
87
# File 'lib/rubi18n/locale.rb', line 79

def self.find(locales)
  locales = locales.to_a if String == locales.class
  
  locales.each_with_index do |locale, i|
    locale = locale[0..4] if locale.length > 5
    locales.insert(i + 1, locale[0..1]) if locale.length > 2
    return self.new(locale) if self.exists? locale
  end
end

.localesObject

All available locales



67
68
69
70
71
# File 'lib/rubi18n/locale.rb', line 67

def self.locales
  Dir.glob(File.join(LOCALES_DIR, "*.yml")).map do |i|
    File.basename(i, ".yml")
  end
end

Instance Method Details

#==(locale) ⇒ Object

Is another locale has same language code



111
112
113
# File 'lib/rubi18n/locale.rb', line 111

def ==(locale)
  @locale['code'] == locale['code']
end

#[](name) ⇒ Object

Get information about locale



106
107
108
# File 'lib/rubi18n/locale.rb', line 106

def [](name)
  @locale[name]
end

#inspectObject

Human readable language code and title



116
117
118
# File 'lib/rubi18n/locale.rb', line 116

def inspect
  "Locale #{@locale['code']} (#{@locale['title']})"
end

#pluralize(n) ⇒ Object

Return pluralization type for this locale for n items. Will be replacing code from locale info file.



122
# File 'lib/rubi18n/locale.rb', line 122

def pluralize(n); end