refactor locale initialization and selection a bit
This commit is contained in:
parent
280184c85e
commit
3c23364fbc
7 changed files with 51 additions and 66 deletions
|
|
@ -71,6 +71,7 @@ class ApplicationController < ActionController::Base
|
|||
else
|
||||
locale = request.preferred_language_from AVAILABLE_LANGUAGE_CODES
|
||||
locale ||= request.compatible_language_from AVAILABLE_LANGUAGE_CODES
|
||||
locale ||= DEFAULT_LANGUAGE
|
||||
I18n.locale = locale
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -26,6 +26,6 @@ module LanguageHelper
|
|||
end
|
||||
|
||||
def rtl?
|
||||
@rtl ||= RTL_LANGUAGES.include? I18n.locale
|
||||
@rtl ||= RTL_LANGUAGES.include? I18n.locale.to_s
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -16,21 +16,6 @@ require File.expand_path('../application', __FILE__)
|
|||
Haml::Template.options[:format] = :html5
|
||||
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
|
||||
USERNAME_BLACKLIST = ['admin', 'administrator', 'hostmaster', 'info', 'postmaster', 'root', 'ssladmin',
|
||||
'ssladministrator', 'sslwebmaster', 'sysadmin', 'webmaster', 'support', 'contact', 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo']
|
||||
|
|
|
|||
|
|
@ -46,9 +46,6 @@ Diaspora::Application.configure do
|
|||
# Disable delivery errors, bad email addresses will be ignored
|
||||
# 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!
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -43,9 +43,6 @@ Diaspora::Application.configure do
|
|||
# Disable delivery errors, bad email addresses will be ignored
|
||||
# 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!
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -3,31 +3,36 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
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.
|
||||
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
|
||||
I18n.default_locale = DEFAULT_LANGUAGE
|
||||
|
||||
I18n::Backend::Simple.send(:include, I18n::Backend::InterpolationFallbacks)
|
||||
|
||||
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
||||
AVAILABLE_LANGUAGE_CODES.each do |c|
|
||||
if LANGUAGE_CODES_MAP.key?(c)
|
||||
I18n.fallbacks[c.to_sym] = 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
|
||||
AVAILABLE_LANGUAGE_CODES.each do |c|
|
||||
I18n.fallbacks[c] = [c]
|
||||
if LANGUAGE_CODES_MAP.key?(c)
|
||||
I18n.fallbacks[c].concat(LANGUAGE_CODES_MAP[c])
|
||||
end
|
||||
I18n.fallbacks[c].concat([DEFAULT_LANGUAGE, "en"])
|
||||
end
|
||||
|
|
|
|||
|
|
@ -62,45 +62,45 @@ available:
|
|||
|
||||
fallbacks:
|
||||
en-GB:
|
||||
- :en
|
||||
- "en"
|
||||
en-US:
|
||||
- :en
|
||||
- "en"
|
||||
en_shaw:
|
||||
- :en
|
||||
- :en-GB
|
||||
- :en-US
|
||||
- "en"
|
||||
- "en-GB"
|
||||
- "en-US"
|
||||
en_pirate:
|
||||
- :en
|
||||
- :en-GB
|
||||
- :en-US
|
||||
- "en"
|
||||
- "en-GB"
|
||||
- "en-US"
|
||||
en_1337:
|
||||
- :en
|
||||
- :en-GB
|
||||
- :en-US
|
||||
- "en"
|
||||
- "en-GB"
|
||||
- "en-US"
|
||||
sv:
|
||||
- :sv-SE
|
||||
- "sv-SE"
|
||||
he:
|
||||
- :he-IL
|
||||
- "he-IL"
|
||||
es-AR:
|
||||
- :es
|
||||
- "es"
|
||||
es-CL:
|
||||
- :es
|
||||
- "es"
|
||||
es-MX:
|
||||
- :es
|
||||
- "es"
|
||||
gl:
|
||||
- :gl-ES
|
||||
- "gl-ES"
|
||||
zh:
|
||||
- :zh-CN
|
||||
- :zh-TW
|
||||
- "zh-CN"
|
||||
- "zh-TW"
|
||||
ur-PK:
|
||||
- :ur
|
||||
- "ur"
|
||||
de_formal:
|
||||
- :de
|
||||
- "de"
|
||||
|
||||
|
||||
rtl:
|
||||
- :ar
|
||||
- :he
|
||||
- :ur
|
||||
- :ur-PK
|
||||
- :ms
|
||||
- "ar"
|
||||
- "he"
|
||||
- "ur"
|
||||
- "ur-PK"
|
||||
- "ms"
|
||||
|
|
|
|||
Loading…
Reference in a new issue