Skip to content
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: create single output json instead of individual label files #3

Merged
merged 14 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/late-carrots-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"@labdigital/intl-extractor": major
---

Output labels to a single json file

## What changed
Instead of creating individual `<File>.labels.json` files for each TypeScript file it will now output a single JSON file containing all the namespaces and labels.
It conforms to how `next-intl` works and removes the inbetween step since we can automate with this tool.

Next to this the argument for passing directories has changed from `--source` to `--input` as it's confusing with `source.json` otherwise.

## Why
Individual `*.labels.json` files were helpful to colocate labels and merge them to the single required JSON file. But now that we have this extractor we can just check all usages of `useTranslations` and `getTranslations` and merge them directly.

Also the original implementation took file names as input instead of the used namespaces which led to unused labels and no support for multiple namespaces in one file.

## How to update
1. Make sure your output JSON file is completely up to date
2. Update the extractor
3. Use `--input` instead of `--source` for input files
4. Run extractor
5. Remove all `*.labels.json` files from your project :)


4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/*.js
**/*.js
examples/
.eslintrc.cjs
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pnpm-lock.yaml
.changeset/
dist/
examples/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# intl-extractor

Extracts `next-intl` labels from TypeScript files and merges them to a given output json file.

This automates away manually setting labels in a `source.json` file.

## How to use

```bash
npx @labdigital/intl-extractor -d ./path/to/files -o ./path/to/output.json
```
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Examples

Few example use cases to try out changes, use tests if you want to properly test things.
31 changes: 31 additions & 0 deletions examples/client.tsx
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()
}
1 change: 1 addition & 0 deletions examples/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oepfkopweopfkewpo invalid json
14 changes: 14 additions & 0 deletions examples/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"ProductListing": {
"foobar": "foobar",
"title": "title cool",
"Second": {
"results": "results cool"
}
},
"MyComponent": {
"foobar": "foobar mooi geupdate",
"title": "title",
"foodiebar": "foodiebar hallo"
}
}
22 changes: 22 additions & 0 deletions examples/server.tsx
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>
);
};
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Lab Digital",
"type": "module",
"bin": {
"intl-extractor": "./dist/cli.cjs"
"intl-extractor": "./dist/bin/cli.js"
},
"scripts": {
"build": "tsup",
Expand All @@ -17,21 +17,24 @@
"test:ci": "vitest run --coverage"
},
"dependencies": {
"glob": "^10.3.12",
"glob": "^10.4.2",
"yargs": "^17.7.2"
},
"peerDependencies": {
"typescript": ">=5.5"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@types/node": "^20.12.7",
"@changesets/cli": "^2.27.6",
"@types/node": "^20.14.9",
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@vitest/coverage-v8": "^1.5.0",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "^8.57.0",
"eslint-plugin-sort-class-members": "^1.20.0",
"eslint-plugin-unused-imports": "^3.1.0",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"vitest": "^1.5.0"
"eslint-plugin-unused-imports": "^4.0.0",
"tsup": "^8.1.0",
"typescript": "^5.5.2",
"vitest": "^1.6.0"
},
"files": [
"dist",
Expand Down
Loading