-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: integrate i18n infrastructure #11
Open
ehsanmmd
wants to merge
2
commits into
main
Choose a base branch
from
feat/integrate-i18n-infrastructure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a comment and doesn't belong in a json file (same applies to yumlet where we have introduced it).