general gettext definition functions. order in wich GettextLocalize tries to load options
- 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
- 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 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
returns the abbreviated monthnames in current locale starting with a nil value, then january
# 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
# 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
# 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
# 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
# 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
# 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
# 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 an Array ready to be merged in datetime select options :order => [:day, :month, :year]
# 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
sets the default locale to use can be overriden in the controller using set_locale if not defined uses $LANG environment variable
# 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
# 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
# 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
# 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
tries to check if the locale is translated, by checking the load_paths FIXME: check if Gettext has this
# 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 monthnames in current locale starting with a nil value, then january
# 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
# 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 plugin textdomain and forces the creation of a RAILS_ROOT/vendor/plugins/$plugin/po/$textdomain.pot when executing rake gettext:plugins:updatepo
# 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 country options specified in the countries.yml file.
# 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
# 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 default application locale paths being RAILS_ROOT/locale and RAILS_ROOT/vendor/plugins/$plugin/locale in case the plugin has a locale dir
# 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
# 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
# File vendor/plugins/gettext_localize/lib/gettext_localize.rb, line 59
59: def self.textdomain
60: self.get_textdomain
61: end