Skip to content

Commit

Permalink
Move the translation information into i18n.js
Browse files Browse the repository at this point in the history
This will an initialization problem. Sometimes the translation has wasn't initialized, but the translation for a given was requested, that lead to untranslated string in the UI.
  • Loading branch information
sascha-karnatz committed Sep 29, 2023
1 parent decb42d commit da563a8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 19 deletions.
2 changes: 0 additions & 2 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Buttons from "alchemy_admin/buttons"
import GUI from "alchemy_admin/gui"
import { translate } from "alchemy_admin/i18n"
import Dirty from "alchemy_admin/dirty"
import translationData from "alchemy_admin/translations"
import fileEditors from "alchemy_admin/file_editors"
import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link"
import pictureEditors from "alchemy_admin/picture_editors"
Expand Down Expand Up @@ -41,7 +40,6 @@ Object.assign(Alchemy, {
...Dirty,
GUI,
t: translate, // Global utility method for translating a given string
translations: Object.assign(Alchemy.translations || {}, translationData),
fileEditors,
pictureEditors,
ImageLoader: ImageLoader.init,
Expand Down
7 changes: 7 additions & 0 deletions app/javascript/alchemy_admin/i18n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import translationData from "alchemy_admin/translations"

Alchemy.translations = Object.assign(
Alchemy.translations || {},
translationData
)

const KEY_SEPARATOR = /\./

function getTranslations() {
Expand Down
3 changes: 0 additions & 3 deletions spec/javascript/alchemy_admin/components/component.helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import translations from "alchemy_admin/translations"

/**
* render component helper
* @param {string} name
Expand All @@ -13,5 +11,4 @@ export const renderComponent = (name, html) => {

export const setupLanguage = () => {
document.documentElement.lang = "en"
Alchemy.translations = translations
}
14 changes: 0 additions & 14 deletions spec/javascript/alchemy_admin/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,12 @@ describe("i18n", () => {
})

describe("if translation is present", () => {
beforeEach(() => {
Alchemy.translations = { en: { help: "Help" } }
})

it("Returns translated string", () => {
expect(translate("help")).toEqual("Help")
})

describe("if key includes a period", () => {
describe("that is translated", () => {
beforeEach(() => {
Alchemy.translations = { en: { formats: { date: "Y-m-d" } } }
})

it("splits into group", () => {
expect(translate("formats.date")).toEqual("Y-m-d")
})
Expand All @@ -75,12 +67,6 @@ describe("i18n", () => {
})

describe("if replacement is given", () => {
beforeEach(() => {
Alchemy.translations = {
en: { allowed_chars: "of %{number} chars" }
}
})

it("replaces it", () => {
expect(translate("allowed_chars", 5)).toEqual("of 5 chars")
})
Expand Down

0 comments on commit da563a8

Please sign in to comment.