Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(i18n): check placeholder and tag parity in i18n:check (#42532)

Baki Burak Öğünee0efe0bdbad
diff --git a/web/scripts/__tests__/check-i18n-values.spec.ts b/web/scripts/__tests__/check-i18n-values.spec.ts
new file mode 100644
index 00000000000..0f935326052
--- /dev/null
+++ b/web/scripts/__tests__/check-i18n-values.spec.ts
@@ -0,0 +1,182 @@
+import { describe, expect, it } from 'vite-plus/test'
+import { findValueIssues, formatValueIssue } from '../check-i18n-values'
+
+describe('check-i18n-values', () => {
+ describe('findValueIssues', () => {
+ it('should accept a translation that keeps every placeholder and tag', () => {
+ // Arrange
+ const source = {
+ 'invite.sent': 'Invitation sent to {{email}}',
+ 'plan.docs': '{{count,number}} documents',
+ 'encrypted.front': 'Your API KEY will be encrypted using <techLink>PKCS1_OAEP</techLink>',
+ }
+ const translation = {
+ 'invite.sent': '{{email}} adresine davet gönderildi',
+ 'plan.docs': '{{count,number}} belge',
+ 'encrypted.front':
+ 'API anahtarınız <techLink>PKCS1_OAEP</techLink> kullanılarak şifrelenecek',
+ }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([])
+ })
+
+ it('should report a placeholder the translation dropped', () => {
+ // Arrange
+ const source = { 'invite.sent': 'Invitation sent to {{email}}' }
+ const translation = { 'invite.sent': 'Davet gönderildi' }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([
+ { key: 'invite.sent', kind: 'placeholder', expected: ['{{email}}'], actual: [] },
+ ])
+ })
+
+ it('should report a placeholder the translation renamed', () => {
+ // Arrange
+ const source = { 'workspace.limit': 'Upgrade from {{free}} to Pro' }
+ const translation = { 'workspace.limit': "{{hobby}} planından Pro'ya yükseltin" }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([
+ {
+ key: 'workspace.limit',
+ kind: 'placeholder',
+ expected: ['{{free}}'],
+ actual: ['{{hobby}}'],
+ },
+ ])
+ })
+
+ it('should report a placeholder the translation invented', () => {
+ // Arrange
+ const source = { 'goal.editCriterion': 'Edit criterion' }
+ const translation = { 'goal.editCriterion': 'Kriteri düzenle C{{seq}}' }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([
+ { key: 'goal.editCriterion', kind: 'placeholder', expected: [], actual: ['{{seq}}'] },
+ ])
+ })
+
+ it('should report a dropped tag separately from placeholders', () => {
+ // Arrange
+ const source = { 'payment.switched': 'Switched from <bold>{{from}}</bold> to {{to}}' }
+ const translation = { 'payment.switched': '{{from}} planından {{to}} planına geçildi' }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([
+ {
+ key: 'payment.switched',
+ kind: 'tag',
+ expected: ['</bold>', '<bold>'],
+ actual: [],
+ },
+ ])
+ })
+
+ it('should accept placeholders reordered by the target language word order', () => {
+ // Arrange
+ const source = { 'usage.window': '{{used}} of {{total}} used' }
+ const translation = { 'usage.window': '{{total}} içinden {{used}} kullanıldı' }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([])
+ })
+
+ it('should report a repeated placeholder that appears a different number of times', () => {
+ // Arrange
+ const source = { 'quota.hint': '{{name}} is full. Free up space in {{name}}.' }
+ const translation = { 'quota.hint': '{{name}} dolu. Yer açın.' }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([
+ {
+ key: 'quota.hint',
+ kind: 'placeholder',
+ expected: ['{{name}}', '{{name}}'],
+ actual: ['{{name}}'],
+ },
+ ])
+ })
+
+ it('should ignore keys that are missing from the translation', () => {
+ // Arrange
+ const source = { present: 'Sent to {{email}}', absent: 'Retry in {{minutes}}' }
+ const translation = { present: '{{email}} adresine gönderildi' }
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([])
+ })
+
+ it('should report a key whose translation is present but not a string', () => {
+ // Arrange
+ // The key check is satisfied by the key alone, so nothing else would
+ // notice that the sentence and its placeholder are gone.
+ const source = { 'invite.sent': 'Sent to {{email}}' }
+ const translation = { 'invite.sent': null } as Record<string, unknown>
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([
+ { key: 'invite.sent', kind: 'placeholder', expected: ['{{email}}'], actual: [] },
+ ])
+ })
+
+ it('should accept a non-string translation when the source carries no marker', () => {
+ // Arrange
+ const source = { 'plan.name': 'Sandbox' }
+ const translation = { 'plan.name': 42 } as Record<string, unknown>
+
+ // Act
+ const issues = findValueIssues(source, translation)
+
+ // Assert
+ expect(issues).toEqual([])
+ })
+ })
+
+ describe('formatValueIssue', () => {
+ it('should name the file, key and both sides of a placeholder mismatch', () => {
+ // Arrange
+ const issue = {
+ key: 'invite.sent',
+ kind: 'placeholder' as const,
+ expected: ['{{email}}'],
+ actual: [],
+ }
+
+ // Act
+ const line = formatValueIssue('common', issue)
+
+ // Assert
+ expect(line).toBe('common.invite.sent: expected placeholders {{email}}, found (none)')
+ })
+ })
+})
diff --git a/web/scripts/check-i18n-values.ts b/web/scripts/check-i18n-values.ts
new file mode 100644
index 00000000000..f7070a6568e
--- /dev/null
+++ b/web/scripts/check-i18n-values.ts
@@ -0,0 +1,99 @@
+/**
+ * Value-level checks for locale files.
+ *
+ * `check-i18n.js` compares key sets, so a translation can drop, invent or rename
+ * an interpolation variable and still pass. Those breakages reach the user as a
+ * raw `{{name}}` in the UI, or as a sentence missing the value it was meant to
+ * show. The translation sync workflow asks its agent to preserve placeholders
+ * exactly, and these functions are what verifies that it did.
+ */
+
+export type ValueIssueKind = 'placeholder' | 'tag'
+
+export type ValueIssue = {
+ key: string
+ kind: ValueIssueKind
+ expected: string[]
+ actual: string[]
+}
+
+/** i18next interpolation, including formatters such as `{{count,number}}`. */
+const PLACEHOLDER_PATTERN = /\{\{[^{}]*\}\}|\$\{[^{}]*\}/g
+
+/** Markup handed to `<Trans>`, plus plain HTML tags used inside values. */
+const TAG_PATTERN = /<\/?[a-z][^<>]*>/gi
+
+function collect(value: string, pattern: RegExp): string[] {
+ return (value.match(pattern) ?? []).sort()
+}
+
+/**
+ * Interpolation variables in `value`, sorted so that reordering a sentence is
+ * not reported as a difference.
+ */
+export function extractPlaceholders(value: string): string[] {
+ return collect(value, PLACEHOLDER_PATTERN)
+}
+
+/** Markup tags in `value`, sorted for the same reason as placeholders. */
+export function extractTags(value: string): string[] {
+ return collect(value, TAG_PATTERN)
+}
+
+function sameMembers(expected: string[], actual: string[]): boolean {
+ return (
+ expected.length === actual.length && expected.every((item, index) => item === actual[index])
+ )
+}
+
+/**
+ * Compare one locale file against its en-US source.
+ *
+ * Only keys present in both files are compared; missing and extra keys are
+ * already reported by the key-level check, and reporting them twice would bury
+ * the value problems this is meant to surface.
+ *
+ * A key that is present but holds something other than a string is a different
+ * case: the key check sees it and is satisfied, so nothing else would notice
+ * that the sentence — and every placeholder in it — is gone. Such a value is
+ * compared as the empty string, which makes a marker-bearing source fail here.
+ */
+export function findValueIssues(
+ source: Record<string, unknown>,
+ translation: Record<string, unknown>,
+): ValueIssue[] {
+ const issues: ValueIssue[] = []
+
+ for (const [key, sourceValue] of Object.entries(source)) {
+ if (typeof sourceValue !== 'string') continue
+ if (!Object.hasOwn(translation, key)) continue
+
+ const rawValue = translation[key]
+ const translatedValue = typeof rawValue === 'string' ? rawValue : ''
+
+ const expectedPlaceholders = extractPlaceholders(sourceValue)
+ const actualPlaceholders = extractPlaceholders(translatedValue)
+ if (!sameMembers(expectedPlaceholders, actualPlaceholders)) {
+ issues.push({
+ key,
+ kind: 'placeholder',
+ expected: expectedPlaceholders,
+ actual: actualPlaceholders,
+ })
+ }
+
+ const expectedTags = extractTags(sourceValue)
+ const actualTags = extractTags(translatedValue)
+ if (!sameMembers(expectedTags, actualTags))
+ issues.push({ key, kind: 'tag', expected: expectedTags, actual: actualTags })
+ }
+
+ return issues
+}
+
+export function formatValueIssue(fileName: string, issue: ValueIssue): string {
+ const label = issue.kind === 'placeholder' ? 'placeholders' : 'tags'
+ const expected = issue.expected.length ? issue.expected.join(' ') : '(none)'
+ const actual = issue.actual.length ? issue.actual.join(' ') : '(none)'
+ return `${fileName}.${issue.key}: expected ${label} ${expected}, found ${actual}`
+}
diff --git a/web/scripts/check-i18n.js b/web/scripts/check-i18n.js
index cc206914fbd..f364ab1c576 100644
--- a/web/scripts/check-i18n.js
+++ b/web/scripts/check-i18n.js
@@ -2,6 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import data from '../i18n/languages'
+import { findValueIssues, formatValueIssue } from './check-i18n-values'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
@@ -196,6 +197,28 @@ async function removeExtraKeysFromFile(language, fileName, extraKeys) {
}
}
+function readLocaleFile(language, fileName) {
+ const filePath = path.resolve(__dirname, '../i18n/locales', language, `${fileName}.json`)
+
+ if (!fs.existsSync(filePath)) return null
+
+ try {
+ const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'))
+ return parsed && typeof parsed === 'object' ? parsed : null
+ } catch (error) {
+ console.error(`Error processing file ${filePath}:`, error.message)
+ return null
+ }
+}
+
+function listSourceFileNames() {
+ const folderPath = path.resolve(__dirname, '../i18n/locales', targetLanguage)
+ return fs
+ .readdirSync(folderPath)
+ .filter((file) => file.endsWith('.json'))
+ .map((file) => file.replace(/\.json$/, ''))
+}
+
// Add command line argument support
const args = parseArgs(process.argv)
const targetFiles = Array.from(new Set(args.files))
@@ -283,6 +306,35 @@ async function main() {
return hasDiff
}
+ const compareValues = () => {
+ let hasDiff = false
+ const fileNames = targetFiles.length ? targetFiles : listSourceFileNames()
+ const languagesToProcess = (targetLangs.length ? targetLangs : languages).filter(
+ (language) => language !== targetLanguage,
+ )
+
+ for (const language of languagesToProcess) {
+ const issueLines = []
+
+ for (const fileName of fileNames) {
+ const source = readLocaleFile(targetLanguage, fileName)
+ const translation = readLocaleFile(language, fileName)
+ if (!source || !translation) continue
+
+ for (const issue of findValueIssues(source, translation))
+ issueLines.push(formatValueIssue(fileName, issue))
+ }
+
+ if (issueLines.length) {
+ hasDiff = true
+ console.log(`Placeholder and tag mismatches in ${language}:`)
+ issueLines.forEach((line) => console.log(` ${line}`))
+ }
+ }
+
+ return hasDiff
+ }
+
console.log('🚀 Starting i18n:check script...')
if (targetFiles.length) console.log(`📁 Checking files: ${targetFiles.join(', ')}`)
@@ -290,9 +342,10 @@ async function main() {
if (autoRemove) console.log('🤖 Auto-remove mode: ENABLED')
- const hasDiff = await compareKeysCount()
- if (hasDiff) {
- console.error('\n❌ i18n keys are not aligned. Fix issues above.')
+ const hasKeyDiff = await compareKeysCount()
+ const hasValueDiff = compareValues()
+ if (hasKeyDiff || hasValueDiff) {
+ console.error('\n❌ i18n files are not aligned. Fix issues above.')
process.exitCode = 1
} else {
console.log('\n✅ All i18n files are in sync')