2013-05-30 01:53:40 -04:00
|
|
|
module JsLocaleHelper
|
|
|
|
|
2017-08-09 13:57:58 -04:00
|
|
|
def self.plugin_client_files(locale_str)
|
2017-08-09 17:28:11 -04:00
|
|
|
Dir["#{Rails.root}/plugins/*/config/locales/client.#{locale_str}.yml"]
|
2017-08-09 13:57:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reloadable_plugins(locale, ctx)
|
|
|
|
return unless Rails.env.development?
|
|
|
|
plugin_client_files(locale.to_s).each do |file|
|
|
|
|
ctx.depend_on(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-28 16:26:36 -04:00
|
|
|
def self.plugin_translations(locale_str)
|
|
|
|
@plugin_translations ||= HashWithIndifferentAccess.new
|
|
|
|
|
|
|
|
@plugin_translations[locale_str] ||= begin
|
|
|
|
translations = {}
|
|
|
|
|
2017-08-09 13:57:58 -04:00
|
|
|
plugin_client_files(locale_str).each do |file|
|
2017-02-24 05:31:21 -05:00
|
|
|
if plugin_translations = YAML.load_file(file)[locale_str]
|
2016-09-30 03:01:42 -04:00
|
|
|
translations.deep_merge!(plugin_translations)
|
|
|
|
end
|
2016-09-28 16:26:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.load_translations(locale, opts = nil)
|
2015-08-20 12:03:43 -04:00
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
@loaded_translations = nil if opts[:force]
|
2017-08-09 13:57:58 -04:00
|
|
|
@plugin_translations = nil if opts[:force]
|
2015-08-20 12:03:43 -04:00
|
|
|
|
2015-07-15 17:23:41 -04:00
|
|
|
@loaded_translations ||= HashWithIndifferentAccess.new
|
2015-07-15 11:56:46 -04:00
|
|
|
@loaded_translations[locale] ||= begin
|
|
|
|
locale_str = locale.to_s
|
|
|
|
|
|
|
|
# load default translations
|
2017-11-16 16:21:53 -05:00
|
|
|
yml_file = "#{Rails.root}/config/locales/client.#{locale_str}.yml"
|
|
|
|
if File.exist?(yml_file)
|
|
|
|
translations = YAML.load_file(yml_file)
|
|
|
|
else
|
|
|
|
# If we can't find a base file in Discourse, it might only exist in a plugin
|
|
|
|
# so let's start with a basic object we can merge into
|
|
|
|
translations = {
|
|
|
|
locale_str => {
|
|
|
|
'js' => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2015-07-15 11:56:46 -04:00
|
|
|
|
|
|
|
# merge translations (plugin translations overwrite default translations)
|
2016-09-28 16:26:36 -04: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 11:56:46 -04:00
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
# deeply removes keys from "deleting_from" that are already present in "checking_hashes"
|
|
|
|
def self.deep_delete_matches(deleting_from, checking_hashes)
|
2015-07-15 11:56:46 -04:00
|
|
|
checking_hashes.compact!
|
|
|
|
|
|
|
|
new_hash = deleting_from.dup
|
|
|
|
deleting_from.each do |key, value|
|
2017-02-24 05:31:21 -05:00
|
|
|
if value.is_a?(Hash)
|
|
|
|
new_at_key = deep_delete_matches(deleting_from[key], checking_hashes.map { |h| h[key] })
|
2015-07-15 11:56:46 -04:00
|
|
|
if new_at_key.empty?
|
2017-02-24 05:31:21 -05:00
|
|
|
new_hash.delete(key)
|
2015-07-15 11:56:46 -04:00
|
|
|
else
|
|
|
|
new_hash[key] = new_at_key
|
|
|
|
end
|
|
|
|
else
|
2017-02-24 05:31:21 -05:00
|
|
|
if checking_hashes.any? { |h| h.include?(key) }
|
|
|
|
new_hash.delete(key)
|
2015-07-15 11:56:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
new_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_translations_merged(*locales)
|
|
|
|
@loaded_merges ||= {}
|
|
|
|
@loaded_merges[locales.join('-')] ||= begin
|
2015-07-15 17:30:16 -04:00
|
|
|
all_translations = {}
|
2015-07-15 11:56:46 -04:00
|
|
|
merged_translations = {}
|
2015-07-15 17:30:16 -04:00
|
|
|
loaded_locales = []
|
|
|
|
|
|
|
|
locales.map(&:to_s).each do |locale|
|
2017-02-24 05:31:21 -05:00
|
|
|
all_translations[locale] = load_translations(locale)
|
|
|
|
merged_translations[locale] = deep_delete_matches(all_translations[locale][locale], loaded_locales.map { |l| merged_translations[l] })
|
2015-07-15 17:30:16 -04:00
|
|
|
loaded_locales << locale
|
2015-07-15 11:56:46 -04:00
|
|
|
end
|
|
|
|
merged_translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-25 16:33:29 -04:00
|
|
|
def self.translations_for(locale_str)
|
2014-08-07 12:08:15 -04:00
|
|
|
current_locale = I18n.locale
|
2017-02-24 05:31:21 -05:00
|
|
|
locale_sym = locale_str.to_sym
|
|
|
|
site_locale = SiteSetting.default_locale.to_sym
|
2014-08-07 12:08:15 -04:00
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
I18n.locale = locale_sym
|
2013-05-30 01:53:40 -04:00
|
|
|
|
2016-09-28 16:26:36 -04:00
|
|
|
translations =
|
|
|
|
if Rails.env.development?
|
|
|
|
load_translations(locale_sym, force: true)
|
|
|
|
elsif locale_sym == :en
|
|
|
|
load_translations(locale_sym)
|
2015-07-15 17:25:24 -04:00
|
|
|
elsif locale_sym == site_locale || site_locale == :en
|
2016-09-28 16:26:36 -04:00
|
|
|
load_translations_merged(locale_sym, :en)
|
2015-07-15 17:25:24 -04:00
|
|
|
else
|
2016-09-28 16:26:36 -04:00
|
|
|
load_translations_merged(locale_sym, site_locale, :en)
|
2015-07-15 17:25:24 -04:00
|
|
|
end
|
2014-12-11 11:08:47 -05:00
|
|
|
|
2016-08-25 16:33:29 -04:00
|
|
|
I18n.locale = current_locale
|
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.output_locale(locale)
|
|
|
|
locale_str = locale.to_s
|
2017-02-24 05:31:21 -05:00
|
|
|
translations = Marshal.load(Marshal.dump(translations_for(locale_str)))
|
2016-08-25 16:33:29 -04:00
|
|
|
|
2017-03-24 15:42:23 -04:00
|
|
|
message_formats = strip_out_message_formats!(translations[locale_str]['js'])
|
|
|
|
message_formats.merge!(strip_out_message_formats!(translations[locale_str]['admin_js']))
|
|
|
|
result = generate_message_format(message_formats, locale_str)
|
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
translations.keys.each do |locale|
|
|
|
|
translations[locale].keys.each do |k|
|
|
|
|
translations[locale].delete(k) unless k == "js"
|
|
|
|
end
|
2016-08-25 16:33:29 -04:00
|
|
|
end
|
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
# I18n
|
2013-05-30 01:53:40 -04:00
|
|
|
result << "I18n.translations = #{translations.to_json};\n"
|
2013-06-11 03:25:50 -04:00
|
|
|
result << "I18n.locale = '#{locale_str}';\n"
|
2017-02-24 05:31:21 -05:00
|
|
|
result << "I18n.pluralizationRules.#{locale_str} = MessageFormat.locale.#{locale_str};\n" if locale_str != "en"
|
|
|
|
|
|
|
|
# moment
|
2013-06-11 03:25:50 -04:00
|
|
|
result << File.read("#{Rails.root}/lib/javascripts/moment.js")
|
2013-06-07 04:03:09 -04:00
|
|
|
result << moment_locale(locale_str)
|
2013-06-11 03:25:50 -04:00
|
|
|
result << moment_formats
|
2014-08-07 12:08:15 -04:00
|
|
|
|
2013-05-30 01:53:40 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-11 03:25:50 -04: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 02:38:02 -04:00
|
|
|
result << "moment.fn.relativeAge = function(opts){ return Discourse.Formatter.relativeAge(this.toDate(), opts)};\n"
|
2013-06-11 03:25:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.moment_format_function(name)
|
2014-03-26 10:55:40 -04:00
|
|
|
format = I18n.t("dates.#{name}")
|
2015-08-20 12:03:43 -04:00
|
|
|
"moment.fn.#{name.camelize(:lower)} = function(){ return this.format('#{format}'); };\n"
|
2013-06-11 03:25:50 -04:00
|
|
|
end
|
|
|
|
|
2013-06-07 04:03:09 -04:00
|
|
|
def self.moment_locale(locale_str)
|
2016-02-05 15:49:03 -05:00
|
|
|
# moment.js uses a different naming scheme for locale files
|
|
|
|
locale_str = locale_str.tr('_', '-').downcase
|
2017-02-24 05:31:21 -05:00
|
|
|
filename = "#{Rails.root}/lib/javascripts/moment_locale/#{locale_str}.js"
|
2016-02-05 15:49:03 -05:00
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
# try the language without the territory
|
|
|
|
locale_str = locale_str.split("-")[0]
|
|
|
|
filename = "#{Rails.root}/lib/javascripts/moment_locale/#{locale_str}.js" unless File.exists?(filename)
|
2016-02-05 15:49:03 -05:00
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
File.exists?(filename) ? File.read(filename) << "\n" : ""
|
2013-06-07 04:03:09 -04:00
|
|
|
end
|
|
|
|
|
2013-05-30 01:53:40 -04:00
|
|
|
def self.generate_message_format(message_formats, locale_str)
|
2017-07-27 21:20:09 -04:00
|
|
|
formats = message_formats.map { |k, v| k.inspect << " : " << compile_message_format(locale_str, v) }.join(", ")
|
2013-05-30 01:53:40 -04:00
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
filename = "#{Rails.root}/lib/javascripts/locale/#{locale_str}.js"
|
|
|
|
filename = "#{Rails.root}/lib/javascripts/locale/en.js" unless File.exists?(filename)
|
2016-04-08 14:49:50 -04:00
|
|
|
|
2017-02-24 05:31:21 -05:00
|
|
|
result = "MessageFormat = {locale: {}};\n"
|
|
|
|
result << "I18n._compiledMFs = {#{formats}};\n"
|
|
|
|
result << File.read(filename) << "\n"
|
|
|
|
result << File.read("#{Rails.root}/lib/javascripts/messageformat-lookup.js") << "\n"
|
2013-05-30 01:53:40 -04:00
|
|
|
end
|
|
|
|
|
2016-11-01 22:34:20 -04:00
|
|
|
def self.reset_context
|
2017-07-20 00:17:45 -04:00
|
|
|
@ctx&.dispose
|
2016-11-01 22:34:20 -04:00
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
|
2016-05-19 08:25:08 -04:00
|
|
|
@mutex = Mutex.new
|
|
|
|
def self.with_context
|
|
|
|
@mutex.synchronize do
|
|
|
|
yield @ctx ||= begin
|
|
|
|
ctx = MiniRacer::Context.new
|
2017-02-24 05:31:21 -05:00
|
|
|
ctx.load("#{Rails.root}/lib/javascripts/messageformat.js")
|
2016-05-19 08:25:08 -04:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 01:53:40 -04:00
|
|
|
def self.compile_message_format(locale, format)
|
2016-05-19 08:25:08 -04:00
|
|
|
with_context do |ctx|
|
2017-02-24 05:31:21 -05:00
|
|
|
path = "#{Rails.root}/lib/javascripts/locale/#{locale}.js"
|
2016-05-19 08:25:08 -04:00
|
|
|
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 01:53:40 -04:00
|
|
|
message = "Invalid Format: " << e.message
|
|
|
|
"function(){ return #{message.inspect};}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.strip_out_message_formats!(hash, prefix = "", rval = {})
|
2014-03-26 15:20:41 -04: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 01:53:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rval
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|