|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 4 | +import { FileI18nAdapter } from './file-i18n-adapter'; |
| 5 | +import type { II18nService } from '@objectstack/spec/contracts'; |
| 6 | +import * as fs from 'node:fs'; |
| 7 | +import * as path from 'node:path'; |
| 8 | +import * as os from 'node:os'; |
| 9 | + |
| 10 | +describe('FileI18nAdapter', () => { |
| 11 | + it('should implement II18nService contract', () => { |
| 12 | + const i18n: II18nService = new FileI18nAdapter(); |
| 13 | + expect(typeof i18n.t).toBe('function'); |
| 14 | + expect(typeof i18n.getTranslations).toBe('function'); |
| 15 | + expect(typeof i18n.loadTranslations).toBe('function'); |
| 16 | + expect(typeof i18n.getLocales).toBe('function'); |
| 17 | + expect(typeof i18n.getDefaultLocale).toBe('function'); |
| 18 | + expect(typeof i18n.setDefaultLocale).toBe('function'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should default to "en" locale', () => { |
| 22 | + const i18n = new FileI18nAdapter(); |
| 23 | + expect(i18n.getDefaultLocale()).toBe('en'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should use custom default locale', () => { |
| 27 | + const i18n = new FileI18nAdapter({ defaultLocale: 'zh-CN' }); |
| 28 | + expect(i18n.getDefaultLocale()).toBe('zh-CN'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should set and get default locale', () => { |
| 32 | + const i18n = new FileI18nAdapter(); |
| 33 | + i18n.setDefaultLocale('ja'); |
| 34 | + expect(i18n.getDefaultLocale()).toBe('ja'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return empty translations for unknown locale', () => { |
| 38 | + const i18n = new FileI18nAdapter(); |
| 39 | + expect(i18n.getTranslations('fr')).toEqual({}); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return empty locales when no translations loaded', () => { |
| 43 | + const i18n = new FileI18nAdapter(); |
| 44 | + expect(i18n.getLocales()).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should load and retrieve translations', () => { |
| 48 | + const i18n = new FileI18nAdapter(); |
| 49 | + i18n.loadTranslations('en', { greeting: 'Hello' }); |
| 50 | + i18n.loadTranslations('zh-CN', { greeting: '你好' }); |
| 51 | + |
| 52 | + expect(i18n.getLocales()).toContain('en'); |
| 53 | + expect(i18n.getLocales()).toContain('zh-CN'); |
| 54 | + expect(i18n.getTranslations('en')).toEqual({ greeting: 'Hello' }); |
| 55 | + expect(i18n.getTranslations('zh-CN')).toEqual({ greeting: '你好' }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should merge translations when loading into existing locale', () => { |
| 59 | + const i18n = new FileI18nAdapter(); |
| 60 | + i18n.loadTranslations('en', { greeting: 'Hello' }); |
| 61 | + i18n.loadTranslations('en', { farewell: 'Goodbye' }); |
| 62 | + |
| 63 | + expect(i18n.getTranslations('en')).toEqual({ |
| 64 | + greeting: 'Hello', |
| 65 | + farewell: 'Goodbye', |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should translate a simple key', () => { |
| 70 | + const i18n = new FileI18nAdapter(); |
| 71 | + i18n.loadTranslations('en', { greeting: 'Hello' }); |
| 72 | + |
| 73 | + expect(i18n.t('greeting', 'en')).toBe('Hello'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return key when translation is missing', () => { |
| 77 | + const i18n = new FileI18nAdapter(); |
| 78 | + expect(i18n.t('missing.key', 'en')).toBe('missing.key'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should resolve nested dot-notation keys', () => { |
| 82 | + const i18n = new FileI18nAdapter(); |
| 83 | + i18n.loadTranslations('en', { |
| 84 | + objects: { |
| 85 | + account: { |
| 86 | + label: 'Account', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(i18n.t('objects.account.label', 'en')).toBe('Account'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should interpolate parameters', () => { |
| 95 | + const i18n = new FileI18nAdapter(); |
| 96 | + i18n.loadTranslations('en', { greeting: 'Hello, {{name}}!' }); |
| 97 | + |
| 98 | + expect(i18n.t('greeting', 'en', { name: 'World' })).toBe('Hello, World!'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should keep placeholder when parameter is missing', () => { |
| 102 | + const i18n = new FileI18nAdapter(); |
| 103 | + i18n.loadTranslations('en', { greeting: 'Hello, {{name}}!' }); |
| 104 | + |
| 105 | + expect(i18n.t('greeting', 'en', {})).toBe('Hello, {{name}}!'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should fallback to fallback locale when key not found', () => { |
| 109 | + const i18n = new FileI18nAdapter({ fallbackLocale: 'en' }); |
| 110 | + i18n.loadTranslations('en', { greeting: 'Hello' }); |
| 111 | + |
| 112 | + expect(i18n.t('greeting', 'zh-CN')).toBe('Hello'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not fallback when key exists in requested locale', () => { |
| 116 | + const i18n = new FileI18nAdapter({ fallbackLocale: 'en' }); |
| 117 | + i18n.loadTranslations('en', { greeting: 'Hello' }); |
| 118 | + i18n.loadTranslations('zh-CN', { greeting: '你好' }); |
| 119 | + |
| 120 | + expect(i18n.t('greeting', 'zh-CN')).toBe('你好'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should return key when neither locale nor fallback has translation', () => { |
| 124 | + const i18n = new FileI18nAdapter({ fallbackLocale: 'en' }); |
| 125 | + i18n.loadTranslations('en', { greeting: 'Hello' }); |
| 126 | + |
| 127 | + expect(i18n.t('missing.key', 'zh-CN')).toBe('missing.key'); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('file-based loading', () => { |
| 131 | + let tmpDir: string; |
| 132 | + |
| 133 | + beforeEach(() => { |
| 134 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'i18n-test-')); |
| 135 | + }); |
| 136 | + |
| 137 | + afterEach(() => { |
| 138 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should load translations from JSON files in a directory', () => { |
| 142 | + fs.writeFileSync( |
| 143 | + path.join(tmpDir, 'en.json'), |
| 144 | + JSON.stringify({ greeting: 'Hello', objects: { account: { label: 'Account' } } }), |
| 145 | + ); |
| 146 | + fs.writeFileSync( |
| 147 | + path.join(tmpDir, 'zh-CN.json'), |
| 148 | + JSON.stringify({ greeting: '你好', objects: { account: { label: '客户' } } }), |
| 149 | + ); |
| 150 | + |
| 151 | + const i18n = new FileI18nAdapter({ localesDir: tmpDir }); |
| 152 | + |
| 153 | + expect(i18n.getLocales()).toContain('en'); |
| 154 | + expect(i18n.getLocales()).toContain('zh-CN'); |
| 155 | + expect(i18n.t('greeting', 'en')).toBe('Hello'); |
| 156 | + expect(i18n.t('greeting', 'zh-CN')).toBe('你好'); |
| 157 | + expect(i18n.t('objects.account.label', 'en')).toBe('Account'); |
| 158 | + expect(i18n.t('objects.account.label', 'zh-CN')).toBe('客户'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should ignore non-JSON files in the directory', () => { |
| 162 | + fs.writeFileSync(path.join(tmpDir, 'en.json'), JSON.stringify({ greeting: 'Hello' })); |
| 163 | + fs.writeFileSync(path.join(tmpDir, 'notes.txt'), 'not a translation file'); |
| 164 | + |
| 165 | + const i18n = new FileI18nAdapter({ localesDir: tmpDir }); |
| 166 | + |
| 167 | + expect(i18n.getLocales()).toEqual(['en']); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should skip malformed JSON files gracefully', () => { |
| 171 | + fs.writeFileSync(path.join(tmpDir, 'en.json'), JSON.stringify({ greeting: 'Hello' })); |
| 172 | + fs.writeFileSync(path.join(tmpDir, 'bad.json'), '{invalid json'); |
| 173 | + |
| 174 | + const i18n = new FileI18nAdapter({ localesDir: tmpDir }); |
| 175 | + |
| 176 | + expect(i18n.getLocales()).toEqual(['en']); |
| 177 | + expect(i18n.t('greeting', 'en')).toBe('Hello'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should handle non-existent directory gracefully', () => { |
| 181 | + const i18n = new FileI18nAdapter({ localesDir: '/nonexistent/path' }); |
| 182 | + expect(i18n.getLocales()).toEqual([]); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments