2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-19 20:25:08 +08:00
|
|
|
require "mini_racer"
|
2013-05-30 13:53:40 +08:00
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe JsLocaleHelper do
|
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 21:44:01 +08:00
|
|
|
let(:v8_ctx) do
|
2024-09-03 17:51:07 +08:00
|
|
|
discourse_node_modules = "#{Rails.root}/app/assets/javascripts/discourse/node_modules"
|
|
|
|
mf_runtime = "#{discourse_node_modules}/@messageformat/runtime"
|
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 21:44:01 +08:00
|
|
|
transpiler = DiscourseJsProcessor::Transpiler.new
|
|
|
|
ctx = MiniRacer::Context.new
|
2024-09-03 17:51:07 +08:00
|
|
|
ctx.load("#{discourse_node_modules}/loader.js/dist/loader/loader.js")
|
2023-11-07 01:49:21 +08:00
|
|
|
ctx.eval("var window = globalThis;")
|
2024-06-18 00:21:04 +08:00
|
|
|
{
|
2024-09-03 17:51:07 +08:00
|
|
|
"@messageformat/runtime/messages": "#{mf_runtime}/esm/messages.js",
|
|
|
|
"@messageformat/runtime": "#{mf_runtime}/esm/runtime.js",
|
|
|
|
"@messageformat/runtime/lib/cardinals": "#{mf_runtime}/esm/cardinals.js",
|
|
|
|
"make-plural/cardinals": "#{discourse_node_modules}/make-plural/cardinals.mjs",
|
2024-06-18 00:21:04 +08:00
|
|
|
"discourse-i18n": "#{Rails.root}/app/assets/javascripts/discourse-i18n/src/index.js",
|
|
|
|
}.each do |module_name, path|
|
|
|
|
ctx.eval(transpiler.perform(File.read(path), "", module_name.to_s))
|
|
|
|
end
|
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 21:44:01 +08:00
|
|
|
ctx.eval <<~JS
|
|
|
|
define("discourse/loader-shims", () => {})
|
|
|
|
JS
|
2024-06-18 00:21:04 +08:00
|
|
|
# As there are circular references in the return value, this raises an
|
|
|
|
# error if we let MiniRacer try to convert the value to JSON. Forcing
|
|
|
|
# returning `null` from `#eval` will prevent that.
|
|
|
|
ctx.eval("#{File.read("#{Rails.root}/app/assets/javascripts/locales/i18n.js")};null")
|
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 21:44:01 +08:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
2015-07-16 05:23:41 +08:00
|
|
|
module StubLoadTranslations
|
|
|
|
def set_translations(locale, translations)
|
|
|
|
@loaded_translations ||= HashWithIndifferentAccess.new
|
|
|
|
@loaded_translations[locale] = translations
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache!
|
|
|
|
@loaded_translations = nil
|
|
|
|
@loaded_merges = nil
|
|
|
|
end
|
|
|
|
end
|
2017-02-24 18:31:21 +08:00
|
|
|
|
2015-07-16 05:23:41 +08:00
|
|
|
JsLocaleHelper.extend StubLoadTranslations
|
|
|
|
|
DEV: Fix heisentest (#10946)
This should fix the following sporadic spec failure:
```
1) JsLocaleHelper performs fallbacks to English if a translation is not available
Failure/Error: expect(ctx.eval('I18n.translations.uk.js').keys).to contain_exactly("all_three", "english_and_user", "only_user", "site_and_user")
expected collection contained: ["all_three", "english_and_user", "only_user", "site_and_user"]
actual collection contained: ["about", "action_codes", "activity", "admin", "admin_title", "adplugin", "age", "akismet", "all_time..."voting", "week", "week_desc", "weekly", "wizard_required", "year", "year_desc", "yes_value", "you"]
the missing elements were: ["all_three", "english_and_user", "only_user", "site_and_user"]
the extra elements were: ["about", "action_codes", "activity", "admin", "admin_title", "adplugin", "age", "akismet", "all_time..."voting", "week", "week_desc", "weekly", "wizard_required", "year", "year_desc", "yes_value", "you"]
# ./spec/components/js_locale_helper_spec.rb:182:in `block (2 levels) in <main>'
# ./bundle/ruby/2.6.0/gems/webmock-3.9.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
```
2020-10-18 18:00:35 +08:00
|
|
|
before { JsLocaleHelper.clear_cache! }
|
|
|
|
after { JsLocaleHelper.clear_cache! }
|
2015-07-16 05:23:41 +08:00
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
describe "#output_locale" do
|
|
|
|
it "doesn't change the cached translations hash" do
|
|
|
|
I18n.locale = :fr
|
|
|
|
expect(JsLocaleHelper.output_locale("fr").length).to be > 0
|
|
|
|
expect(JsLocaleHelper.translations_for("fr")["fr"].keys).to contain_exactly(
|
|
|
|
"js",
|
|
|
|
"admin_js",
|
|
|
|
"wizard_js",
|
|
|
|
)
|
|
|
|
end
|
2013-05-30 13:53:40 +08:00
|
|
|
end
|
|
|
|
|
2020-05-07 04:57:14 +08:00
|
|
|
it "performs fallbacks to English if a translation is not available" do
|
2018-12-04 17:48:16 +08:00
|
|
|
JsLocaleHelper.set_translations(
|
|
|
|
"en",
|
|
|
|
"en" => {
|
2015-07-16 05:23:41 +08:00
|
|
|
"js" => {
|
2018-12-04 17:48:16 +08:00
|
|
|
"only_english" => "1-en",
|
|
|
|
"english_and_site" => "3-en",
|
|
|
|
"english_and_user" => "5-en",
|
|
|
|
"all_three" => "7-en",
|
2015-07-16 05:23:41 +08:00
|
|
|
},
|
2017-02-24 18:31:21 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-12-04 17:48:16 +08:00
|
|
|
JsLocaleHelper.set_translations(
|
|
|
|
"ru",
|
|
|
|
"ru" => {
|
2015-07-16 05:23:41 +08:00
|
|
|
"js" => {
|
2018-12-04 17:48:16 +08:00
|
|
|
"only_site" => "2-ru",
|
|
|
|
"english_and_site" => "3-ru",
|
|
|
|
"site_and_user" => "6-ru",
|
|
|
|
"all_three" => "7-ru",
|
2015-07-16 05:23:41 +08:00
|
|
|
},
|
2017-02-24 18:31:21 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-12-04 17:48:16 +08:00
|
|
|
JsLocaleHelper.set_translations(
|
|
|
|
"uk",
|
|
|
|
"uk" => {
|
2015-07-16 05:23:41 +08:00
|
|
|
"js" => {
|
2018-12-04 17:48:16 +08:00
|
|
|
"only_user" => "4-uk",
|
|
|
|
"english_and_user" => "5-uk",
|
|
|
|
"site_and_user" => "6-uk",
|
|
|
|
"all_three" => "7-uk",
|
2015-07-16 05:23:41 +08:00
|
|
|
},
|
2017-02-24 18:31:21 +08:00
|
|
|
},
|
|
|
|
)
|
2015-07-16 05:23:41 +08:00
|
|
|
|
|
|
|
expected = {
|
2018-12-04 17:48:16 +08:00
|
|
|
"none" => "[uk.js.none]",
|
|
|
|
"only_english" => "1-en",
|
2020-05-07 04:57:14 +08:00
|
|
|
"only_site" => "[uk.js.only_site]",
|
|
|
|
"english_and_site" => "3-en",
|
2018-12-04 17:48:16 +08:00
|
|
|
"only_user" => "4-uk",
|
|
|
|
"english_and_user" => "5-uk",
|
|
|
|
"site_and_user" => "6-uk",
|
2020-05-07 04:57:14 +08:00
|
|
|
"all_three" => "7-uk",
|
2015-07-16 05:23:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SiteSetting.default_locale = "ru"
|
|
|
|
I18n.locale = :uk
|
|
|
|
|
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 21:44:01 +08:00
|
|
|
v8_ctx.eval(JsLocaleHelper.output_locale(I18n.locale))
|
|
|
|
v8_ctx.eval('I18n.defaultLocale = "ru";')
|
2015-07-16 05:23:41 +08:00
|
|
|
|
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 21:44:01 +08:00
|
|
|
expect(v8_ctx.eval("I18n.translations").keys).to contain_exactly("uk", "en")
|
|
|
|
expect(v8_ctx.eval("I18n.translations.uk.js").keys).to contain_exactly(
|
2017-02-24 18:31:21 +08:00
|
|
|
"all_three",
|
|
|
|
"english_and_user",
|
|
|
|
"only_user",
|
|
|
|
"site_and_user",
|
2020-05-07 04:57:14 +08:00
|
|
|
)
|
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 21:44:01 +08:00
|
|
|
expect(v8_ctx.eval("I18n.translations.en.js").keys).to contain_exactly(
|
2020-05-07 04:57:14 +08:00
|
|
|
"only_english",
|
|
|
|
"english_and_site",
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
|
|
|
|
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 21:44:01 +08:00
|
|
|
expected.each do |key, expect|
|
|
|
|
expect(v8_ctx.eval("I18n.t(#{"js.#{key}".inspect})")).to eq(expect)
|
|
|
|
end
|
2015-07-16 01:21:06 +08:00
|
|
|
end
|
|
|
|
|
2013-07-25 09:09:29 +08:00
|
|
|
LocaleSiteSetting.values.each do |locale|
|
2013-08-24 05:35:01 +08:00
|
|
|
it "generates valid date helpers for #{locale[:value]} locale" do
|
|
|
|
js = JsLocaleHelper.output_locale(locale[:value])
|
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 21:44:01 +08:00
|
|
|
v8_ctx.eval(js)
|
2013-07-25 09:09:29 +08:00
|
|
|
end
|
2016-02-06 04:49:03 +08:00
|
|
|
|
|
|
|
it "finds moment.js locale file for #{locale[:value]}" do
|
|
|
|
content = JsLocaleHelper.moment_locale(locale[:value])
|
|
|
|
|
2019-05-16 05:43:00 +08:00
|
|
|
if (locale[:value] == SiteSettings::DefaultsProvider::DEFAULT_LOCALE)
|
2016-02-06 04:49:03 +08:00
|
|
|
expect(content).to eq("")
|
|
|
|
else
|
|
|
|
expect(content).to_not eq("")
|
|
|
|
end
|
|
|
|
end
|
2024-07-26 21:44:46 +08:00
|
|
|
|
|
|
|
it "generates valid MF locales for the '#{locale[:value]}' locale" do
|
|
|
|
expect(described_class.output_MF(locale[:value])).not_to match(/Failed to compile/)
|
|
|
|
end
|
2013-07-25 09:09:29 +08:00
|
|
|
end
|
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
describe ".output_MF" do
|
2024-07-25 00:05:17 +08:00
|
|
|
fab!(:overriden_translation_en) do
|
2024-06-18 00:21:04 +08:00
|
|
|
Fabricate(
|
|
|
|
:translation_override,
|
|
|
|
translation_key: "admin_js.admin.user.penalty_history_MF",
|
|
|
|
value: "OVERRIDEN",
|
|
|
|
)
|
2022-02-08 09:31:08 +08:00
|
|
|
end
|
2024-07-25 00:05:17 +08:00
|
|
|
fab!(:overriden_translation_ja) do
|
2024-07-25 22:56:08 +08:00
|
|
|
Fabricate(:translation_override, locale: "ja", translation_key: "js.posts_likes_MF")
|
2024-07-25 00:05:17 +08:00
|
|
|
end
|
2024-10-04 21:21:58 +08:00
|
|
|
fab!(:overriden_translation_zh_tw) do
|
|
|
|
Fabricate(:translation_override, locale: "zh_TW", translation_key: "js.posts_likes_MF")
|
2024-07-25 00:05:17 +08:00
|
|
|
end
|
|
|
|
let(:output) { described_class.output_MF(locale).gsub(/^import.*$/, "") }
|
|
|
|
let(:generated_locales) { v8_ctx.eval("Object.keys(I18n._mfMessages._data)") }
|
|
|
|
let(:translated_message) do
|
|
|
|
v8_ctx.eval("I18n._mfMessages.get('posts_likes_MF', {count: 3, ratio: 'med'})")
|
|
|
|
end
|
2024-10-04 21:21:58 +08:00
|
|
|
let(:fake_logger) { FakeLogger.new }
|
2022-02-08 09:31:08 +08:00
|
|
|
|
2024-07-25 22:56:08 +08:00
|
|
|
before do
|
2024-10-04 21:21:58 +08:00
|
|
|
Rails.logger.broadcast_to(fake_logger)
|
2024-07-25 22:56:08 +08:00
|
|
|
overriden_translation_ja.update_columns(
|
|
|
|
value: "{ count, plural, one {返信 # 件、} other {返信 # 件、} }",
|
|
|
|
)
|
2024-10-04 21:21:58 +08:00
|
|
|
overriden_translation_zh_tw.update_columns(value: "{ count, plural, ")
|
2024-07-25 22:56:08 +08:00
|
|
|
v8_ctx.eval(output)
|
|
|
|
end
|
2024-06-18 00:21:04 +08:00
|
|
|
|
2024-10-04 21:21:58 +08:00
|
|
|
after { Rails.logger.stop_broadcasting_to(fake_logger) }
|
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
context "when locale is 'en'" do
|
2024-10-04 21:21:58 +08:00
|
|
|
let(:locale) { :en }
|
2024-06-18 00:21:04 +08:00
|
|
|
|
|
|
|
it "generates messages for the 'en' locale only" do
|
|
|
|
expect(generated_locales).to eq %w[en]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "translates messages properly" do
|
|
|
|
expect(
|
|
|
|
translated_message,
|
2024-07-11 01:14:36 +08:00
|
|
|
).to eq "3 replies, very high like to post ratio, jump to the first or last post…\n"
|
2024-06-18 00:21:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the translation is overriden" do
|
|
|
|
let(:translated_message) do
|
|
|
|
v8_ctx.eval(
|
|
|
|
"I18n._mfMessages.get('admin.user.penalty_history_MF', { SUSPENDED: 3, SILENCED: 2 })",
|
|
|
|
)
|
|
|
|
end
|
2020-01-16 21:40:53 +08:00
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
it "returns the overriden translation" do
|
|
|
|
expect(translated_message).to eq "OVERRIDEN"
|
|
|
|
end
|
|
|
|
end
|
2022-02-08 09:31:08 +08:00
|
|
|
end
|
|
|
|
|
2024-06-18 00:21:04 +08:00
|
|
|
context "when locale is not 'en'" do
|
2024-10-04 21:21:58 +08:00
|
|
|
let(:locale) { :fr }
|
2024-06-18 00:21:04 +08:00
|
|
|
|
|
|
|
it "generates messages for the current locale and uses 'en' as fallback" do
|
|
|
|
expect(generated_locales).to match(%w[fr en])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "translates messages properly" do
|
|
|
|
expect(
|
|
|
|
translated_message,
|
2024-07-29 21:16:40 +08:00
|
|
|
).to eq "3 réponses, avec un taux très élevé de « J'aime » par publication, accéder à la première ou dernière publication...\n"
|
2024-06-18 00:21:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when a translation is missing" do
|
|
|
|
before { v8_ctx.eval("delete I18n._mfMessages._data.fr.posts_likes_MF") }
|
|
|
|
|
|
|
|
it "returns the fallback translation" do
|
|
|
|
expect(
|
|
|
|
translated_message,
|
2024-07-11 01:14:36 +08:00
|
|
|
).to eq "3 replies, very high like to post ratio, jump to the first or last post…\n"
|
2024-06-18 00:21:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the fallback translation is overriden" do
|
|
|
|
let(:translated_message) do
|
|
|
|
v8_ctx.eval(
|
|
|
|
"I18n._mfMessages.get('admin.user.penalty_history_MF', { SUSPENDED: 3, SILENCED: 2 })",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
v8_ctx.eval("delete I18n._mfMessages._data.fr['admin.user.penalty_history_MF']")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the overriden fallback translation" do
|
|
|
|
expect(translated_message).to eq "OVERRIDEN"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-16 21:40:53 +08:00
|
|
|
end
|
2024-07-25 00:05:17 +08:00
|
|
|
|
|
|
|
context "when locale contains invalid plural keys" do
|
2024-10-04 21:21:58 +08:00
|
|
|
let(:locale) { :ja }
|
2024-07-25 00:05:17 +08:00
|
|
|
|
|
|
|
it "does not raise an error" do
|
|
|
|
expect(generated_locales).to match(%w[ja en])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when locale contains malformed messages" do
|
2024-10-04 21:21:58 +08:00
|
|
|
let(:locale) { :zh_TW }
|
2024-07-25 00:05:17 +08:00
|
|
|
|
|
|
|
it "raises an error" do
|
|
|
|
expect(output).to match(/Failed to compile message formats/)
|
|
|
|
end
|
2024-10-04 21:21:58 +08:00
|
|
|
|
|
|
|
it "logs which keys are problematic" do
|
|
|
|
output
|
|
|
|
expect(fake_logger.errors).to include(/posts_likes_MF/)
|
|
|
|
end
|
2024-07-25 00:05:17 +08:00
|
|
|
end
|
2020-01-16 21:40:53 +08:00
|
|
|
end
|
2024-07-29 15:21:25 +08:00
|
|
|
|
|
|
|
describe ".output_client_overrides" do
|
2024-07-30 09:56:46 +08:00
|
|
|
subject(:client_overrides) { described_class.output_client_overrides("en") }
|
|
|
|
|
|
|
|
before do
|
2024-07-29 15:21:25 +08:00
|
|
|
Fabricate(
|
|
|
|
:translation_override,
|
|
|
|
locale: "en",
|
|
|
|
translation_key: "js.user.preferences.title",
|
|
|
|
value: "SHOULD_SHOW",
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:translation_override,
|
|
|
|
locale: "en",
|
|
|
|
translation_key: "js.user.preferences",
|
|
|
|
value: "SHOULD_NOT_SHOW",
|
|
|
|
status: "deprecated",
|
|
|
|
)
|
2024-07-30 09:56:46 +08:00
|
|
|
end
|
2024-07-29 15:21:25 +08:00
|
|
|
|
2024-07-30 09:56:46 +08:00
|
|
|
it "does not output deprecated translation overrides" do
|
|
|
|
expect(client_overrides).to include("SHOULD_SHOW")
|
|
|
|
expect(client_overrides).not_to include("SHOULD_NOT_SHOW")
|
2024-07-29 15:21:25 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-30 13:53:40 +08:00
|
|
|
end
|