-
Notifications
You must be signed in to change notification settings - Fork 1
add i18n translation hygiene #42
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b8ef0b
add i18n translation hygiene
dnywh a735df8
address copilot i18n comments
dnywh 2a562e5
add agent collaboration guidelines
dnywh da9fc18
agents cleanup
dnywh c7c3e5b
preserve newsletter mdx list spacing
dnywh 25a4a65
copilot
dnywh 8e54154
fix newsletter mdx list spacing
dnywh 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 hidden or 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,32 @@ | ||
| name: Validate App | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| check: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
|
|
||
| steps: | ||
| - name: Check out repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 22 | ||
| cache: npm | ||
|
|
||
| - name: Install dependencies | ||
| run: npm ci | ||
|
|
||
| - name: Run app checks | ||
| run: npm run check | ||
|
|
||
| - name: Build app | ||
| run: npm run build | ||
This file contains hidden or 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 @@ | ||
| # AGENTS.md | ||
|
|
||
| These instructions apply to the whole repository. | ||
|
|
||
| ## Language | ||
|
|
||
| - Use Australian/British English over US English. | ||
|
|
||
| ## Frontend | ||
|
|
||
| - Prefer Server Components by default. Only add `"use client"` when the component needs browser-only APIs, state/effects, event handlers, or client-only libraries. | ||
| - When touching JS/JSX components, convert them to TS/TSX when it is reasonable and scoped to the change. | ||
| - Keep shared presentational components server-safe where possible. For translated labels or suffixes, prefer passing translated text from the caller instead of adding `useTranslations` to a shared component. | ||
| - In MDX prose, if an inline component inside a Markdown list item is formatted onto multiple lines and changes rendered spacing, use a targeted `{/* prettier-ignore */}` before that list rather than disabling formatting for the whole file. | ||
|
|
||
| ## Internationalisation | ||
|
|
||
| - Put user-facing app UI copy in `next-intl` message files under `messages/`. | ||
| - Treat `messages/en.json` as the source catalogue. Keep Spanish (`es`) and German (`de`) complete whenever adding or changing message keys. | ||
| - Run `npm run i18n:check` after editing messages. Run `npm run check` before handing work back. | ||
| - Do not put dynamic user-generated content, internal enum values, table names, route constants, CSS strings, logs, or analytics identifiers into message files. | ||
| - For listing types and other enums, pass stable keys such as `business`, `community`, or `residential` and handle display grammar in translations, usually with ICU `select`. |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,266 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import process from "node:process"; | ||
|
|
||
| const rootDir = process.cwd(); | ||
| const configPath = path.join(rootDir, "src/i18n/config.ts"); | ||
| const messagesDir = path.join(rootDir, "messages"); | ||
|
|
||
| function readConfig() { | ||
| const source = fs.readFileSync(configPath, "utf8"); | ||
| const localesMatch = source.match(/locales\s*=\s*(\[[^\]]+\])\s*as const/); | ||
| const defaultLocaleMatch = source.match( | ||
| /defaultLocale:\s*Locale\s*=\s*["']([^"']+)["']/ | ||
| ); | ||
|
|
||
| if (!localesMatch) { | ||
| throw new Error(`Could not find the locales array in ${configPath}`); | ||
| } | ||
|
|
||
| const locales = JSON.parse(localesMatch[1].replaceAll("'", '"')); | ||
| const defaultLocale = defaultLocaleMatch?.[1] ?? locales[0]; | ||
|
|
||
| if (!locales.includes(defaultLocale)) { | ||
| throw new Error( | ||
| `Default locale "${defaultLocale}" is not listed in ${configPath}` | ||
| ); | ||
| } | ||
|
|
||
| return { locales, defaultLocale }; | ||
| } | ||
|
|
||
| function readMessages(locale) { | ||
| const filePath = path.join(messagesDir, `${locale}.json`); | ||
|
|
||
| if (!fs.existsSync(filePath)) { | ||
| throw new Error( | ||
| `Missing message file: ${path.relative(rootDir, filePath)}` | ||
| ); | ||
| } | ||
|
|
||
| return JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
| } | ||
|
|
||
| function valueType(value) { | ||
| if (Array.isArray(value)) { | ||
| return "array"; | ||
| } | ||
|
|
||
| if (value === null) { | ||
| return "null"; | ||
| } | ||
|
|
||
| return typeof value; | ||
| } | ||
|
|
||
| function flattenMessages(value, pathParts = [], entries = new Map()) { | ||
| if (value && typeof value === "object" && !Array.isArray(value)) { | ||
| entries.set(pathParts.join("."), { | ||
| type: "object", | ||
| value, | ||
| }); | ||
|
|
||
| for (const [key, childValue] of Object.entries(value)) { | ||
| flattenMessages(childValue, [...pathParts, key], entries); | ||
| } | ||
|
|
||
| return entries; | ||
| } | ||
|
|
||
| entries.set(pathParts.join("."), { | ||
| type: valueType(value), | ||
| value, | ||
| }); | ||
|
|
||
| return entries; | ||
| } | ||
|
|
||
| function leafKeys(entries) { | ||
| return [...entries] | ||
| .filter(([, entry]) => entry.type !== "object") | ||
| .map(([key]) => key) | ||
| .sort(); | ||
| } | ||
|
|
||
| function extractIcuArguments(message) { | ||
| const argumentsSet = new Set(); | ||
|
|
||
| for (let index = 0; index < message.length; index += 1) { | ||
| if (message[index] !== "{") { | ||
| continue; | ||
| } | ||
|
|
||
| const match = message | ||
| .slice(index + 1) | ||
| .match(/^([A-Za-z_][\w]*)\s*(?:[,}])/); | ||
|
|
||
| if (match) { | ||
| argumentsSet.add(match[1]); | ||
| } | ||
|
|
||
| let depth = 1; | ||
| index += 1; | ||
|
|
||
| while (index < message.length && depth > 0) { | ||
| if (message[index] === "{") { | ||
| depth += 1; | ||
| } else if (message[index] === "}") { | ||
| depth -= 1; | ||
| } | ||
|
|
||
| index += 1; | ||
| } | ||
| } | ||
|
|
||
| return [...argumentsSet].sort(); | ||
| } | ||
|
|
||
| function extractRichTextTags(message) { | ||
| const tags = new Set(); | ||
| const tagPattern = /<\/?([A-Za-z][\w]*)\b[^>]*>/g; | ||
|
|
||
| for (const match of message.matchAll(tagPattern)) { | ||
| tags.add(match[1]); | ||
| } | ||
|
|
||
| return [...tags].sort(); | ||
| } | ||
|
|
||
| function listDifference(left, right) { | ||
| const rightSet = new Set(right); | ||
| return left.filter((item) => !rightSet.has(item)); | ||
| } | ||
|
|
||
| function sameList(left, right) { | ||
| return ( | ||
| left.length === right.length && | ||
| left.every((item, index) => item === right[index]) | ||
| ); | ||
| } | ||
|
|
||
| function formatList(items) { | ||
| return items.map((item) => `- ${item}`).join("\n"); | ||
| } | ||
|
|
||
| function compareLocale(locale, baselineLocale, baselineEntries, localeEntries) { | ||
| const problems = []; | ||
| const baselineLeaves = leafKeys(baselineEntries); | ||
| const localeLeaves = leafKeys(localeEntries); | ||
| const missingKeys = listDifference(baselineLeaves, localeLeaves); | ||
| const extraKeys = listDifference(localeLeaves, baselineLeaves); | ||
|
|
||
| if (missingKeys.length > 0) { | ||
| problems.push( | ||
| `messages/${locale}.json is missing keys from messages/${baselineLocale}.json:\n${formatList( | ||
| missingKeys | ||
| )}` | ||
| ); | ||
| } | ||
|
|
||
| if (extraKeys.length > 0) { | ||
| problems.push( | ||
| `messages/${locale}.json has extra keys not found in messages/${baselineLocale}.json:\n${formatList( | ||
| extraKeys | ||
| )}` | ||
| ); | ||
| } | ||
|
|
||
| for (const [key, baselineEntry] of baselineEntries) { | ||
| const localeEntry = localeEntries.get(key); | ||
|
|
||
| if (!localeEntry) { | ||
| continue; | ||
| } | ||
|
|
||
| if (baselineEntry.type !== localeEntry.type) { | ||
| problems.push( | ||
| `messages/${locale}.json has a structural mismatch at "${key}": expected ${baselineEntry.type}, found ${localeEntry.type}` | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| if (localeEntry.type !== "string") { | ||
| continue; | ||
| } | ||
|
|
||
| if (localeEntry.value.trim() === "") { | ||
| problems.push(`messages/${locale}.json has an empty string at "${key}"`); | ||
| continue; | ||
| } | ||
|
|
||
| const baselineArguments = extractIcuArguments(baselineEntry.value); | ||
| const localeArguments = extractIcuArguments(localeEntry.value); | ||
|
|
||
| if (!sameList(baselineArguments, localeArguments)) { | ||
| problems.push( | ||
| `messages/${locale}.json has ICU placeholder mismatch at "${key}": expected {${baselineArguments.join( | ||
| ", " | ||
| )}}, found {${localeArguments.join(", ")}}` | ||
| ); | ||
| } | ||
|
|
||
| const baselineTags = extractRichTextTags(baselineEntry.value); | ||
| const localeTags = extractRichTextTags(localeEntry.value); | ||
|
|
||
| if (!sameList(baselineTags, localeTags)) { | ||
| problems.push( | ||
| `messages/${locale}.json has rich-text tag mismatch at "${key}": expected <${baselineTags.join( | ||
| ">, <" | ||
| )}>, found <${localeTags.join(">, <")}>` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return problems; | ||
| } | ||
|
|
||
| function main() { | ||
| const { locales, defaultLocale } = readConfig(); | ||
| const baselineMessages = readMessages(defaultLocale); | ||
| const baselineEntries = flattenMessages(baselineMessages); | ||
| const problems = []; | ||
|
|
||
| for (const locale of locales) { | ||
| const messages = readMessages(locale); | ||
| const entries = flattenMessages(messages); | ||
|
|
||
| for (const [key, entry] of entries) { | ||
| if (entry.type === "string" && entry.value.trim() === "") { | ||
| problems.push( | ||
| `messages/${locale}.json has an empty string at "${key}"` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (locale === defaultLocale) { | ||
| continue; | ||
| } | ||
|
|
||
| problems.push( | ||
| ...compareLocale(locale, defaultLocale, baselineEntries, entries) | ||
| ); | ||
| } | ||
|
|
||
| if (problems.length > 0) { | ||
| console.error( | ||
| `i18n message check failed with ${problems.length} problem(s):` | ||
| ); | ||
| console.error(""); | ||
| console.error(problems.join("\n\n")); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| console.log( | ||
| `i18n message check passed for ${locales.length} locale(s): ${locales.join( | ||
| ", " | ||
| )}` | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| main(); | ||
| } catch (error) { | ||
| console.error(error instanceof Error ? error.message : error); | ||
| process.exitCode = 1; | ||
| } |
This file contains hidden or 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.
The PR description calls out running
npm run buildin validation, but this workflow only runsnpm run check(currently i18n + prettier). Consider adding a build step (and/or typecheck/lint if applicable), or foldingnpm run buildintonpm run check, so CI enforces the stated validation.