2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-30 00:53:40 -05:00
|
|
|
module JsLocaleHelper
|
2017-08-09 12:57:58 -05:00
|
|
|
def self.plugin_client_files(locale_str)
|
2021-04-15 20:12:34 -05:00
|
|
|
files = Dir["#{Rails.root}/plugins/*/config/locales/client*.#{locale_str}.yml"]
|
|
|
|
I18n::Backend::DiscourseI18n.sort_locale_files(files)
|
2017-08-09 12:57:58 -05:00
|
|
|
end
|
|
|
|
|
2019-10-26 04:55:54 -05:00
|
|
|
def self.reloadable_plugins(locale_sym, ctx)
|
2017-08-09 12:57:58 -05:00
|
|
|
return unless Rails.env.development?
|
2019-10-26 04:55:54 -05:00
|
|
|
I18n.fallbacks[locale_sym].each do |locale|
|
2023-01-09 06:10:19 -06:00
|
|
|
plugin_client_files(locale.to_s).each { |file| ctx.depend_on(file) }
|
2017-08-09 12:57:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-28 15:26:36 -05:00
|
|
|
def self.plugin_translations(locale_str)
|
|
|
|
@plugin_translations ||= HashWithIndifferentAccess.new
|
|
|
|
|
|
|
|
@plugin_translations[locale_str] ||= begin
|
|
|
|
translations = {}
|
|
|
|
|
2017-08-09 12:57:58 -05:00
|
|
|
plugin_client_files(locale_str).each do |file|
|
2017-02-24 04:31:21 -06:00
|
|
|
if plugin_translations = YAML.load_file(file)[locale_str]
|
2016-09-30 02:01:42 -05:00
|
|
|
translations.deep_merge!(plugin_translations)
|
|
|
|
end
|
2016-09-28 15:26:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 15:55:31 -06:00
|
|
|
def self.load_translations(locale)
|
2015-07-15 16:23:41 -05:00
|
|
|
@loaded_translations ||= HashWithIndifferentAccess.new
|
2015-07-15 10:56:46 -05:00
|
|
|
@loaded_translations[locale] ||= begin
|
|
|
|
locale_str = locale.to_s
|
|
|
|
|
|
|
|
# load default translations
|
2017-11-16 15:21:53 -06: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
|
2023-01-09 06:10:19 -06:00
|
|
|
translations = { locale_str => { "js" => {}, "admin_js" => {}, "wizard_js" => {} } }
|
2017-11-16 15:21:53 -06:00
|
|
|
end
|
2015-07-15 10:56:46 -05:00
|
|
|
|
|
|
|
# merge translations (plugin translations overwrite default translations)
|
2017-11-24 13:11:01 -06:00
|
|
|
if translations[locale_str] && plugin_translations(locale_str)
|
2023-01-09 06:10:19 -06:00
|
|
|
translations[locale_str]["js"] ||= {}
|
|
|
|
translations[locale_str]["admin_js"] ||= {}
|
|
|
|
translations[locale_str]["wizard_js"] ||= {}
|
2018-06-12 09:09:40 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if plugin_translations(locale_str)["js"]
|
|
|
|
translations[locale_str]["js"].deep_merge!(plugin_translations(locale_str)["js"])
|
|
|
|
end
|
|
|
|
if plugin_translations(locale_str)["admin_js"]
|
|
|
|
translations[locale_str]["admin_js"].deep_merge!(
|
|
|
|
plugin_translations(locale_str)["admin_js"],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if plugin_translations(locale_str)["wizard_js"]
|
|
|
|
translations[locale_str]["wizard_js"].deep_merge!(
|
|
|
|
plugin_translations(locale_str)["wizard_js"],
|
|
|
|
)
|
|
|
|
end
|
2017-11-24 13:11:01 -06:00
|
|
|
end
|
2015-07-15 10:56:46 -05:00
|
|
|
|
|
|
|
translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-24 04:31:21 -06: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 10:56:46 -05:00
|
|
|
checking_hashes.compact!
|
|
|
|
|
|
|
|
new_hash = deleting_from.dup
|
|
|
|
deleting_from.each do |key, value|
|
2017-02-24 04:31:21 -06:00
|
|
|
if value.is_a?(Hash)
|
|
|
|
new_at_key = deep_delete_matches(deleting_from[key], checking_hashes.map { |h| h[key] })
|
2015-07-15 10:56:46 -05:00
|
|
|
if new_at_key.empty?
|
2017-02-24 04:31:21 -06:00
|
|
|
new_hash.delete(key)
|
2015-07-15 10:56:46 -05:00
|
|
|
else
|
|
|
|
new_hash[key] = new_at_key
|
|
|
|
end
|
|
|
|
else
|
2023-01-09 06:10:19 -06:00
|
|
|
new_hash.delete(key) if checking_hashes.any? { |h| h.include?(key) }
|
2015-07-15 10:56:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
new_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_translations_merged(*locales)
|
2019-03-07 11:51:12 -06:00
|
|
|
locales = locales.uniq.compact
|
2015-07-15 10:56:46 -05:00
|
|
|
@loaded_merges ||= {}
|
2023-01-09 06:10:19 -06:00
|
|
|
@loaded_merges[locales.join("-")] ||= begin
|
2015-07-15 16:30:16 -05:00
|
|
|
all_translations = {}
|
2015-07-15 10:56:46 -05:00
|
|
|
merged_translations = {}
|
2015-07-15 16:30:16 -05:00
|
|
|
loaded_locales = []
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
locales
|
|
|
|
.map(&:to_s)
|
|
|
|
.each do |locale|
|
|
|
|
all_translations[locale] = load_translations(locale)
|
|
|
|
merged_translations[locale] = deep_delete_matches(
|
|
|
|
all_translations[locale][locale],
|
|
|
|
loaded_locales.map { |l| merged_translations[l] },
|
|
|
|
)
|
|
|
|
loaded_locales << locale
|
|
|
|
end
|
2015-07-15 10:56:46 -05:00
|
|
|
merged_translations
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-23 14:23:31 -05:00
|
|
|
def self.clear_cache!
|
|
|
|
@loaded_translations = nil
|
|
|
|
@plugin_translations = nil
|
|
|
|
@loaded_merges = nil
|
|
|
|
end
|
|
|
|
|
2016-08-25 15:33:29 -05:00
|
|
|
def self.translations_for(locale_str)
|
2019-05-23 14:23:31 -05:00
|
|
|
clear_cache! if Rails.env.development?
|
2019-01-15 15:55:31 -06:00
|
|
|
|
2019-05-10 18:38:58 -05:00
|
|
|
locale_sym = locale_str.to_sym
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
translations =
|
|
|
|
I18n.with_locale(locale_sym) do
|
|
|
|
if locale_sym == :en
|
|
|
|
load_translations(locale_sym)
|
|
|
|
else
|
|
|
|
load_translations_merged(*I18n.fallbacks[locale_sym])
|
|
|
|
end
|
2015-07-15 16:25:24 -05:00
|
|
|
end
|
2019-05-23 14:23:31 -05:00
|
|
|
|
|
|
|
Marshal.load(Marshal.dump(translations))
|
2016-08-25 15:33:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.output_locale(locale)
|
|
|
|
locale_str = locale.to_s
|
2018-01-25 05:09:18 -06:00
|
|
|
fallback_locale_str = LocaleSiteSetting.fallback_locale(locale_str)&.to_s
|
2019-05-23 14:23:31 -05:00
|
|
|
translations = translations_for(locale_str)
|
2016-08-25 15:33:29 -05:00
|
|
|
|
2019-05-15 16:43:00 -05:00
|
|
|
message_formats = remove_message_formats!(translations, locale)
|
2019-02-19 07:42:58 -06:00
|
|
|
mf_locale, mf_filename = find_message_format_locale([locale_str], fallback_to_english: true)
|
2018-01-25 05:09:18 -06:00
|
|
|
result = generate_message_format(message_formats, mf_locale, mf_filename)
|
2017-03-24 14:42:23 -05:00
|
|
|
|
2018-01-08 17:23:49 -06:00
|
|
|
translations.keys.each do |l|
|
2023-01-09 06:10:19 -06:00
|
|
|
translations[l].keys.each { |k| translations[l].delete(k) unless k == "js" }
|
2016-08-25 15:33:29 -05:00
|
|
|
end
|
|
|
|
|
2017-02-24 04:31:21 -06:00
|
|
|
# I18n
|
2013-05-30 00:53:40 -05:00
|
|
|
result << "I18n.translations = #{translations.to_json};\n"
|
2013-06-11 02:25:50 -05:00
|
|
|
result << "I18n.locale = '#{locale_str}';\n"
|
2023-01-09 06:10:19 -06:00
|
|
|
if fallback_locale_str && fallback_locale_str != "en"
|
|
|
|
result << "I18n.fallbackLocale = '#{fallback_locale_str}';\n"
|
|
|
|
end
|
|
|
|
if mf_locale != "en"
|
|
|
|
result << "I18n.pluralizationRules.#{locale_str} = MessageFormat.locale.#{mf_locale};\n"
|
|
|
|
end
|
2017-02-24 04:31:21 -06:00
|
|
|
|
|
|
|
# moment
|
2019-02-12 12:57:52 -06:00
|
|
|
result << File.read("#{Rails.root}/vendor/assets/javascripts/moment.js")
|
|
|
|
result << File.read("#{Rails.root}/vendor/assets/javascripts/moment-timezone-with-data.js")
|
2013-06-07 03:03:09 -05:00
|
|
|
result << moment_locale(locale_str)
|
2019-02-25 13:40:02 -06:00
|
|
|
result << moment_locale(locale_str, timezone_names: true)
|
2013-06-11 02:25:50 -05:00
|
|
|
result << moment_formats
|
2014-08-07 11:08:15 -05:00
|
|
|
|
2013-05-30 00:53:40 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2021-12-16 14:53:50 -06:00
|
|
|
def self.output_client_overrides(main_locale)
|
|
|
|
all_overrides = {}
|
|
|
|
has_overrides = false
|
2019-11-04 19:15:44 -06:00
|
|
|
|
2021-12-16 14:53:50 -06:00
|
|
|
I18n.fallbacks[main_locale].each do |locale|
|
2023-01-09 06:10:19 -06:00
|
|
|
overrides =
|
|
|
|
all_overrides[locale] = TranslationOverride
|
|
|
|
.where(locale: locale)
|
|
|
|
.where("translation_key LIKE 'js.%' OR translation_key LIKE 'admin_js.%'")
|
|
|
|
.pluck(:translation_key, :value, :compiled_js)
|
2019-11-04 19:15:44 -06:00
|
|
|
|
2021-12-16 14:53:50 -06:00
|
|
|
has_overrides ||= overrides.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
return "" if !has_overrides
|
|
|
|
|
|
|
|
result = +"I18n._overrides = {};"
|
|
|
|
existing_keys = Set.new
|
2021-12-16 09:54:45 -06:00
|
|
|
message_formats = []
|
|
|
|
|
2021-12-16 14:53:50 -06:00
|
|
|
all_overrides.each do |locale, overrides|
|
|
|
|
translations = {}
|
|
|
|
|
|
|
|
overrides.each do |key, value, compiled_js|
|
|
|
|
next if existing_keys.include?(key)
|
|
|
|
existing_keys << key
|
|
|
|
|
|
|
|
if key.end_with?("_MF")
|
|
|
|
message_formats << "#{key.inspect}: #{compiled_js}"
|
|
|
|
else
|
|
|
|
translations[key] = value
|
|
|
|
end
|
2019-11-04 19:15:44 -06:00
|
|
|
end
|
|
|
|
|
2021-12-16 14:53:50 -06:00
|
|
|
result << "I18n._overrides['#{locale}'] = #{translations.to_json};" if translations.present?
|
|
|
|
end
|
2019-11-04 19:15:44 -06:00
|
|
|
|
2021-12-16 14:53:50 -06:00
|
|
|
result << "I18n._mfOverrides = {#{message_formats.join(", ")}};"
|
|
|
|
result
|
2019-11-04 19:15:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.output_extra_locales(bundle, locale)
|
|
|
|
translations = translations_for(locale)
|
2022-06-17 12:34:08 -05:00
|
|
|
locales = translations.keys
|
2019-11-04 19:15:44 -06:00
|
|
|
|
2022-06-17 12:34:08 -05:00
|
|
|
locales.each do |l|
|
2019-11-04 19:15:44 -06:00
|
|
|
translations[l].keys.each do |k|
|
|
|
|
bundle_translations = translations[l].delete(k)
|
|
|
|
translations[l].deep_merge!(bundle_translations) if k == bundle
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-17 07:50:21 -05:00
|
|
|
return "" if translations.blank?
|
|
|
|
|
2022-06-17 12:34:08 -05:00
|
|
|
output = +"if (!I18n.extras) { I18n.extras = {}; }"
|
DEV: convert I18n pseudo package into real package (discourse-i18n) (#23867)
Currently, `window.I18n` is defined in an old school hand written
script, inlined into locale/*.js by the Rails asset pipeline, and
then the global variable is shimmed into a pseudo AMD module later
in `module-shims.js`.
This approach has some problems – for one thing, when we add a new
V2 addon (e.g. in #23859), Embroider/Webpack is stricter about its
dependencies and won't let you `import from "I18n";` when `"I18n"`
isn't listed as one of its `dependencies` or `peerDependencies`.
This moves `I18n` into a real package – `discourse-i18n`. (I was
originally planning to keep the `I18n` name since it's a private
package anyway, but NPM packages are supposed to have lower case
names and that may cause problems with other tools.)
This package defines and exports a regular class, but also defines
the default global instance for backwards compatibility. We should
use the exported class in tests to make one-off instances without
mutating the global instance and having to clean it up after the
test run. However, I did not attempt that refactor in this PR.
Since `discourse-i18n` is now included by the app, the locale
scripts needs to be loaded after the app chunks. Since no "real"
work happens until later on when we kick things off in the boot
script, the order in which the script tags appear shouldn't be a
problem. Alternatively, we can rework the locale bundles to be more
lazy like everything else, and require/import them into the app.
I avoided renaming the imports in this commit since that would be
quite noisy and drowns out the actual changes here. Instead, I used
a Webpack alias to redirect the current `"I18n"` import to the new
package for the time being. In a separate commit later on, I'll
rename all the imports in oneshot and remove the alias. As always,
plugins and the legacy bundles (admin/wizard) still relies on the
runtime AMD shims regardless.
For the most part, I avoided refactoring the actual I18n code too
much other than making it a class, and some light stuff like `var`
into `let`.
However, now that it is in a reasonable format to work with (no
longer inside the global script context!) it may also be a good
opportunity to refactor and make clear what is intended to be
public API vs internal implementation details.
Speaking of, I took the librety to make `PLACEHOLDER`, `SEPARATOR`
and `I18nMissingInterpolationArgument` actual constants since it
seemed pretty clear to me those were just previously stashed on to
the `I18n` global to avoid polluting the global namespace, rather
than something we expect the consumers to set/replace.
2023-10-12 08:44:01 -05:00
|
|
|
locales.each do |l|
|
|
|
|
translations_json = translations[l].to_json
|
|
|
|
output << <<~JS
|
2022-06-17 12:34:08 -05:00
|
|
|
if (!I18n.extras["#{l}"]) { I18n.extras["#{l}"] = {}; }
|
DEV: convert I18n pseudo package into real package (discourse-i18n) (#23867)
Currently, `window.I18n` is defined in an old school hand written
script, inlined into locale/*.js by the Rails asset pipeline, and
then the global variable is shimmed into a pseudo AMD module later
in `module-shims.js`.
This approach has some problems – for one thing, when we add a new
V2 addon (e.g. in #23859), Embroider/Webpack is stricter about its
dependencies and won't let you `import from "I18n";` when `"I18n"`
isn't listed as one of its `dependencies` or `peerDependencies`.
This moves `I18n` into a real package – `discourse-i18n`. (I was
originally planning to keep the `I18n` name since it's a private
package anyway, but NPM packages are supposed to have lower case
names and that may cause problems with other tools.)
This package defines and exports a regular class, but also defines
the default global instance for backwards compatibility. We should
use the exported class in tests to make one-off instances without
mutating the global instance and having to clean it up after the
test run. However, I did not attempt that refactor in this PR.
Since `discourse-i18n` is now included by the app, the locale
scripts needs to be loaded after the app chunks. Since no "real"
work happens until later on when we kick things off in the boot
script, the order in which the script tags appear shouldn't be a
problem. Alternatively, we can rework the locale bundles to be more
lazy like everything else, and require/import them into the app.
I avoided renaming the imports in this commit since that would be
quite noisy and drowns out the actual changes here. Instead, I used
a Webpack alias to redirect the current `"I18n"` import to the new
package for the time being. In a separate commit later on, I'll
rename all the imports in oneshot and remove the alias. As always,
plugins and the legacy bundles (admin/wizard) still relies on the
runtime AMD shims regardless.
For the most part, I avoided refactoring the actual I18n code too
much other than making it a class, and some light stuff like `var`
into `let`.
However, now that it is in a reasonable format to work with (no
longer inside the global script context!) it may also be a good
opportunity to refactor and make clear what is intended to be
public API vs internal implementation details.
Speaking of, I took the librety to make `PLACEHOLDER`, `SEPARATOR`
and `I18nMissingInterpolationArgument` actual constants since it
seemed pretty clear to me those were just previously stashed on to
the `I18n` global to avoid polluting the global namespace, rather
than something we expect the consumers to set/replace.
2023-10-12 08:44:01 -05:00
|
|
|
Object.assign(I18n.extras["#{l}"], #{translations_json});
|
2022-06-17 12:34:08 -05:00
|
|
|
JS
|
DEV: convert I18n pseudo package into real package (discourse-i18n) (#23867)
Currently, `window.I18n` is defined in an old school hand written
script, inlined into locale/*.js by the Rails asset pipeline, and
then the global variable is shimmed into a pseudo AMD module later
in `module-shims.js`.
This approach has some problems – for one thing, when we add a new
V2 addon (e.g. in #23859), Embroider/Webpack is stricter about its
dependencies and won't let you `import from "I18n";` when `"I18n"`
isn't listed as one of its `dependencies` or `peerDependencies`.
This moves `I18n` into a real package – `discourse-i18n`. (I was
originally planning to keep the `I18n` name since it's a private
package anyway, but NPM packages are supposed to have lower case
names and that may cause problems with other tools.)
This package defines and exports a regular class, but also defines
the default global instance for backwards compatibility. We should
use the exported class in tests to make one-off instances without
mutating the global instance and having to clean it up after the
test run. However, I did not attempt that refactor in this PR.
Since `discourse-i18n` is now included by the app, the locale
scripts needs to be loaded after the app chunks. Since no "real"
work happens until later on when we kick things off in the boot
script, the order in which the script tags appear shouldn't be a
problem. Alternatively, we can rework the locale bundles to be more
lazy like everything else, and require/import them into the app.
I avoided renaming the imports in this commit since that would be
quite noisy and drowns out the actual changes here. Instead, I used
a Webpack alias to redirect the current `"I18n"` import to the new
package for the time being. In a separate commit later on, I'll
rename all the imports in oneshot and remove the alias. As always,
plugins and the legacy bundles (admin/wizard) still relies on the
runtime AMD shims regardless.
For the most part, I avoided refactoring the actual I18n code too
much other than making it a class, and some light stuff like `var`
into `let`.
However, now that it is in a reasonable format to work with (no
longer inside the global script context!) it may also be a good
opportunity to refactor and make clear what is intended to be
public API vs internal implementation details.
Speaking of, I took the librety to make `PLACEHOLDER`, `SEPARATOR`
and `I18nMissingInterpolationArgument` actual constants since it
seemed pretty clear to me those were just previously stashed on to
the `I18n` global to avoid polluting the global namespace, rather
than something we expect the consumers to set/replace.
2023-10-12 08:44:01 -05:00
|
|
|
end
|
2022-06-17 07:50:21 -05:00
|
|
|
|
2022-06-17 12:34:08 -05:00
|
|
|
output
|
2019-11-04 19:15:44 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
MOMENT_LOCALE_MAPPING ||= { "hy" => "hy-am" }
|
2019-03-28 08:47:37 -05:00
|
|
|
|
2019-02-25 13:40:02 -06:00
|
|
|
def self.find_moment_locale(locale_chain, timezone_names: false)
|
|
|
|
if timezone_names
|
|
|
|
path = "#{Rails.root}/vendor/assets/javascripts/moment-timezone-names-locale"
|
|
|
|
type = :moment_js_timezones
|
|
|
|
else
|
|
|
|
path = "#{Rails.root}/vendor/assets/javascripts/moment-locale"
|
|
|
|
type = :moment_js
|
|
|
|
end
|
2018-01-25 05:09:18 -06:00
|
|
|
|
2019-02-25 13:40:02 -06:00
|
|
|
find_locale(locale_chain, path, type, fallback_to_english: false) do |locale|
|
2019-03-28 08:47:37 -05:00
|
|
|
locale = MOMENT_LOCALE_MAPPING[locale] if MOMENT_LOCALE_MAPPING.key?(locale)
|
2019-02-19 08:27:30 -06:00
|
|
|
# moment.js uses a different naming scheme for locale files
|
2023-01-09 06:10:19 -06:00
|
|
|
locale.tr("_", "-").downcase
|
2019-02-19 08:27:30 -06:00
|
|
|
end
|
2018-01-25 05:09:18 -06:00
|
|
|
end
|
|
|
|
|
2019-02-19 07:42:58 -06:00
|
|
|
def self.find_message_format_locale(locale_chain, fallback_to_english:)
|
2018-01-25 05:09:18 -06:00
|
|
|
path = "#{Rails.root}/lib/javascripts/locale"
|
2019-02-19 07:42:58 -06:00
|
|
|
find_locale(locale_chain, path, :message_format, fallback_to_english: fallback_to_english)
|
2018-01-25 05:09:18 -06:00
|
|
|
end
|
|
|
|
|
2019-02-19 07:42:58 -06:00
|
|
|
def self.find_locale(locale_chain, path, type, fallback_to_english:)
|
2019-08-26 09:17:56 -05:00
|
|
|
locale_chain.map!(&:to_s)
|
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
locale_chain.each do |locale|
|
|
|
|
plugin_locale = DiscoursePluginRegistry.locales[locale]
|
|
|
|
return plugin_locale[type] if plugin_locale&.has_key?(type)
|
|
|
|
|
2019-02-19 09:07:19 -06:00
|
|
|
locale = yield(locale) if block_given?
|
2018-01-25 05:09:18 -06:00
|
|
|
filename = File.join(path, "#{locale}.js")
|
2023-01-09 06:10:19 -06:00
|
|
|
return locale, filename if File.exist?(filename)
|
2018-01-25 05:09:18 -06:00
|
|
|
end
|
|
|
|
|
2019-02-19 09:07:19 -06:00
|
|
|
locale_chain.map! { |locale| yield(locale) } if block_given?
|
2019-02-19 08:27:30 -06:00
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
# try again, but this time only with the language itself
|
2023-01-09 06:10:19 -06:00
|
|
|
locale_chain =
|
|
|
|
locale_chain.map { |l| l.split(/[-_]/)[0] }.uniq.reject { |l| locale_chain.include?(l) }
|
2019-02-19 09:07:19 -06:00
|
|
|
|
|
|
|
if locale_chain.any?
|
2019-02-19 07:42:58 -06:00
|
|
|
locale_data = find_locale(locale_chain, path, type, fallback_to_english: false)
|
2018-01-25 05:09:18 -06:00
|
|
|
return locale_data if locale_data
|
|
|
|
end
|
|
|
|
|
2021-05-20 20:43:47 -05:00
|
|
|
# English should always work
|
2018-01-25 05:09:18 -06:00
|
|
|
["en", File.join(path, "en.js")] if fallback_to_english
|
|
|
|
end
|
|
|
|
|
2013-06-11 02:25:50 -05:00
|
|
|
def self.moment_formats
|
2019-05-02 17:17:27 -05:00
|
|
|
result = +""
|
2023-01-09 06:10:19 -06:00
|
|
|
result << moment_format_function("short_date_no_year")
|
|
|
|
result << moment_format_function("short_date")
|
|
|
|
result << moment_format_function("long_date")
|
2013-06-12 01:38:02 -05:00
|
|
|
result << "moment.fn.relativeAge = function(opts){ return Discourse.Formatter.relativeAge(this.toDate(), opts)};\n"
|
2013-06-11 02:25:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.moment_format_function(name)
|
2014-03-26 09:55:40 -05:00
|
|
|
format = I18n.t("dates.#{name}")
|
2015-08-20 11:03:43 -05:00
|
|
|
"moment.fn.#{name.camelize(:lower)} = function(){ return this.format('#{format}'); };\n"
|
2013-06-11 02:25:50 -05:00
|
|
|
end
|
|
|
|
|
2019-02-25 13:40:02 -06:00
|
|
|
def self.moment_locale(locale, timezone_names: false)
|
|
|
|
_, filename = find_moment_locale([locale], timezone_names: timezone_names)
|
2018-01-25 05:09:18 -06:00
|
|
|
filename && File.exist?(filename) ? File.read(filename) << "\n" : ""
|
2013-06-07 03:03:09 -05:00
|
|
|
end
|
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
def self.generate_message_format(message_formats, locale, filename)
|
2023-01-09 06:10:19 -06:00
|
|
|
formats =
|
|
|
|
message_formats
|
|
|
|
.map { |k, v| k.inspect << " : " << compile_message_format(filename, locale, v) }
|
|
|
|
.join(", ")
|
2016-04-08 13:49:50 -05:00
|
|
|
|
2019-05-02 17:17:27 -05:00
|
|
|
result = +"MessageFormat = {locale: {}};\n"
|
2017-02-24 04:31:21 -06:00
|
|
|
result << "I18n._compiledMFs = {#{formats}};\n"
|
|
|
|
result << File.read(filename) << "\n"
|
2022-02-07 19:31:08 -06:00
|
|
|
|
|
|
|
if locale != "en"
|
|
|
|
# Include "en" pluralization rules for use in fallbacks
|
|
|
|
_, en_filename = find_message_format_locale(["en"], fallback_to_english: false)
|
|
|
|
result << File.read(en_filename) << "\n"
|
|
|
|
end
|
|
|
|
|
2017-02-24 04:31:21 -06:00
|
|
|
result << File.read("#{Rails.root}/lib/javascripts/messageformat-lookup.js") << "\n"
|
2013-05-30 00:53:40 -05:00
|
|
|
end
|
|
|
|
|
2016-11-01 21:34:20 -05:00
|
|
|
def self.reset_context
|
2017-07-19 23:17:45 -05:00
|
|
|
@ctx&.dispose
|
2016-11-01 21:34:20 -05:00
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
|
2016-05-19 07:25:08 -05:00
|
|
|
@mutex = Mutex.new
|
|
|
|
def self.with_context
|
|
|
|
@mutex.synchronize do
|
2023-01-09 06:10:19 -06:00
|
|
|
yield(
|
|
|
|
@ctx ||=
|
|
|
|
begin
|
|
|
|
ctx = MiniRacer::Context.new(timeout: 15_000, ensure_gc_after_idle: 2000)
|
2023-05-19 04:59:04 -05:00
|
|
|
ctx.load(
|
|
|
|
"#{Rails.root}/app/assets/javascripts/node_modules/messageformat/messageformat.js",
|
|
|
|
)
|
2023-01-09 06:10:19 -06:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
)
|
2016-05-19 07:25:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
def self.compile_message_format(path, locale, format)
|
2016-05-19 07:25:08 -05:00
|
|
|
with_context do |ctx|
|
2018-01-25 05:09:18 -06:00
|
|
|
ctx.load(path) if File.exist?(path)
|
2016-05-19 07:25:08 -05:00
|
|
|
ctx.eval("mf = new MessageFormat('#{locale}');")
|
|
|
|
ctx.eval("mf.precompile(mf.parse(#{format.inspect}))")
|
|
|
|
end
|
|
|
|
rescue MiniRacer::EvalError => e
|
2019-05-02 17:17:27 -05:00
|
|
|
message = +"Invalid Format: " << e.message
|
2013-05-30 00:53:40 -05:00
|
|
|
"function(){ return #{message.inspect};}"
|
|
|
|
end
|
|
|
|
|
2019-05-15 16:43:00 -05:00
|
|
|
def self.remove_message_formats!(translations, locale)
|
|
|
|
message_formats = {}
|
2023-01-09 06:10:19 -06:00
|
|
|
I18n.fallbacks[locale]
|
|
|
|
.map(&:to_s)
|
|
|
|
.each do |l|
|
|
|
|
next unless translations.key?(l)
|
2019-05-15 16:43:00 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
%w[js admin_js].each do |k|
|
|
|
|
message_formats.merge!(strip_out_message_formats!(translations[l][k]))
|
|
|
|
end
|
2019-05-15 16:43:00 -05:00
|
|
|
end
|
|
|
|
message_formats
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.strip_out_message_formats!(hash, prefix = "", message_formats = {})
|
2014-03-26 14:20:41 -05:00
|
|
|
if hash.is_a?(Hash)
|
|
|
|
hash.each do |key, value|
|
|
|
|
if value.is_a?(Hash)
|
2023-01-09 06:10:19 -06:00
|
|
|
message_formats.merge!(
|
|
|
|
strip_out_message_formats!(value, join_key(prefix, key), message_formats),
|
|
|
|
)
|
2014-03-26 14:20:41 -05:00
|
|
|
elsif key.to_s.end_with?("_MF")
|
2019-05-15 16:43:00 -05:00
|
|
|
message_formats[join_key(prefix, key)] = value
|
2014-03-26 14:20:41 -05:00
|
|
|
hash.delete(key)
|
2013-05-30 00:53:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-15 16:43:00 -05:00
|
|
|
message_formats
|
2013-05-30 00:53:40 -05:00
|
|
|
end
|
|
|
|
|
2019-05-15 16:43:00 -05:00
|
|
|
def self.join_key(prefix, key)
|
|
|
|
prefix.blank? ? key : "#{prefix}.#{key}"
|
|
|
|
end
|
2013-05-30 00:53:40 -05:00
|
|
|
end
|