forked from fccview/jotty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.ts
More file actions
102 lines (84 loc) · 3.03 KB
/
i18n.ts
File metadata and controls
102 lines (84 loc) · 3.03 KB
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
/**
* @fccview here!
* I know this feels quite complext for a i18n.ts file, but hear me out.
*
* My idea was to actually allow users to override the translations with a custom translation file.
* This is a bit of a hacky solution but it'll make it so ANYONE can create their own translation file and test it locally
* without the need to pull the repo, right in the comfort of their own docker container.
*
* If you have any ideas on how to improve this and make it feel less hacky please create a PR!!!
*/
import { getRequestConfig } from 'next-intl/server';
import path from 'path';
import fs from 'fs';
import { getCurrentUser } from './app/_server/actions/users';
import { getAvailableLocales } from './app/_utils/locale-utils';
function _fallbackMerger(target: any, source: any): any {
const output = { ...target };
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!(key in target)) {
output[key] = source[key];
} else {
output[key] = _fallbackMerger(target[key], source[key]);
}
} else {
output[key] = source[key];
}
});
}
return output;
}
function isObject(item: any): boolean {
return item && typeof item === 'object' && !Array.isArray(item);
}
export type Locale = string;
export default getRequestConfig(async () => {
const user = await getCurrentUser();
const defaultLocale = process.env.DEFAULT_LOCALE || 'en';
const locale = user?.preferredLocale || defaultLocale;
const availableLocales = await getAvailableLocales();
if (!availableLocales.includes(locale)) {
console.warn(`Locale "${locale}" not found, falling back to "en"`);
const fallbackLocale = 'en';
const defaultMessages = (await import(`./app/_translations/${fallbackLocale}.json`)).default;
return {
locale: fallbackLocale,
messages: defaultMessages,
};
}
const englishMessages = (await import(`./app/_translations/en.json`)).default;
const localeMessages = locale !== 'en'
? (await import(`./app/_translations/${locale}.json`)).default
: {};
const customTranslationFile = process.env.CUSTOM_TRANSLATION_FILE;
let customMessages = {};
if (customTranslationFile) {
const customTranslationPath = path.join(
process.cwd(),
'config',
customTranslationFile || 'custom-translations.json'
);
try {
if (fs.existsSync(customTranslationPath)) {
const customFileContent = fs.readFileSync(customTranslationPath, 'utf-8');
customMessages = JSON.parse(customFileContent);
console.log(`Custom translations loaded from: ${customTranslationPath}`);
}
} catch (error) {
console.error(`Error loading custom translation file: ${error}`);
}
}
let messages = englishMessages;
if (locale !== 'en') {
messages = _fallbackMerger(englishMessages, localeMessages);
}
if (Object.keys(customMessages).length > 0) {
messages = _fallbackMerger(messages, customMessages);
}
return {
locale,
messages,
};
});