mirror of
https://github.com/discourse/discourse.git
synced 2026-07-29 15:54:48 -05:00
Refactor to our own Discourse I18n backend
This removes some monkey patches and makes testing easier. It will also support database backed I18n changes.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
require 'i18n/backend/pluralization'
|
||||
|
||||
module I18n
|
||||
module Backend
|
||||
class DiscourseI18n < I18n::Backend::Simple
|
||||
include I18n::Backend::Pluralization
|
||||
|
||||
def available_locales
|
||||
# in case you are wondering this is:
|
||||
# Dir.glob( File.join(Rails.root, 'config', 'locales', 'client.*.yml') )
|
||||
# .map {|x| x.split('.')[-2]}.sort
|
||||
LocaleSiteSetting.supported_locales.map(&:to_sym)
|
||||
end
|
||||
|
||||
# force explicit loading
|
||||
def load_translations(*filenames)
|
||||
unless filenames.empty?
|
||||
filenames.flatten.each { |filename| load_file(filename) }
|
||||
end
|
||||
end
|
||||
|
||||
def fallbacks(locale)
|
||||
[locale, SiteSetting.default_locale.to_sym, :en].uniq.compact
|
||||
end
|
||||
|
||||
def exists?(locale, key)
|
||||
fallbacks(locale).each do |fallback|
|
||||
begin
|
||||
return true if super(fallback, key)
|
||||
rescue I18n::InvalidLocale
|
||||
# we do nothing when the locale is invalid, as this is a fallback anyways.
|
||||
end
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user