]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
feat: add locale unit test (#388)
authorCoolPlayLin <coolplaylin@qq.com>
Fri, 1 Dec 2023 10:44:00 +0000 (18:44 +0800)
committerGitHub <noreply@github.com>
Fri, 1 Dec 2023 10:44:00 +0000 (11:44 +0100)
__test__/locale.spec.ts [new file with mode: 0644]

diff --git a/__test__/locale.spec.ts b/__test__/locale.spec.ts
new file mode 100644 (file)
index 0000000..1ea0a11
--- /dev/null
@@ -0,0 +1,28 @@
+import { describe, it, expect } from 'vitest'
+import { resolve } from 'node:path'
+import { readdirSync } from 'node:fs'
+import en from '../locales/en-US.json'
+
+function getKeys(obj: any, path = '', result: string[] = []) {
+  for (let key in obj) {
+    if (typeof obj[key] === 'object') {
+      getKeys(obj[key], path ? `${path}.${key}` : key, result);
+    } else {
+      result.push(path ? `${path}.${key}` : key);
+    }
+  }
+  return result;
+}
+
+const localesOtherThanEnglish = readdirSync(resolve(__dirname, '../locales')).filter((file) => {
+  return file.endsWith('.json') && !file.startsWith('en-US')
+})
+const defaultKeys = getKeys(en);
+
+describe("locale files should include all keys", () => {
+  localesOtherThanEnglish.forEach((locale) => {
+    it(`for ${locale}`, () => {
+      expect(getKeys(require(`../locales/${locale}`))).toEqual(defaultKeys)
+    })
+  })
+})
\ No newline at end of file