From 3bff13492f53105baebba048bd20a3b3621a801a Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 26 Nov 2020 16:45:30 +1100 Subject: [PATCH] Allow wizard i18n to be overridden by wizard theme i18n --- .../components/custom-user-selector.js.es6 | 4 +-- .../components/wizard-composer-editor.js.es6 | 3 +- .../components/wizard-field-upload.js.es6 | 4 +-- .../components/wizard-text-field.js.es6 | 4 +-- .../wizard/helpers/wizard-i18n.js.es6 | 6 ++++ .../javascripts/wizard/lib/wizard-i18n.js.es6 | 31 +++++++++++++++++++ .../wizard/routes/custom-step.js.es6 | 4 +-- .../components/wizard-composer-editor.hbs | 2 +- .../components/wizard-composer-hyperlink.hbs | 6 ++-- .../templates/components/wizard-editor.hbs | 2 +- .../components/wizard-field-composer.hbs | 2 +- .../components/wizard-field-upload.hbs | 4 +-- .../templates/components/wizard-no-access.hbs | 2 +- .../templates/components/wizard-step.hbs | 10 +++--- .../wizard/templates/custom.index.hbs | 8 ++--- .../wizard/templates/custom.step.hbs | 2 +- views/layouts/wizard.html.erb | 1 + 17 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 assets/javascripts/wizard/helpers/wizard-i18n.js.es6 create mode 100644 assets/javascripts/wizard/lib/wizard-i18n.js.es6 diff --git a/assets/javascripts/wizard/components/custom-user-selector.js.es6 b/assets/javascripts/wizard/components/custom-user-selector.js.es6 index dfc45604..6ca5698d 100644 --- a/assets/javascripts/wizard/components/custom-user-selector.js.es6 +++ b/assets/javascripts/wizard/components/custom-user-selector.js.es6 @@ -1,7 +1,7 @@ import { default as computed, observes } from 'discourse-common/utils/decorators'; import { renderAvatar } from 'discourse/helpers/user-avatar'; import userSearch from '../lib/user-search'; -import I18n from "I18n"; +import WizardI18n from "../lib/wizard-i18n"; const template = function(params) { const options = params.options; @@ -35,7 +35,7 @@ export default Ember.TextField.extend({ @computed("placeholderKey") placeholder(placeholderKey) { - return placeholderKey ? I18n.t(placeholderKey) : ""; + return placeholderKey ? WizardI18n(placeholderKey) : ""; }, @observes('usernames') diff --git a/assets/javascripts/wizard/components/wizard-composer-editor.js.es6 b/assets/javascripts/wizard/components/wizard-composer-editor.js.es6 index 2da0e4a6..3689588a 100644 --- a/assets/javascripts/wizard/components/wizard-composer-editor.js.es6 +++ b/assets/javascripts/wizard/components/wizard-composer-editor.js.es6 @@ -20,6 +20,7 @@ import { import { cacheShortUploadUrl } from "pretty-text/upload-short-url"; import { alias } from "@ember/object/computed"; import { uploadIcon } from "discourse/lib/uploads"; +import WizardI18n from '../lib/wizard-i18n'; const uploadMarkdownResolvers = []; @@ -103,7 +104,7 @@ export default ComposerEditor.extend({ // Limit the number of simultaneous uploads if (max > 0 && data.files.length > max) { bootbox.alert( - I18n.t("post.errors.too_many_dragged_and_dropped_files", { max }) + WizardI18n("post.errors.too_many_dragged_and_dropped_files", { max }) ); return false; } diff --git a/assets/javascripts/wizard/components/wizard-field-upload.js.es6 b/assets/javascripts/wizard/components/wizard-field-upload.js.es6 index 2241c940..5eefce9a 100644 --- a/assets/javascripts/wizard/components/wizard-field-upload.js.es6 +++ b/assets/javascripts/wizard/components/wizard-field-upload.js.es6 @@ -1,6 +1,6 @@ import getUrl from "discourse-common/lib/get-url"; import { getToken } from "wizard/lib/ajax"; -import I18n from "I18n"; +import WizardI18n from "../lib/wizard-i18n"; export default Ember.Component.extend({ classNames: ["wizard-field-upload"], @@ -41,7 +41,7 @@ export default Ember.Component.extend({ }); $upload.on("fileuploadfail", (e, response) => { - let message = I18n.t("wizard.upload_error"); + let message = WizardI18n("wizard.upload_error"); if (response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) { message = response.jqXHR.responseJSON.errors.join("\n"); } diff --git a/assets/javascripts/wizard/components/wizard-text-field.js.es6 b/assets/javascripts/wizard/components/wizard-text-field.js.es6 index 8e4570a7..a05b84e6 100644 --- a/assets/javascripts/wizard/components/wizard-text-field.js.es6 +++ b/assets/javascripts/wizard/components/wizard-text-field.js.es6 @@ -2,7 +2,7 @@ import computed from "discourse-common/utils/decorators"; import { siteDir, isRTL, isLTR } from "discourse/lib/text-direction"; -import I18n from "I18n"; +import WizardI18n from "../lib/wizard-i18n"; export default Ember.TextField.extend({ attributeBindings: ['autocorrect', 'autocapitalize', 'autofocus', 'maxLength', 'dir'], @@ -34,6 +34,6 @@ export default Ember.TextField.extend({ @computed("placeholderKey") placeholder(placeholderKey) { - return placeholderKey ? I18n.t(placeholderKey) : ""; + return placeholderKey ? WizardI18n(placeholderKey) : ""; } }); diff --git a/assets/javascripts/wizard/helpers/wizard-i18n.js.es6 b/assets/javascripts/wizard/helpers/wizard-i18n.js.es6 new file mode 100644 index 00000000..f357cc5b --- /dev/null +++ b/assets/javascripts/wizard/helpers/wizard-i18n.js.es6 @@ -0,0 +1,6 @@ +import { registerUnbound } from 'discourse-common/lib/helpers'; +import WizardI18n from '../lib/wizard-i18n'; + +export default registerUnbound("wizard-i18n", (key, params) => { + return WizardI18n(key, params); +}); \ No newline at end of file diff --git a/assets/javascripts/wizard/lib/wizard-i18n.js.es6 b/assets/javascripts/wizard/lib/wizard-i18n.js.es6 new file mode 100644 index 00000000..4d78bc7e --- /dev/null +++ b/assets/javascripts/wizard/lib/wizard-i18n.js.es6 @@ -0,0 +1,31 @@ +import I18n from "I18n"; + +const getThemeId = () => { + let themeId = parseInt($("meta[name=discourse_theme_ids]")[0].content, 10); + + if (!isNaN(themeId)) { + return themeId.toString(); + } else { + return null; + } +} + +const translationExists = (key) => { + return I18n.findTranslation(key, { locale: I18n.locale }) || + I18n.findTranslation(key, { locale: I18n.defaultLocale }); +} + +const WizardI18n = (key, params={}) => { + const themeId = getThemeId(); + if (!themeId) return I18n.t(key, params); + + const themeKey = `theme_translations.${themeId}.${key}`; + + if (translationExists(themeKey)) { + return I18n.t(themeKey, params); + } else { + return I18n.t(key, params); + } +} + +export default WizardI18n; \ No newline at end of file diff --git a/assets/javascripts/wizard/routes/custom-step.js.es6 b/assets/javascripts/wizard/routes/custom-step.js.es6 index 72dd5263..627da6a1 100644 --- a/assets/javascripts/wizard/routes/custom-step.js.es6 +++ b/assets/javascripts/wizard/routes/custom-step.js.es6 @@ -1,4 +1,4 @@ -import I18n from "I18n"; +import WizardI18n from "../lib/wizard-i18n"; export default Ember.Route.extend({ model(params) { @@ -26,7 +26,7 @@ export default Ember.Route.extend({ if (!model.permitted) { props['stepMessage'] = { state: 'not-permitted', - text: model.permitted_message || I18n.t('wizard.step_not_permitted') + text: model.permitted_message || WizardI18n('wizard.step_not_permitted') }; if (model.index > 0) { props['showReset'] = true; diff --git a/assets/javascripts/wizard/templates/components/wizard-composer-editor.hbs b/assets/javascripts/wizard/templates/components/wizard-composer-editor.hbs index 5e6cdb7f..28c697ce 100644 --- a/assets/javascripts/wizard/templates/components/wizard-composer-editor.hbs +++ b/assets/javascripts/wizard/templates/components/wizard-composer-editor.hbs @@ -31,7 +31,7 @@ {{#if isUploading}}
- {{loading-spinner size="small"}}{{i18n "upload_selector.uploading"}} {{uploadProgress}}% + {{loading-spinner size="small"}}{{wizard-i18n "upload_selector.uploading"}} {{uploadProgress}}% {{#if isCancellable}} {{d-icon "times"}} {{/if}} diff --git a/assets/javascripts/wizard/templates/components/wizard-composer-hyperlink.hbs b/assets/javascripts/wizard/templates/components/wizard-composer-hyperlink.hbs index 44a2495c..f5b7afb5 100644 --- a/assets/javascripts/wizard/templates/components/wizard-composer-hyperlink.hbs +++ b/assets/javascripts/wizard/templates/components/wizard-composer-hyperlink.hbs @@ -1,13 +1,13 @@
@@ -34,23 +34,23 @@ {{loading-spinner size='small'}} {{else}} {{#if showQuitButton}} - {{i18n "wizard.quit"}} + {{wizard-i18n "wizard.quit"}} {{/if}} {{#if showBackButton}} - {{i18n "wizard.back"}} + {{wizard-i18n "wizard.back"}} {{/if}} {{/if}} {{#if showNextButton}} {{/if}} {{#if showDoneButton}} {{/if}}
diff --git a/assets/javascripts/wizard/templates/custom.index.hbs b/assets/javascripts/wizard/templates/custom.index.hbs index 6040d9b5..d233f45b 100644 --- a/assets/javascripts/wizard/templates/custom.index.hbs +++ b/assets/javascripts/wizard/templates/custom.index.hbs @@ -1,14 +1,14 @@ {{#if noWizard}} - {{wizard-no-access text=(i18n 'wizard.none') wizardId=wizardId}} + {{wizard-no-access text=(wizard-i18n 'wizard.none') wizardId=wizardId}} {{else}} {{#if requiresLogin}} - {{wizard-no-access text=(i18n 'wizard.requires_login') wizardId=wizardId}} + {{wizard-no-access text=(wizard-i18n 'wizard.requires_login') wizardId=wizardId}} {{else}} {{#if notPermitted}} - {{wizard-no-access text=(i18n 'wizard.not_permitted') wizardId=wizardId}} + {{wizard-no-access text=(wizard-i18n 'wizard.not_permitted') wizardId=wizardId}} {{else}} {{#if completed}} - {{wizard-no-access text=(i18n 'wizard.completed') wizardId=wizardId}} + {{wizard-no-access text=(wizard-i18n 'wizard.completed') wizardId=wizardId}} {{/if}} {{/if}} {{/if}} diff --git a/assets/javascripts/wizard/templates/custom.step.hbs b/assets/javascripts/wizard/templates/custom.step.hbs index 3c26bf5c..039c8099 100644 --- a/assets/javascripts/wizard/templates/custom.step.hbs +++ b/assets/javascripts/wizard/templates/custom.step.hbs @@ -5,7 +5,7 @@
{{#if showReset}} - {{i18n 'wizard.reset'}} + {{wizard-i18n 'wizard.reset'}} {{/if}} diff --git a/views/layouts/wizard.html.erb b/views/layouts/wizard.html.erb index feaa041b..ea6d6f2a 100644 --- a/views/layouts/wizard.html.erb +++ b/views/layouts/wizard.html.erb @@ -19,6 +19,7 @@ <%= csrf_meta_tags %> <%- unless customization_disabled? %> + <%= theme_translations_lookup %> <%= raw theme_lookup("head_tag") %> <%- end %>