base.rb

Path: lib/base.rb
Last Update: Tue Oct 30 19:19:39 +0100 2007

This is the base file of the Simple Localization plugin. It is loaded at application startup and defines the simple_localization method which should be used in the environment.rb file to configure and initialize the localization.

Methods

Public Instance methods

The main method of the SimpleLocalization plugin used to initialize and configure the plugin. Usually it is called in the environment.rb file.

  simple_localization :language => :de, :class_based_field_error_proc => fase

With the :language option you can specify the name of the language file (without extension) you want to use. You can also use the options to specify if a specific feature (the files inside the features directory) should be loaded or not. By default all features will be loaded. To prevent a feature from beeing loaded you can specify an option with the name of the feature and a value of false.

In the example above this prevents the class_based_field_error_proc feature (the class_based_field_error_proc.rb file in the features directory) from beeing loaded.

Alternativly you can specify the :exept option with a list of features which should not be loaded:

  simple_localization :language => :de, :except => [:localized_models, :localized_date_and_time]

This will load all features except the localized_models and localized_date_and_time features. The opposite way (only specify features which sould be loaded) is also possible by using the :only option.

  simple_localization :language => :de, :only => [:localized_models, :localized_date_and_time]

This will only load the localized_models and localized_date_and_time features, ignoring all others.

If you use this plugin to localize you application (with the localized_application feature) it may also come in handy to move the directory containing the language files to a more important place. This can be done with the :lang_file_dir option:

  simple_localization :language => :de, :lang_file_dir => "#{RAILS_ROOT}/app/languages", :only => [:localized_application]

This example expects the language files in the app/languages directory of your rails application. By default the language files are located in the languages directory of the Simple Localization plugin.

[Source]

     # File lib/base.rb, line 47
 47: def simple_localization(options)
 48:   # available options: language, languages, *options (from the Language module), *features
 49:   lang = ArkanisDevelopment::SimpleLocalization::Language
 50:   feature_manager = ArkanisDevelopment::SimpleLocalization::FeatureManager.instance
 51:   lang_options = lang.options.dup
 52:   lang_file_dirs = lang_options.delete :lang_file_dirs
 53:   features = feature_manager.all_features
 54:   
 55:   default_options = {:language => nil, :languages => nil, :lang_file_dir => nil, :lang_file_dirs => nil}.update lang_options
 56:   features.each{|feature| default_options[feature.to_sym] = true}
 57:   options.reverse_merge! default_options
 58:   
 59:   # Analyse the language and lang_file_dir options and add default values if
 60:   # necessary or possible
 61:   languages = [options.delete(:languages), options.delete(:language)].flatten.compact.uniq
 62:   languages << :de if languages.empty?
 63:   lang_file_dirs = [lang_file_dirs, options.delete(:lang_file_dir), options.delete(:lang_file_dirs)].flatten.compact.uniq
 64:   lang_file_dirs << "#{RAILS_ROOT}/app/languages" if File.directory? "#{RAILS_ROOT}/app/languages"
 65:   
 66:   if options[:only]
 67:     feature_manager.load options[:only]
 68:   elsif options[:except]
 69:     feature_manager.load features
 70:     feature_manager.disable options[:except]
 71:   else
 72:     feature_manager.load(options.collect { |feature, enabled| feature if enabled }.compact)
 73:   end
 74:   
 75:   unless feature_manager.unwanted_features.empty?
 76:     RAILS_DEFAULT_LOGGER.warn "Simple Localization plugin configuration:\n" +
 77:       "  You don't want the features #{feature_manager.unwanted_features.join(', ')} to be loaded.\n" +
 78:       "  However to work with rails observers these features are loaded at the end of the plugins init.rb.\n" +
 79:       '  To suppress a preloaded feature please look into the plugins readme file (chapter "Preloaded features").'
 80:   end
 81:   
 82:   # Load the Language module and load the language files and the features
 83:   lang.options.keys.each do |option|
 84:     lang.options[option] = options[option] if options.key? option
 85:   end
 86:   lang.lang_file_dirs = lang_file_dirs
 87:   lang.load(*languages)
 88:   
 89:   feature_manager.localization_init_features.each do |feature|
 90:     require File.dirname(__FILE__) + "/features/#{feature}"
 91:   end
 92:   
 93:   RAILS_DEFAULT_LOGGER.info "Initialized Simple Localization plugin:\n" +
 94:     "  languages: #{languages.join(', ')}\n" +
 95:     "  language file directories: #{lang.lang_file_dirs.join(', ')}\n" +
 96:     "  features: #{feature_manager.all_loaded_features.join(', ')}\n" +
 97:     "  disabled features: #{feature_manager.disabled_features.join(', ')}"
 98:   
 99:   ArkanisDevelopment::SimpleLocalization::Language.loaded_features = feature_manager.localization_init_features
100: end

[Validate]