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
|
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
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,6 @@ module LanguageHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def rtl?
|
def rtl?
|
||||||
@rtl ||= RTL_LANGUAGES.include? I18n.locale
|
@rtl ||= RTL_LANGUAGES.include? I18n.locale.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -16,21 +16,6 @@ require File.expand_path('../application', __FILE__)
|
||||||
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']
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,6 @@ Diaspora::Application.configure do
|
||||||
# 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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,6 @@ Diaspora::Application.configure do
|
||||||
# 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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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|
|
|
||||||
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.
|
AVAILABLE_LANGUAGE_CODES.each do |c|
|
||||||
# Maybe by loading our paths in the initializer hooks, they'll end up after the gem paths?
|
I18n.fallbacks[c] = [c]
|
||||||
class I18n::Railtie
|
if LANGUAGE_CODES_MAP.key?(c)
|
||||||
class << self
|
I18n.fallbacks[c].concat(LANGUAGE_CODES_MAP[c])
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue