-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Giacomo Materozzi
committed
Mar 5, 2024
1 parent
beacf3b
commit c61373b
Showing
16 changed files
with
286 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import fs from "fs" | ||
import path from "path" | ||
import util from "util" | ||
|
||
import { languagesSupported } from "./index" | ||
|
||
require("dotenv").config() | ||
|
||
const pWriteFile = util.promisify(fs.writeFile) | ||
|
||
const saveFileToFileSystem = async (fileUrl, filePath) => { | ||
try { | ||
const response = await fetch(fileUrl) | ||
if (!response.ok) { | ||
throw new Error("Failed to download file") | ||
} | ||
|
||
const json = await response.text() | ||
await pWriteFile(filePath, JSON.stringify(JSON.parse(json), null, 4), { | ||
encoding: "utf-8", | ||
}) | ||
} catch (error) { | ||
fs.unlink(filePath, () => {}) // Delete the file if an error occurs | ||
} | ||
} | ||
|
||
/** | ||
* Main entry points that executes all the functions | ||
*/ | ||
const doWork = async () => { | ||
await Promise.all( | ||
languagesSupported.map((lang) => { | ||
const root = path.join(__dirname, "..") | ||
const filePath = path.join(root, "i18n", "languages", `${lang}.json`) | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`prepare ${lang}...`) | ||
|
||
return saveFileToFileSystem( | ||
`${process.env.TRANSLO_I18N_BASE_URL}/${lang}.json`, | ||
filePath | ||
) | ||
}) | ||
) | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("all languages imported.") | ||
} | ||
|
||
doWork() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import i18next from "i18next" | ||
|
||
import translationEN from "./languages/en.json" | ||
import translationES from "./languages/es.json" | ||
import translationIT from "./languages/it.json" | ||
|
||
export const languagesSupported = ["en", "es", "it"] | ||
|
||
export const defaultLanguage = "en" | ||
|
||
i18next.init({ | ||
resources: { | ||
en: { | ||
translation: translationEN, | ||
}, | ||
es: { | ||
translation: translationES, | ||
}, | ||
it: { | ||
translation: translationIT, | ||
}, | ||
}, | ||
lng: defaultLanguage, | ||
debug: false, | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
nsSeparator: false, | ||
keySeparator: false, | ||
fallbackLng: false, | ||
}) | ||
|
||
export type I18nKey = keyof typeof translationEN | ||
|
||
export default { | ||
changeLanguage: (lang: string) => { | ||
if (!languagesSupported.includes(lang)) { | ||
throw "Language not found" | ||
} | ||
|
||
i18next.changeLanguage(lang) | ||
}, | ||
t: (key: I18nKey) => i18next.t(key), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app.Get started": "Get started" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app.Get started": "Comienza ahora" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app.Get started": "Inizia ora" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.