2013-05-30 13:53:40 +08:00
|
|
|
module JsLocaleHelper
|
|
|
|
|
2016-09-29 04:26:36 +08:00
|
|
|
def self.plugin_translations(locale_str)
|
|
|
|
@plugin_translations ||= HashWithIndifferentAccess.new
|
|
|
|
|
|
|
|
@plugin_translations[locale_str] ||= begin
|
|
|
|
translations = {}
|
|
|
|
|
|
|
|
Dir["#{Rails.root}/plugins/*/config/locales/client.#{locale_str}.yml"].each do |file|
|
2016-09-30 15:01:42 +08:00
|
|
|
if plugin_translations = YAML::load(File.open(file))[locale_str]
|
|
|
|
translations.deep_merge!(plugin_translations)
|
|
|
|
end
|
2016-09-29 04:26:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-21 00:03:43 +08:00
|
|
|
def self.load_translations(locale, opts=nil)
|
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
@loaded_translations = nil if opts[:force]
|
|
|
|
|
2015-07-16 05:23:41 +08:00
|
|
|
@loaded_translations ||= HashWithIndifferentAccess.new
|
2015-07-15 23:56:46 +08:00
|
|
|
@loaded_translations[locale] ||= begin
|
|
|
|
locale_str = locale.to_s
|
|
|
|
|
|
|
|
# load default translations
|
|
|
|
translations = YAML::load(File.open("#{Rails.root}/config/locales/client.#{locale_str}.yml"))
|
|
|
|
|
|
|
|
# merge translations (plugin translations overwrite default translations)
|
2016-09-29 04:26:36 +08:00
|
|
|
translations[locale_str]['js'].deep_merge!(plugin_translations(locale_str)['js']) if translations[locale_str] && plugin_translations(locale_str) && plugin_translations(locale_str)['js']
|
2015-07-15 23:56:46 +08:00
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# purpose-built recursive algorithm ahoy!
|
|
|
|
def self.deep_delete_matches(deleting_from, *checking_hashes)
|
|
|
|
checking_hashes.compact!
|
|
|
|
|
|
|
|
new_hash = deleting_from.dup
|
|
|
|
deleting_from.each do |key, value|
|
|
|
|
if value.is_a? Hash
|
|
|
|
# Recurse
|
|
|
|
new_at_key = deep_delete_matches(deleting_from[key], *(checking_hashes.map {|h| h[key]}))
|
|
|
|
if new_at_key.empty?
|
|
|
|
new_hash.delete key
|
|
|
|
else
|
|
|
|
new_hash[key] = new_at_key
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if checking_hashes.any? {|h| h.include? key}
|
|
|
|
new_hash.delete key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
new_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_translations_merged(*locales)
|
|
|
|
@loaded_merges ||= {}
|
|
|
|
@loaded_merges[locales.join('-')] ||= begin
|
2015-07-16 05:30:16 +08:00
|
|
|
all_translations = {}
|
2015-07-15 23:56:46 +08:00
|
|
|
merged_translations = {}
|
2015-07-16 05:30:16 +08:00
|
|
|
loaded_locales = []
|
|
|
|
|
|
|
|
locales.map(&:to_s).each do |locale|
|
|
|
|
all_translations[locale] = JsLocaleHelper.load_translations locale
|
|
|
|
merged_translations[locale] = deep_delete_matches(all_translations[locale][locale], *loaded_locales.map { |l| merged_translations[l] })
|
|
|
|
loaded_locales << locale
|
2015-07-15 23:56:46 +08:00
|
|
|
end
|
|
|
|
merged_translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-26 04:33:29 +08:00
|
|
|
def self.translations_for(locale_str)
|
|
|
|
locale_sym = locale_str.to_sym
|
2015-07-16 05:23:41 +08:00
|
|
|
|
2014-08-08 00:08:15 +08:00
|
|
|
current_locale = I18n.locale
|
2015-07-16 05:23:41 +08:00
|
|
|
I18n.locale = locale_sym
|
2014-08-08 00:08:15 +08:00
|
|
|
|
2015-07-15 23:56:46 +08:00
|
|
|
site_locale = SiteSetting.default_locale.to_sym
|
2013-05-30 13:53:40 +08:00
|
|
|
|
2016-09-29 04:26:36 +08:00
|
|
|
translations =
|
|
|
|
if Rails.env.development?
|
|
|
|
load_translations(locale_sym, force: true)
|
|
|
|
elsif locale_sym == :en
|
|
|
|
load_translations(locale_sym)
|
2015-07-16 05:25:24 +08:00
|
|
|
elsif locale_sym == site_locale || site_locale == :en
|
2016-09-29 04:26:36 +08:00
|
|
|
load_translations_merged(locale_sym, :en)
|
2015-07-16 05:25:24 +08:00
|
|
|
else
|
2016-09-29 04:26:36 +08:00
|
|
|
load_translations_merged(locale_sym, site_locale, :en)
|
2015-07-16 05:25:24 +08:00
|
|
|
end
|
2014-12-12 00:08:47 +08:00
|
|
|
|
2016-08-26 04:33:29 +08:00
|
|
|
I18n.locale = current_locale
|
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.output_locale(locale)
|
|
|
|
locale_str = locale.to_s
|
|
|
|
translations = translations_for(locale_str).dup
|
|
|
|
|
|
|
|
translations[locale_str].keys.each do |k|
|
|
|
|
translations[locale_str].delete(k) unless k == "js"
|
|
|
|
end
|
|
|
|
|
2013-05-30 13:53:40 +08:00
|
|
|
message_formats = strip_out_message_formats!(translations[locale_str]['js'])
|
|
|
|
|
|
|
|
result = generate_message_format(message_formats, locale_str)
|
|
|
|
|
|
|
|
result << "I18n.translations = #{translations.to_json};\n"
|
2013-06-11 15:25:50 +08:00
|
|
|
result << "I18n.locale = '#{locale_str}';\n"
|
|
|
|
# loading moment here cause we must customize it
|
|
|
|
result << File.read("#{Rails.root}/lib/javascripts/moment.js")
|
2013-06-07 16:03:09 +08:00
|
|
|
result << moment_locale(locale_str)
|
2013-06-11 15:25:50 +08:00
|
|
|
result << moment_formats
|
2014-08-08 00:08:15 +08:00
|
|
|
|
2013-05-30 13:53:40 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-11 15:25:50 +08:00
|
|
|
def self.moment_formats
|
|
|
|
result = ""
|
|
|
|
result << moment_format_function('short_date_no_year')
|
|
|
|
result << moment_format_function('short_date')
|
|
|
|
result << moment_format_function('long_date')
|
2013-06-12 14:38:02 +08:00
|
|
|
result << "moment.fn.relativeAge = function(opts){ return Discourse.Formatter.relativeAge(this.toDate(), opts)};\n"
|
2013-06-11 15:25:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.moment_format_function(name)
|
2014-03-26 22:55:40 +08:00
|
|
|
format = I18n.t("dates.#{name}")
|
2015-08-21 00:03:43 +08:00
|
|
|
"moment.fn.#{name.camelize(:lower)} = function(){ return this.format('#{format}'); };\n"
|
2013-06-11 15:25:50 +08:00
|
|
|
end
|
|
|
|
|
2013-06-07 16:03:09 +08:00
|
|
|
def self.moment_locale(locale_str)
|
2016-02-06 04:49:03 +08:00
|
|
|
# moment.js uses a different naming scheme for locale files
|
|
|
|
locale_str = locale_str.tr('_', '-').downcase
|
2013-07-25 09:09:29 +08:00
|
|
|
filename = Rails.root + "lib/javascripts/moment_locale/#{locale_str}.js"
|
2016-02-06 04:49:03 +08:00
|
|
|
|
|
|
|
unless File.exists?(filename)
|
|
|
|
# try the language without the territory
|
|
|
|
locale_str = locale_str.partition('-').first
|
|
|
|
filename = Rails.root + "lib/javascripts/moment_locale/#{locale_str}.js"
|
|
|
|
end
|
|
|
|
|
2013-06-07 16:03:09 +08:00
|
|
|
if File.exists?(filename)
|
|
|
|
File.read(filename) << "\n"
|
|
|
|
end || ""
|
|
|
|
end
|
|
|
|
|
2013-05-30 13:53:40 +08:00
|
|
|
def self.generate_message_format(message_formats, locale_str)
|
|
|
|
|
|
|
|
result = "MessageFormat = {locale: {}};\n"
|
2013-07-25 09:16:07 +08:00
|
|
|
|
2016-04-09 02:49:50 +08:00
|
|
|
formats = message_formats.map{|k,v| k.inspect << " : " << compile_message_format(locale_str,v)}.join(", ")
|
|
|
|
result << "I18n._compiledMFs = {#{formats}};\n\n"
|
|
|
|
|
2013-07-25 09:16:07 +08:00
|
|
|
filename = Rails.root + "lib/javascripts/locale/#{locale_str}.js"
|
|
|
|
filename = Rails.root + "lib/javascripts/locale/en.js" unless File.exists?(filename)
|
2016-04-09 02:49:50 +08:00
|
|
|
result << File.read(filename) << "\n\n"
|
|
|
|
|
|
|
|
result << File.read("#{Rails.root}/lib/javascripts/messageformat-lookup.js")
|
2013-07-25 09:16:07 +08:00
|
|
|
|
2016-04-09 02:49:50 +08:00
|
|
|
result
|
2013-05-30 13:53:40 +08:00
|
|
|
end
|
|
|
|
|
2016-11-02 10:34:20 +08:00
|
|
|
def self.reset_context
|
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
|
2016-05-19 20:25:08 +08:00
|
|
|
@mutex = Mutex.new
|
|
|
|
def self.with_context
|
|
|
|
@mutex.synchronize do
|
|
|
|
yield @ctx ||= begin
|
|
|
|
ctx = MiniRacer::Context.new
|
|
|
|
ctx.load(Rails.root + 'lib/javascripts/messageformat.js')
|
|
|
|
ctx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 13:53:40 +08:00
|
|
|
def self.compile_message_format(locale, format)
|
2016-05-19 20:25:08 +08:00
|
|
|
with_context do |ctx|
|
|
|
|
path = Rails.root + "lib/javascripts/locale/#{locale}.js"
|
|
|
|
ctx.load(path) if File.exists?(path)
|
|
|
|
ctx.eval("mf = new MessageFormat('#{locale}');")
|
|
|
|
ctx.eval("mf.precompile(mf.parse(#{format.inspect}))")
|
|
|
|
end
|
|
|
|
rescue MiniRacer::EvalError => e
|
2013-05-30 13:53:40 +08:00
|
|
|
message = "Invalid Format: " << e.message
|
|
|
|
"function(){ return #{message.inspect};}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.strip_out_message_formats!(hash, prefix = "", rval = {})
|
2014-03-27 03:20:41 +08:00
|
|
|
if hash.is_a?(Hash)
|
|
|
|
hash.each do |key, value|
|
|
|
|
if value.is_a?(Hash)
|
|
|
|
rval.merge!(strip_out_message_formats!(value, prefix + (prefix.length > 0 ? "." : "") << key, rval))
|
|
|
|
elsif key.to_s.end_with?("_MF")
|
|
|
|
rval[prefix + (prefix.length > 0 ? "." : "") << key] = value
|
|
|
|
hash.delete(key)
|
2013-05-30 13:53:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rval
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|