mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 02:48:13 -05:00
[PR #14885] [CLOSED] feat(i18n): Implement automated translation file management script #23629
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/14885
Author: @silentoplayz
Created: 6/11/2025
Status: ❌ Closed
Base:
dev← Head:i18n-keys-alignment📝 Commits (4)
f725c40fix: Removed orphaned keys & aligned with en-US keys63b7bdachore: Update 2 translation.json files4858268fix: consistent sorting of all keys, including nested ones, with lowercase before uppercase4c2885bUpdate sync-translations.cjs📊 Changes
4 files changed (+1675 additions, -557 deletions)
View changed files
📝
package.json(+2 -1)➕
scripts/sync-translations.cjs(+228 -0)📝
src/lib/i18n/locales/gl-ES/translation.json(+259 -47)📝
src/lib/i18n/locales/tk-TM/translation.json(+1186 -509)📄 Description
Pull Request Checklist
Before submitting, make sure you've checked the following:
devbranch.Changelog Entry
Description
scripts/sync-translations.cjs) and an associatednpm run i18n:synccommand to fully automate and standardize the management oftranslation.jsonfiles across all locales. This significantly reduces manual effort for internationalization, ensuring unparalleled consistency and accuracy in our internationalization assets.Added
npm run i18n:syncscript inpackage.json.scripts/sync-translations.cjs.Changed
translation.jsonfiles, including newly created ones, are now automatically synchronized with theen-USmaster locale. This includes:src/lib/i18n/locales/xx-YY), and runningnpm run i18n:syncwill automatically generate and populate itstranslation.jsonfile from theen-USmaster.translation.jsonfiles are strictly aligned with theen-USmaster's key set and nested structure, preserving existing translations.Removed
translation.jsonfiles if they are not present in theen-USmaster, ensuring a clean and current key set.Fixed
translation.jsonfiles (e.g., missing keys are added, and keys not present inen-USare removed).translation.jsonfiles, including nested objects, to ensure a consistent, natural alphabetical order (lowercase before uppercase).Breaking Changes
en-USmaster), this is not considered a breaking change as these keys were already non-functional or obsolete. It strictly improves consistency without impacting active translations.Additional Information
This pull request enhances our existing internationalization (i18n) workflow by introducing a dedicated synchronization script.
Existing i18n Workflow (Context):
i18next-parser(npm run i18n:parse): Our project currently usesi18next-parser(configured byi18next-parser.config.ts) to automate the extraction of translation keys.src/**/*.{js,svelte}).translation.jsonfiles (e.g.,src/lib/i18n/locales/$LOCALE/$NAMESPACE.json).en-US/translation.json(and other generated files) up-to-date with strings found in the code, adding new keys (defaultValue: '') and potentially removing keys no longer detected in the codebase (keepRemoved: false).i18next-parseris dynamically sourced fromsrc/lib/i18n/locales/languages.jsonthrough thegetLanguageshelper insrc/lib/i18n/index.ts.src/lib/i18n/index.ts: This file sets upi18nextfor our application, handling language detection, loading translation resources dynamically (resourcesToBackend), and managing the active language. It's the runtime core of our i18n system.The Problem
sync-translations.cjsSolves:While
i18next-parseris excellent for code-to-translation file synchronization, it has limitations in ensuring perfect consistency between existingtranslation.jsonfiles themselves, or in enforcing a precise, consistent internal sorting order across all files. This can lead to:en-USbut be missing intk-TMorgl-ES(if it wasn't a newly extracted key by the parser). Conversely, some locale files might accumulate "orphaned" keys not present inen-USor the current codebase.i18next-parserofferssort: true, its default alphabetical sorting might not match a "natural" order (e.g., placing "Add Arena" before "Add a model"), leading to messy Git diffs and difficult readability.translation.jsonfile and then running the parser, which still wouldn't guarantee a complete key set without manual merging.How
scripts/sync-translations.cjs(npm run i18n:sync) Works:To address these gaps, the new
scripts/sync-translations.cjsscript provides a post-processing and comprehensive synchronization layer that operates directly on the JSON translation files, leveraging theen-USfile as the ultimate source of truth.Master Locale as Canonical Source:
src/lib/i18n/locales/en-US/translation.json. This file is used as the definitive canonical structure for all keys (including nested paths) and their desired alphabetical order. All other locale files will be brought into strict alignment with this master.Iterative Processing of All Locales:
src/lib/i18n/locales/(e.g.,tk-TM,gl-ES,fr), regardless of whether atranslation.jsonfile currently exists within them.Automatic File Creation and Initialization:
translation.jsonfile is missing (e.g., for a newly added language where only the folder exists), the script automatically creates this file. It initializes this new file as an empty JSON object in memory, ready to be populated.Intelligent Key Merging (Adding Missing Keys):
translation.jsondata (whether newly loaded or initialized as empty), the script performs a deep, recursive merge with theen-USmaster's canonical structure.en-USmaster but missing in the current locale's file are recursively added.en-USmaster as a placeholder, or an empty string ("") if noen-USequivalent is found (though this is rare givenen-USis the source).Strict Key Pruning (Removing Unused Keys):
translation.jsonfile but is not present in theen-USmaster's canonical structure, that key is automatically removed. This eliminates obsolete entries and keeps our translation files lean and current, ensuring they only contain keys actively defined in the master.Recursive Natural Alphabetical Sorting:
translation.jsonfile is recursively sorted alphabetically.String.prototype.localeComparewithcaseFirst: 'lower', which ensures a consistent and "natural" alphabetical order. This means lowercase letters will consistently appear before their uppercase counterparts (e.g.,"Add a model ID"will sort before"Add Arena Model"), creating highly readable files and minimal Git diffs.Optimized File Writes:
The Workflow:
The ideal workflow after this PR will be:
npm run i18n:parse: Run this first to scan your codebase for new strings and updateen-US/translation.json(and other files) with what's found in the code. This is your "source code to master translation" sync.npm run i18n:sync: Run this second to take the (now updated)en-US/translation.jsonand propagate its structure, keys, and order to all othertranslation.jsonfiles. This is your "master translation to other translations" sync and cleanup.In essence:
i18n:parseis about discovery and extraction from code.i18n:syncis about enforcement of consistency, standardization, and cleanup between translation files, driven by a single master.Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.