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 d44a7c3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 51 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
4 changes: 4 additions & 0 deletions app/javascript/alchemy_admin/i18n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { en } from "alchemy_admin/locales/en"

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

const KEY_SEPARATOR = /\./

function getTranslations() {
Expand Down
28 changes: 28 additions & 0 deletions app/javascript/alchemy_admin/locales/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const en = {
allowed_chars: "of %{count} chars",
cancel: "Cancel",
cancelled: "Cancelled",
click_to_edit: "click to edit",
complete: "Complete",
element_dirty_notice:
"This element has unsaved changes. Do you really want to fold it?",
help: "Help",
ok: "Ok",
page_dirty_notice:
"You have unsaved changes on this page. They will be lost if you continue.",
page_found: "Page found",
pages_found: "Pages found",
url_validation_failed: "The url has no valid format.",
warning: "Warning!",
"File is too large": "File is too large",
"File is too small": "File is too small",
"File type not allowed": "File type not allowed",
"Maximum number of files exceeded": "Maximum number of files exceeded.",
"Uploaded bytes exceed file size": "Uploaded bytes exceed file size",
formats: {
datetime: "Y-m-d H:i",
date: "Y-m-d",
time: "H:i",
time_24hr: false
}
}
32 changes: 0 additions & 32 deletions app/javascript/alchemy_admin/translations.js

This file was deleted.

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 d44a7c3

Please sign in to comment.