fix(tests): fix i18n tests
This commit is contained in:
@@ -118,10 +118,12 @@
|
||||
"Stream started": "Stream started",
|
||||
"TROUBLESHOOT": "TROUBLESHOOT",
|
||||
"Testing": {
|
||||
"itemCount": "<strong><em>Missing translation Testing.itemCount: Please report</em></strong>",
|
||||
"messageCount": "<strong><em>Missing translation Testing.messageCount: Please report</em></strong>",
|
||||
"noPluralKey": "<strong><em>Missing translation Testing.noPluralKey: Please report</em></strong>",
|
||||
"simpleKey": "<strong><em>Missing translation Testing.simpleKey: Please report</em></strong>"
|
||||
"itemCount": "You have {{count}} items",
|
||||
"itemCount_one": "You have {{count}} item",
|
||||
"messageCount": "{{sender}} has sent {{count}} messages",
|
||||
"messageCount_one": "{{sender}} has sent one message",
|
||||
"noPluralKey": "This has no singular key (_one).",
|
||||
"simpleKey": "Simple translation text"
|
||||
},
|
||||
"Time": "Time",
|
||||
"Timestamp": "Timestamp",
|
||||
|
||||
@@ -7,30 +7,48 @@ import { Localization } from '../types/localization';
|
||||
jest.mock('next-export-i18n', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, vars?: Record<string, any>) => {
|
||||
// Mock translations for testing
|
||||
// Use the actual keys as in localization.ts and translation.json
|
||||
const translations: Record<string, string> = {
|
||||
hello_world: 'Hello <strong>{{name}}</strong>, welcome to the world!',
|
||||
'Chat is offline': 'Chat is offline',
|
||||
chat_offline: 'Chat is offline',
|
||||
notification_message:
|
||||
'You can <a href="#">click here</a> to receive notifications when {{streamer}} goes live.',
|
||||
simple_key: 'Simple translation text',
|
||||
|
||||
// Pluralization test keys
|
||||
item_count_one: 'You have {{count}} item',
|
||||
item_count: 'You have {{count}} items', // Original key serves as plural form
|
||||
message_count_one: 'You have {{count}} message from {{sender}}',
|
||||
message_count: 'You have {{count}} messages from {{sender}}', // Original key serves as plural form
|
||||
|
||||
// Keys without pluralization variants
|
||||
no_plural_key: 'This key has no plural variants - {{count}} things',
|
||||
component_error: 'Error: {{message}}',
|
||||
offline_basic: 'This stream is offline. Check back soon!',
|
||||
// Testing keys
|
||||
'Testing.simpleKey': 'Simple translation text',
|
||||
'Testing.itemCount_one': 'You have {{count}} item',
|
||||
'Testing.itemCount': 'You have {{count}} items',
|
||||
'Testing.messageCount_one': 'You have {{count}} message from {{sender}}',
|
||||
'Testing.messageCount': 'You have {{count}} messages from {{sender}}',
|
||||
'Testing.noPluralKey': 'This key has no plural variants - {{count}} things',
|
||||
};
|
||||
|
||||
let result = translations[key] || key;
|
||||
let result = translations[key];
|
||||
|
||||
// If not found, try to fallback to a snake_case version (for legacy or fallback)
|
||||
if (!result && key.includes('.')) {
|
||||
const [ns, k] = key.split('.');
|
||||
// Try snake_case
|
||||
const snakeKey =
|
||||
ns +
|
||||
'.' +
|
||||
k
|
||||
.replace(/([A-Z])/g, '_$1')
|
||||
.toLowerCase()
|
||||
.replace(/^_/, '');
|
||||
result = translations[snakeKey];
|
||||
}
|
||||
|
||||
// If still not found, return the key itself
|
||||
if (!result) {
|
||||
result = key;
|
||||
}
|
||||
|
||||
// Simple variable replacement for testing
|
||||
if (vars) {
|
||||
Object.keys(vars).forEach(varKey => {
|
||||
result = result.replace(`{{${varKey}}}`, vars[varKey]);
|
||||
result = result.replace(new RegExp(`{{${varKey}}}`, 'g'), vars[varKey]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -118,10 +118,10 @@ export const Localization = {
|
||||
Testing: {
|
||||
testing: 'testing_string',
|
||||
another: 'another_test',
|
||||
simpleKey: 'simple_key',
|
||||
itemCount: 'item_count',
|
||||
messageCount: 'message_count',
|
||||
noPluralKey: 'no_plural_key',
|
||||
simpleKey: 'Testing.simpleKey',
|
||||
itemCount: 'Testing.itemCount',
|
||||
messageCount: 'Testing.messageCount',
|
||||
noPluralKey: 'Testing.noPluralKey',
|
||||
},
|
||||
} as const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user