Skip to content

Commit

Permalink
refactor locale initialization and selection a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Mar 21, 2012
1 parent 280184c commit 3c23364
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 65 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -71,6 +71,7 @@ def set_locale
else else
locale = request.preferred_language_from AVAILABLE_LANGUAGE_CODES locale = request.preferred_language_from AVAILABLE_LANGUAGE_CODES
locale ||= request.compatible_language_from AVAILABLE_LANGUAGE_CODES locale ||= request.compatible_language_from AVAILABLE_LANGUAGE_CODES
locale ||= DEFAULT_LANGUAGE
I18n.locale = locale I18n.locale = locale
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/language_helper.rb
Expand Up @@ -26,6 +26,6 @@ def direction_for(string)
end end


def rtl? def rtl?
@rtl ||= RTL_LANGUAGES.include? I18n.locale @rtl ||= RTL_LANGUAGES.include? I18n.locale.to_s
end end
end end
15 changes: 0 additions & 15 deletions config/environment.rb
Expand Up @@ -16,21 +16,6 @@ def sqlite?
Haml::Template.options[:format] = :html5 Haml::Template.options[:format] = :html5
Haml::Template.options[:escape_html] = true Haml::Template.options[:escape_html] = true


if File.exists?(File.expand_path("./config/locale_settings.yml"))
locale_settings = YAML::load(File.open(File.expand_path("./config/locale_settings.yml")))
AVAILABLE_LANGUAGES = (locale_settings['available'].length > 0) ? locale_settings['available'] : { :en => 'English' }
DEFAULT_LANGUAGE = (AVAILABLE_LANGUAGES.include?(locale_settings['default'])) ? locale_settings['default'] : AVAILABLE_LANGUAGES.keys[0].to_s
AVAILABLE_LANGUAGE_CODES = locale_settings['available'].keys.map { |v| v.to_s }
LANGUAGE_CODES_MAP = locale_settings['fallbacks']
RTL_LANGUAGES = locale_settings['rtl']
else
AVAILABLE_LANGUAGES = { :en => 'English' }
DEFAULT_LANGUAGE = 'en'
AVAILABLE_LANGUAGE_CODES = ['en']
LANGUAGE_CODES_MAP = {}
RTL_LANGUAGES = []
end

# Blacklist of usernames # Blacklist of usernames
USERNAME_BLACKLIST = ['admin', 'administrator', 'hostmaster', 'info', 'postmaster', 'root', 'ssladmin', USERNAME_BLACKLIST = ['admin', 'administrator', 'hostmaster', 'info', 'postmaster', 'root', 'ssladmin',
'ssladministrator', 'sslwebmaster', 'sysadmin', 'webmaster', 'support', 'contact', 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo'] 'ssladministrator', 'sslwebmaster', 'sysadmin', 'webmaster', 'support', 'contact', 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo']
Expand Down
3 changes: 0 additions & 3 deletions config/environments/production.rb
Expand Up @@ -46,9 +46,6 @@
# Disable delivery errors, bad email addresses will be ignored # Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false # config.action_mailer.raise_delivery_errors = false


# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
config.threadsafe! config.threadsafe!
end end


Expand Down
3 changes: 0 additions & 3 deletions config/environments/staging.rb
Expand Up @@ -43,9 +43,6 @@
# Disable delivery errors, bad email addresses will be ignored # Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false # config.action_mailer.raise_delivery_errors = false


# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
config.threadsafe! config.threadsafe!
end end


Expand Down
39 changes: 22 additions & 17 deletions config/initializers/locale.rb
Expand Up @@ -3,31 +3,36 @@
# the COPYRIGHT file. # the COPYRIGHT file.


require 'i18n_interpolation_fallbacks' require 'i18n_interpolation_fallbacks'
require "i18n/backend/fallbacks"


if File.exists?(File.expand_path("./config/locale_settings.yml"))
locale_settings = YAML::load(File.open(File.expand_path("./config/locale_settings.yml")))
AVAILABLE_LANGUAGES = (locale_settings['available'].length > 0) ? locale_settings['available'] : { "en" => 'English' }
AVAILABLE_LANGUAGE_CODES = locale_settings['available'].keys
DEFAULT_LANGUAGE = (AVAILABLE_LANGUAGE_CODES.include?(locale_settings['default'].to_s)) ? locale_settings['default'].to_s : "en"
LANGUAGE_CODES_MAP = locale_settings['fallbacks']
RTL_LANGUAGES = locale_settings['rtl']
else
AVAILABLE_LANGUAGES = { "en" => 'English' }
DEFAULT_LANGUAGE = "en"
AVAILABLE_LANGUAGE_CODES = ["en"]
LANGUAGE_CODES_MAP = {}
RTL_LANGUAGES = []
end



# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
I18n.default_locale = DEFAULT_LANGUAGE I18n.default_locale = DEFAULT_LANGUAGE


I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationFallbacks) I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationFallbacks)

I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

AVAILABLE_LANGUAGE_CODES.each do |c| AVAILABLE_LANGUAGE_CODES.each do |c|
I18n.fallbacks[c] = [c]
if LANGUAGE_CODES_MAP.key?(c) if LANGUAGE_CODES_MAP.key?(c)
I18n.fallbacks[c.to_sym] = LANGUAGE_CODES_MAP[c] I18n.fallbacks[c].concat(LANGUAGE_CODES_MAP[c])
I18n.fallbacks[c.to_sym].concat([c.to_sym, DEFAULT_LANGUAGE.to_sym, :en])
else
I18n.fallbacks[c.to_sym] = [c.to_sym, DEFAULT_LANGUAGE.to_sym, :en]
end
end

# There's almost certainly a better way to do this.
# Maybe by loading our paths in the initializer hooks, they'll end up after the gem paths?
class I18n::Railtie
class << self
def initialize_i18n_with_path_cleanup *args
initialize_i18n_without_path_cleanup *args
I18n.load_path.reject!{|path| path.match(/devise_invitable/) }
end
alias_method_chain :initialize_i18n, :path_cleanup
end end
I18n.fallbacks[c].concat([DEFAULT_LANGUAGE, "en"])
end end
52 changes: 26 additions & 26 deletions config/locale_settings.yml
Expand Up @@ -62,45 +62,45 @@ available:


fallbacks: fallbacks:
en-GB: en-GB:
- :en - "en"
en-US: en-US:
- :en - "en"
en_shaw: en_shaw:
- :en - "en"
- :en-GB - "en-GB"
- :en-US - "en-US"
en_pirate: en_pirate:
- :en - "en"
- :en-GB - "en-GB"
- :en-US - "en-US"
en_1337: en_1337:
- :en - "en"
- :en-GB - "en-GB"
- :en-US - "en-US"
sv: sv:
- :sv-SE - "sv-SE"
he: he:
- :he-IL - "he-IL"
es-AR: es-AR:
- :es - "es"
es-CL: es-CL:
- :es - "es"
es-MX: es-MX:
- :es - "es"
gl: gl:
- :gl-ES - "gl-ES"
zh: zh:
- :zh-CN - "zh-CN"
- :zh-TW - "zh-TW"
ur-PK: ur-PK:
- :ur - "ur"
de_formal: de_formal:
- :de - "de"




rtl: rtl:
- :ar - "ar"
- :he - "he"
- :ur - "ur"
- :ur-PK - "ur-PK"
- :ms - "ms"

0 comments on commit 3c23364

Please sign in to comment.