-
Notifications
You must be signed in to change notification settings - Fork 219
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
Showing
5 changed files
with
165 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { join } from "node:path"; | ||
import { rootDir } from "../root.js"; | ||
|
||
const outputDir = join(rootDir, "packages", "keybr-intl"); | ||
|
||
export function translationsPath(locale) { | ||
return join(outputDir, `translations`, `${locale}.json`); | ||
} | ||
|
||
export function mergedTranslationsPath(locale) { | ||
return join(outputDir, `translations`, `${locale}-merged.json`); | ||
} | ||
|
||
export function messagesPath(locale) { | ||
return join(outputDir, `lib`, `messages`, `${locale}.json`); | ||
} |
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,38 @@ | ||
export const defaultLocale = "en"; | ||
|
||
export const allLocales = [ | ||
defaultLocale, | ||
"ar", | ||
"bg", | ||
"ca", | ||
"cs", | ||
"da", | ||
"de", | ||
"el", | ||
"eo", | ||
"es", | ||
"et", | ||
"fa", | ||
"fr", | ||
"he", | ||
"hr", | ||
"hu", | ||
"id", | ||
"it", | ||
"ja", | ||
"ko", | ||
"ne", | ||
"nl", | ||
"pl", | ||
"pt-br", | ||
"pt-pt", | ||
"ro", | ||
"ru", | ||
"sv", | ||
"th", | ||
"tr", | ||
"uk", | ||
"vi", | ||
"zh-hans", | ||
"zh-hant", | ||
]; |
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,66 @@ | ||
import { join } from "node:path"; | ||
import { config } from "dotenv"; | ||
import { readJsonSync, writeJsonSync } from "./lib/fs-json.js"; | ||
import { translationsPath } from "./lib/intl-io.js"; | ||
import { allLocales, defaultLocale } from "./locale.js"; | ||
import { rootDir } from "./root.js"; | ||
|
||
config({ path: join(rootDir, ".env") }); | ||
|
||
for (const locale of allLocales) { | ||
if (locale === defaultLocale) { | ||
continue; | ||
} | ||
await pullTranslations(locale); | ||
} | ||
|
||
async function pullTranslations(locale) { | ||
const defaultTranslationsFile = translationsPath(defaultLocale); | ||
const defaultTranslations = readJsonSync(defaultTranslationsFile); | ||
const translationsFile = translationsPath(locale); | ||
const translations = await downloadTranslations(locale); | ||
writeJsonSync( | ||
translationsFile, | ||
remap(defaultTranslations, ([id, message]) => [ | ||
id, | ||
translations[id] && | ||
translations[id] !== id && | ||
translations[id] !== message | ||
? translations[id] | ||
: undefined, | ||
]), | ||
); | ||
} | ||
|
||
async function downloadTranslations(locale) { | ||
console.log(`Downloading translations ${locale}`); | ||
if (locale === "pt-pt") { | ||
locale = "pt"; | ||
} | ||
const body = new FormData(); | ||
body.append("api_token", process.env["POEDITOR_TOKEN"]); | ||
body.append("id", process.env["POEDITOR_PROJECT"]); | ||
body.append("language", locale); | ||
body.append("type", "key_value_json"); | ||
body.append("filters", "translated"); | ||
const res = await fetch("https://api.poeditor.com/v2/projects/export", { | ||
method: "POST", | ||
body: body, | ||
}); | ||
if (!res.ok) { | ||
throw new Error(res.statusText); | ||
} | ||
return await fetchJson((await res.json()).result.url); | ||
} | ||
|
||
async function fetchJson(url) { | ||
const res = await fetch(url); | ||
if (!res.ok) { | ||
throw new Error(res.statusText); | ||
} | ||
return await res.json(); | ||
} | ||
|
||
function remap(entries, callback) { | ||
return Object.fromEntries(Object.entries(entries).map(callback)); | ||
} |
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