wwf
2025-05-20 938c3e5a587ce950a94964ea509b9e7f8834dfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use client'
import { resources } from '@/i18n/i18next-config'
import { useEffect, useState } from 'react'
import cn from '@/utils/classnames'
 
export default function I18nTest() {
  const [langs, setLangs] = useState<Lang[]>([])
 
  useEffect(() => {
    setLangs(genLangs())
  }, [])
 
  return (
    <div
      style={{
        height: 'calc(100% - 6em)',
        overflowY: 'auto',
        margin: '1em 1em 5em',
      }}
    >
 
      <div style={{ minHeight: '75vh' }}>
        <h2>Summary</h2>
 
        <table
          className={cn('mt-2 min-w-[340px] border-collapse border-0')}
        >
          <thead className="system-xs-medium-uppercase text-text-tertiary">
            <tr>
              <td className="w-5 min-w-5 whitespace-nowrap rounded-l-lg bg-background-section-burn pl-2 pr-1">
                #
              </td>
              <td className="w-20 min-w-20 whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
                lang
              </td>
              <td className="w-20 min-w-20 whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
                count
              </td>
              <td className="w-20 min-w-20 whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
                missing
              </td>
              <td className="w-20 min-w-20 whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
                extra
              </td>
            </tr>
          </thead>
          <tbody className="system-sm-regular text-text-secondary">
            {langs.map(({ locale, count, missing, extra }, idx) => <tr key={locale}>
              <td className="">{idx}</td>
              <td className="p-1.5">{locale}</td>
              <td>{count}</td>
              <td>{missing.length}</td>
              <td>{extra.length}</td>
            </tr>)}
          </tbody>
        </table>
      </div>
 
      <h2>Details</h2>
 
      <table
        className={cn('mt-2 w-full min-w-[340px] border-collapse border-0')}
      >
        <thead className="system-xs-medium-uppercase text-text-tertiary">
          <tr>
            <td className="w-5 min-w-5 whitespace-nowrap rounded-l-lg bg-background-section-burn pl-2 pr-1">
              #
            </td>
            <td className="w-20 min-w-20 whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
              lang
            </td>
            <td className="w-full whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
              missing
            </td>
            <td className="w-full whitespace-nowrap bg-background-section-burn py-1.5 pl-3">
              extra
            </td>
          </tr>
        </thead>
 
        <tbody>
          {langs.map(({ locale, missing, extra }, idx) => {
            return (<tr key={locale}>
              <td className="py-2 align-top">{idx}</td>
              <td className="py-2 align-top">{locale}</td>
              <td className="py-2 align-top">
                <ul>
                  {missing.map(key => (
                    <li key={key}>{key}</li>
                  ))}
                </ul>
              </td>
              <td className="py-2 align-top">
                <ul>
                  {extra.map(key => (
                    <li key={key}>{key}</li>
                  ))}
                </ul>
              </td>
            </tr>
            )
          })}
        </tbody>
      </table>
 
    </div>
  )
}
 
function genLangs() {
  const langs_: Lang[] = []
  let en!: Lang
 
  for (const [key, value] of Object.entries(resources)) {
    const keys = getNestedKeys(value.translation)
    const lang: Lang = {
      locale: key,
      keys: new Set(keys),
      count: keys.length,
      missing: [],
      extra: [],
    }
 
    langs_.push(lang)
    if (key === 'en-US') en = lang
  }
 
  for (const lang of langs_) {
    const missing: string[] = []
    const extra: string[] = []
 
    for (const key of lang.keys)
      if (!en.keys.has(key)) extra.push(key)
 
    for (const key of en.keys)
      if (!lang.keys.has(key)) missing.push(key)
 
    lang.missing = missing
    lang.extra = extra
  }
  return langs_
}
 
function getNestedKeys(translation: Record<string, any>): string[] {
  const nestedKeys: string[] = []
  const iterateKeys = (obj: Record<string, any>, prefix = '') => {
    for (const key in obj) {
      const nestedKey = prefix ? `${prefix}.${key}` : key
      //   nestedKeys.push(nestedKey);
      if (typeof obj[key] === 'object') iterateKeys(obj[key], nestedKey)
      else if (typeof obj[key] === 'string') nestedKeys.push(nestedKey)
    }
  }
  iterateKeys(translation)
  return nestedKeys
}
 
type Lang = {
  locale: string;
  keys: Set<string>;
  count: number;
  missing: string[];
  extra: string[];
}