Module GettextLocalize
In: vendor/plugins/gettext_localize/lib/gettext_localize.rb
vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb
vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb

general gettext definition functions. order in wich GettextLocalize tries to load options

locale:

  - cookie set with name 'lang'
  - locale set in controller with set_locale(lang)
  - locale set in environment with GettextLocalize::default_locale = lang
  - locale set in environment variable $LANG
  - locale set in this plugin init.rb with GettextLocalize::fallback_locale = lang

country

  - cookie set with name 'country'
  - country set in controller with set_country(country)
  - country set in environment with GettextLocalize::default_country = country
  - country part of locale
  - country set in this plugins init.rb with GettextLocalize::fallback_country = country

textdomain:

  - textdomain set in plugin using plugin_bindtextdomain(textdomain)
    uses loadpath RAILS_ROOT/vendor/plugins/$plugin/locale
  - textdomain set in controller with init_gettext(textdomain)
    uses loadpath RAILS_ROOT/locale
  - textdomain set in environment.rb with GettextLocalize::default_textdomain = textdomain
  - app_name set in environment.rb with GettextLocalize::app_name = app
  - name of the directory the rails app is in

Methods

Classes and Modules

Module GettextLocalize::Controller
Module GettextLocalize::Helper
Module GettextLocalize::TimeMethods

Public Class methods

returns the abbreviated daynames in current locale starting mith sunday!

[Source]

   # File vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb, line 5
5:   def self.abbr_daynames
6:      [_("Sun"),_("Mon"),_("Tue"),_("Wed"),_("Thu"),_("Fri"),_("Sat")]
7:   end

returns the abbreviated monthnames in current locale starting with a nil value, then january

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb, line 17
17:   def self.abbr_monthnames
18:     [nil,_("Jan"),_("Feb"),_("Mar"),_("Apr"),s_("Abbr|May"),_("Jun"),
19:      _("Jul"),_("Aug"),_("Sep"),_("Oct"),_("Nov"),_("Dec")]
20:   end

adds a default locale path

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 225
225:   def self.add_default_locale_path(dir=nil)
226:     version = GetText::VERSION.to_s.split(".").map{|v| v.to_i }
227:     # if gettext 1.9.0 or greater
228:     if version[0] >= 1 and version[1] >= 9
229:       path = self.get_locale_path_with_vars(dir)
230:     else
231:       path = self.get_locale_path(dir)
232:     end
233:     GetText::add_default_locale_path(path) if path
234:   end

returns the current application name

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 96
96:   def self.app_name
97:     self.get_app_name
98:   end

defines application name if not defined uses rails app directory

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 91
91:   def self.app_name=(name)
92:     self.set_app_name(name)
93:   end

returns app_name with version concadenated in a string

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 114
114:   def self.app_name_version
115:     a = []
116:     a << self.get_app_name if self.get_app_name
117:     a << self.get_app_version if self.get_app_version
118:     a.join(" ")
119:   end

returns the current application version

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 108
108:   def self.app_version
109:     self.get_app_version
110:   end

sets application version needed to generate the po files by default set to 1.0.0

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 103
103:   def self.app_version=(version)
104:     self.set_app_version(version)
105:   end

returns the country to select if not set tries to read from $LANG environment variable else tries to read fallback_country

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 134
134:   def self.country
135:     self.get_country
136:   end

returns the options country hash which contains currency, date order for selects, etc.

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 140
140:   def self.country_options
141:     self.get_country_options
142:   end

Returns an Array ready to be merged in datetime select options :order => [:day, :month, :year]

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 146
146:   def self.date_order
147:     country_options[:date_select_order][:order].collect! { |element| element.to_sym }
148:     country_options[:date_select_order]
149:   end

returns the daynames in current locale starting mith sunday!

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb, line 11
11:   def self.daynames
12:     [_("Sunday"),_("Monday"),_("Tuesday"),_("Wednesday"),_("Thursday"),_("Friday"),_("Saturday")]
13:   end

sets the country to select by default

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 122
122:   def self.default_country=(country)
123:     self.set_default_country(country)
124:   end

sets the default locale to use can be overriden in the controller using set_locale if not defined uses $LANG environment variable

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 66
66:   def self.default_locale=(lc)
67:     self.set_default_locale(lc)
68:   end

defines the default methods to find a locale in every controller can be overriden using set_locale_by as a before_filter possible methods are: :header, :cookie, :session, :param read GettextLocalize::Controller for more info

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 170
170:   def self.default_methods=(ms)
171:     self.set_default_methods(ms)
172:   end

defines default textdomain if not defined uses application name

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 85
85:   def self.default_textdomain=(td)
86:     self.set_default_textdomain(td)
87:   end

iterates over every application plugin that has gettext localization yielding the variables version, name and plugin directory

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 277
277:   def self.each_plugin
278:     Dir.glob(File.join(self.get_plugins_base_dir,"*")).each do |file|
279:       begin
280:         po_dir = Pathname.new(File.join(file,"po")).realpath.to_s
281:       rescue
282:         next
283:       end
284:       if File.directory?(po_dir) and File.writable?(po_dir)
285:         if defined?(@@plugins) and @@plugins[file]
286:           version = @@plugins[file][:version]
287:           name = @@plugins[file][:name]
288:         else
289:           version = "1.0.0",
290:           name = File.basename(file)
291:         end
292:         yield version, name, file
293:       end
294:     end
295:   end

sets the country to select if everything else fails

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 127
127:   def self.fallback_country=(country)
128:     self.set_fallback_country(country)
129:   end

defines fallback locale in case nothing else works, please do not call this function

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 79
79:   def self.fallback_locale=(lc)
80:     self.set_fallback_locale(lc)
81:   end

