-
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
Showing
24 changed files
with
1,072 additions
and
300 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { readdir, writeFile } from "node:fs/promises"; | ||
import { extract, compile } from "@formatjs/cli-lib"; | ||
import { resolve } from "path"; | ||
import process from "process"; | ||
import url from "url"; | ||
import type { MessageDescriptor } from "@formatjs/cli-lib"; | ||
import { supportedLocales } from "./supportedLocales.ts"; | ||
|
||
const dirname = url.fileURLToPath(new URL(".", import.meta.url)); | ||
const localesFolder = resolve(dirname, "locales"); | ||
const messagesPath = resolve(dirname, "messages.json"); | ||
|
||
async function main() { | ||
try { | ||
await extractMessages(); | ||
await compileLocales(); | ||
} catch (err: unknown) { | ||
console.error(err); // eslint-disable-line no-console | ||
} | ||
} | ||
|
||
async function extractMessages() { | ||
const patternsToExclude = ["/src/api/generated/", "/src/stories/", ".d.ts"]; | ||
|
||
const files = ( | ||
await readdir(resolve(process.cwd(), "src/"), { | ||
recursive: true, | ||
withFileTypes: true, | ||
}) | ||
) | ||
.filter( | ||
entry => | ||
(entry.isFile() && entry.name.endsWith("ts")) || | ||
entry.name.endsWith("tsx"), | ||
) | ||
.map(entry => resolve(entry.path, entry.name)) | ||
.filter(filePath => !patternsToExclude.some(dir => filePath.includes(dir))); | ||
|
||
const resultAsString = await extract(files, { | ||
idInterpolationPattern: "[sha512:contenthash:base64:16]", | ||
flatten: true, | ||
extractSourceLocation: true, | ||
}); | ||
|
||
// we want to omit the attributes start and end as they only describe the | ||
// tokens in the token stream which is most likely irrelevant to the translator | ||
const result = Object.fromEntries( | ||
Object.entries( | ||
JSON.parse(resultAsString) as Record<string, MessageDescriptor>, | ||
).map<[string, MessageDescriptor]>( | ||
([k, { start: _ignoreStart, end: _ignoreEnd, ...rest }]) => [k, rest], | ||
), | ||
); | ||
|
||
await writeFile(messagesPath, JSON.stringify(result, undefined, 4), { | ||
encoding: "utf8", | ||
}); | ||
} | ||
|
||
async function compileLocales() { | ||
const localeAsString = await compile([messagesPath]); | ||
|
||
for (const locale of supportedLocales) { | ||
await writeFile(resolve(localesFolder, `${locale}.json`), localeAsString, { | ||
encoding: "utf8", | ||
}); | ||
} | ||
} | ||
|
||
void main(); |
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 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": "Title" | ||
} |
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 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": "Title" | ||
} |
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 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": "Title" | ||
} |
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,9 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": { | ||
"col": 8, | ||
"defaultMessage": "Title", | ||
"description": "A title for the test page", | ||
"file": "/home/ehsan/repos/sparse.tech/react-ts-webpack-template/src/pages/test.tsx", | ||
"line": 29 | ||
} | ||
} |
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 @@ | ||
export const supportedLocales = ["en", "de", "fa"] as const; |
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 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": "Diese Seite wurde zu Testzwecken erstellt" | ||
} |
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 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": "This page has been created for testing purposes" | ||
} |
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 @@ | ||
{ | ||
"NNQFHO0+LVD2AC/L": "این صفحه برای اهداف آزمایشی ساخته شده است" | ||
} |
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.