Skip to content

Commit

Permalink
Fix translations with - and warn about missing translations in the co…
Browse files Browse the repository at this point in the history
…nsole (#385)
  • Loading branch information
ludeeus authored Feb 14, 2021
1 parent afabe55 commit 6ff9291
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion script/gulp/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require("fs-extra");
const del = require("del");
const log = require("fancy-log")

const changeLang = {"et_EE": "et"}
const changeLang = {"et_EE": "et", "zh-Hans": "zh_Hans", "pt-BR": "pt_BR"}

gulp.task("generate-translations", async function (task) {
del.sync("./src/localize/generated.ts");
Expand Down
25 changes: 25 additions & 0 deletions src/localize/localize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { languages } from "./generated";

const warnings = { language: [], sting: {} };

export function localize(
language: string,
string: string,
Expand All @@ -14,12 +16,35 @@ export function localize(
.replace(/['"]+/g, "")
.replace("-", "_");

if (!languages[lang] && !warnings.language.includes(lang)) {
warnings.language.push(lang);
console.warn(
`Language '${lang.replace(
"_",
"-"
)}' is not added to HACS. https://hacs.xyz/docs/developer/translation`
);
}

try {
translated = languages[lang];
split.forEach((section) => {
translated = translated[section];
});
} catch (e) {
if (languages[lang] && !warnings.sting[lang]) {
warnings.sting[lang] = [];
}
if (languages[lang] && !warnings.sting[lang].includes(string)) {
warnings.sting[lang].push(string);
console.warn(
`Translation string '${string}' for '${lang.replace(
"_",
"-"
)}' is not added to HACS. https://hacs.xyz/docs/developer/translation`
);
}

translated = languages["en"];
split.forEach((section) => {
translated = translated[section];
Expand Down

0 comments on commit 6ff9291

Please sign in to comment.