-
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.
feat: create single output file out of input
Instead of creating separate labels.json files for each component, this will merge them together to one output.json file with the correct namespaces intact.
- Loading branch information
Showing
11 changed files
with
295 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"foobar": "foobar", | ||
"title": "title", | ||
"foodiebar": "foodiebar" | ||
} |
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,31 @@ | ||
"use client"; | ||
|
||
import { useTranslations } from "next-intl"; | ||
|
||
|
||
export const MyComponent = () => { | ||
const t = useTranslations("MyComponent"); | ||
|
||
const foobar = t("foobar"); | ||
|
||
return ( | ||
<div> | ||
<h1>{t("title")}</h1> | ||
</div> | ||
) | ||
} | ||
|
||
// Nested scope | ||
export function MyOtherComponent = () => { | ||
const t = useTranslations("MyComponent"); | ||
|
||
const content () => { | ||
const foobar = t("foodiebar"); | ||
return ( | ||
<div> | ||
<h1>{t("title")}</h1> | ||
</div> | ||
) | ||
} | ||
return content() | ||
} |
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 @@ | ||
{ | ||
"Test": { | ||
"Test": "Test" | ||
}, | ||
"ProductListing": { | ||
"foobar2": "foobar2", | ||
"foobar": "Original text that should not be removed." | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"foobar": "foobar", | ||
"title": "title", | ||
"results": "results" | ||
} |
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,22 @@ | ||
import { getTranslations } from "next-intl/server"; | ||
|
||
export const MyComponent = async () => { | ||
const t = await getTranslations({ namespace: "ProductListing", locale }); | ||
const t2 = await getTranslations({ | ||
namespace: "ProductListing.Second", | ||
locale, | ||
}); | ||
|
||
const foobar = t("foobar"); | ||
|
||
return ( | ||
<div> | ||
<h1>{t("title")}</h1> | ||
<div> | ||
{t2("results", { | ||
total: products.total, | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; |
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,51 @@ | ||
import yargs from "yargs"; | ||
import { hideBin } from "yargs/helpers"; | ||
import { writeTranslations } from "../write"; | ||
|
||
// Setup yargs to use the command modules from the commands directory | ||
|
||
async function main() { | ||
yargs(hideBin(process.argv)) | ||
.usage("$0 --source [path] --output [json file]") | ||
.options({ | ||
source: { | ||
type: "array", | ||
alias: "s", | ||
describe: "Source directories to process", | ||
demandOption: true, // Require at least one source path | ||
coerce: (arg: string | string[]) => { | ||
// Ensure that the input is always an array of strings | ||
if (typeof arg === "string") { | ||
return [arg]; | ||
} | ||
return arg; | ||
}, | ||
}, | ||
output: { | ||
type: "string", | ||
alias: "o", | ||
describe: "Output file", | ||
demandOption: true, | ||
}, | ||
}) | ||
.command( | ||
"$0", | ||
"Default command", | ||
() => {}, | ||
async (argv) => { | ||
for (const source of argv.source) { | ||
await writeTranslations(source, argv.output); | ||
} | ||
// Process the source directories | ||
} | ||
) | ||
.help() | ||
.alias("help", "h") | ||
.parse(); | ||
} | ||
|
||
// Run the application | ||
main().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
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,112 @@ | ||
type WriteCacheObject = { [key: string]: WriteCacheObject | string }; | ||
|
||
import * as fs from "fs"; | ||
import * as glob from "glob"; | ||
import { findTranslationsUsage } from "./parse-new"; | ||
|
||
export async function writeTranslations( | ||
rootPath: string, | ||
output: string | ||
): Promise<void> { | ||
const cache: WriteCacheObject = {}; | ||
const pattern = "**/*.{ts,tsx}"; | ||
|
||
const options = { | ||
cwd: rootPath, | ||
absolute: true, | ||
}; | ||
|
||
const files = glob.sync(pattern, options); | ||
for (const file of files) { | ||
const data = await findTranslationsUsage(file); | ||
updateCache(cache, data); | ||
} | ||
|
||
updateOutputFile(output, cache); | ||
} | ||
|
||
function updateCache( | ||
cache: WriteCacheObject, | ||
data: Record<string, Set<string>> | ||
) { | ||
for (const key of Object.keys(data)) { | ||
const keys = key.split("."); | ||
let currentCache = cache; | ||
for (let i = 0; i < keys.length; i++) { | ||
const currentKey = keys[i]; | ||
if (i === keys.length - 1) { | ||
if (currentCache[currentKey] === undefined) { | ||
currentCache[currentKey] = {}; | ||
} | ||
|
||
for (const value of data[key]) { | ||
currentCache[currentKey][value] = value; | ||
} | ||
} else { | ||
if (currentCache[currentKey] === undefined) { | ||
currentCache[currentKey] = {}; | ||
} | ||
|
||
currentCache = currentCache[currentKey]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function updateOutputFile(file: string, cache: WriteCacheObject) { | ||
// Compare the output file with the cache | ||
let existingData = {}; | ||
|
||
if (fs.existsSync(file)) { | ||
// Read the existing data | ||
const fileContent = await fs.promises.readFile(file, "utf8"); | ||
existingData = JSON.parse(fileContent); | ||
} | ||
|
||
// Recursively delete keys from existing data if they don't exist | ||
// in the cache | ||
removeKeysFromObject(existingData, cache); | ||
|
||
// Recursively copy new items | ||
copyKeysToObject(existingData, cache); | ||
|
||
console.log(existingData); | ||
} | ||
|
||
function removeKeysFromObject(data: WriteCacheObject, cache: WriteCacheObject) { | ||
const dataKeys = Object.keys(data); | ||
const keys = Object.keys(cache); | ||
for (const key of dataKeys) { | ||
if (!keys.includes(key)) { | ||
// Key doesn't exist in the cache, no need to check further | ||
delete data[key]; | ||
continue; | ||
} | ||
|
||
if (typeof data[key] === "object") { | ||
if (typeof cache[key] !== "object") { | ||
// This is not an object in the cache, delete the object | ||
delete data[key]; | ||
continue; | ||
} else { | ||
// Both are objects, we go deeper | ||
removeKeysFromObject(data[key], cache[key]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function copyKeysToObject(data: WriteCacheObject, cache: WriteCacheObject) { | ||
const keys = Object.keys(cache); | ||
for (const key of keys) { | ||
if (data[key] === undefined) { | ||
// Key doesn't exist in the data, copy it from the cache | ||
data[key] = cache[key]; | ||
} else { | ||
if (typeof data[key] === "object" && typeof cache[key] === "object") { | ||
// Both are objects, recurse into deeper object | ||
copyKeysToObject(data[key], cache[key]); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.