returns the formats

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 187
187:   def self.formats
188:     @@formats
189:   end

returns the country options updated

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 260
260:   def self.get_country_options
261:     self.set_country_options
262:     @@country_options
263:   end

tries to check if the locale is translated, by checking the load_paths FIXME: check if Gettext has this

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 194
194:   def self.has_locale?(locale)
195:     locale = self.format_locale(locale)
196:     return true if locale=='C' or locale.split("_")[0] == 'en' # default locale to translate
197:     mos = [File.join(self.get_locale_path,locale,"LC_MESSAGES",self.get_textdomain+".mo")]
198:     mos << File.join(self.get_locale_path,locale.split("_")[0],"LC_MESSAGES",self.get_textdomain+".mo")
199:     self.each_plugin do |version,textdomain,path|
200:       path = self.get_locale_path(path)
201:       mos << File.join(path,locale,"LC_MESSAGES",textdomain+".mo")
202:       mos << File.join(path,locale.split("_")[0],"LC_MESSAGES",textdomain+".mo")
203:     end
204:     mos.each do |mo|
205:       return true if File.file?(mo) and File.readable?(mo)
206:     end
207:     return false
208:   end

returns the current locale checking all the priorities

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 53
53:   def self.locale
54:     self.get_locale
55:   end

returns the methods assigned to obtain the locale, in an array by preference

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 176
176:   def self.methods
177:     self.get_methods
178:   end

returns the monthnames in current locale starting with a nil value, then january

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb, line 24
24:   def self.monthnames
25:     [nil,_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),
26:      _("July"),_("August"),_("September"),_("October"),_("November"),_("December")]
27:   end

returns the monthnames in current locale in format "5 of January", localized apart to correctly translate month diferences in other locales use %V to get this in strftime starting with a nil value, then january

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb, line 34
34:   def self.of_monthnames
35:     [nil,_("%s of January"),_("%s of February"),_("%s of March"),_("%s of April"),_("%s of May"),_("%s of June"),
36:      _("%s of July"),_("%s of August"),_("%s of September"),_("%s of October"),_("%s of November"),_("%s of December")]
37:   end

sets the original locale, the one used in literals inside the _() by default is english

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 73
73:   def self.original_locale=(lc)
74:     self.set_original_locale(lc)
75:   end

sets the plugin textdomain and forces the creation of a RAILS_ROOT/vendor/plugins/$plugin/po/$textdomain.pot when executing rake gettext:plugins:updatepo

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 268
268:   def self.plugin_bindtextdomain(name=nil,version="1.0.0")
269:     name = self.get_plugin_dir_name if name.nil?
270:     self.add_plugin(name,version)
271:     GetText::bindtextdomain(name, :path => self.get_plugin_locale_path)
272:   end

sets the country

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 152
152:   def self.set_country(country)
153:     @@country = self.format_country(country)
154:   end

sets country options specified in the countries.yml file.

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 237
237:   def self.set_country_options(country=nil)
238:     begin
239:       country = self.get_country if country.nil?
240:       if country != @@country_options_country or !@@country_options
241:         countries_yml_file = Pathname.new(File.join(self.get_plugin_dir(),"countries.yml")).realpath.to_s
242:         countries = YAML::load(File.open(countries_yml_file))
243:         countries = self.string_to_sym(countries)
244:         if countries.has_key?(country.to_sym)
245:           @@country_options = countries[country.to_sym]
246:           @@country_options_country = country
247:           return true
248:         elsif @@country_options.nil?
249:           @@country_options = countries.to_a.first.last
250:           @@country_options_country = countries.to_a.first.first
251:         end
252:       end
253:     rescue
254:       ActiveRecord::Base.logger.error("error loading countries config #{countries_yml_file} with country #{country}: #{$!}")
255:     end
256:     false
257:   end

sets a datetime format to overwrite the default settings

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 182
182:   def self.set_format(format_name,format)
183:     @@formats[format_name.to_sym] = format
184:   end

sets the locale if locale is nil unsets

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 158
158:   def self.set_locale(locale)
159:     @@locale = self.format_locale(locale)
160:     GetText::set_locale(@@locale)
161:   end

sets the default application locale paths being RAILS_ROOT/locale and RAILS_ROOT/vendor/plugins/$plugin/locale in case the plugin has a locale dir

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 214
214:   def self.set_locale_paths
215:     require 'gettext'
216:     GetText::add_default_locale_path(self.get_locale_path)
217:     self.each_plugin do |version,name,dir|
218:       if File.directory?(dir) and File.readable?(dir)
219:         GetText::add_default_locale_path(self.get_locale_path(dir))
220:       end
221:     end
222:   end

returns a hash with the supported locales in the aplication as keys and the localized names of the locales as values

[Source]

     # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 300
300:   def self.supported_locales
301:     locale_dir = File.join(RAILS_ROOT,"locale","**")
302:     locales = Dir.glob(locale_dir).select { |file| File.directory? file }.collect { |dir| File.basename(dir) }
303:     locales.collect!{|l| self.format_locale l }
304:     locales.each{|l| locales << l[0..1] if l.length > 2 }
305:     locales << @@original_locale
306:     self.all_locales.delete_if{|k,v| !locales.include? k.to_s }
307:   end

returns the current textdomain checking all the priorities

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 59
59:   def self.textdomain
60:     self.get_textdomain
61:   end

[Source]

    # File vendor/plugins/gettext_localize/lib/gettext_localize_extend.rb, line 39
39:   def self.to_sentence_text_connector
40:     _("and")
41:   end

[Validate]