Skip to content

Commit f11d8ae

Browse files
committed
refactor: Code Cleanup
1 parent c0e8479 commit f11d8ae

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

src/index.ts

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export function isLoaded(lang?: string): boolean {
4242
return loaded.some((row) => row.lang.replace(/[-_]/g, '-') === lang.replace(/[-_]/g, '-'))
4343
}
4444

45+
/**
46+
* Loads the language async.
47+
*/
4548
function loadLanguage(lang: string, dashLangTry: boolean = false): void {
4649
const loadedLang: LanguageInterface = loaded.find((row) => row.lang === lang)
4750

@@ -53,22 +56,7 @@ function loadLanguage(lang: string, dashLangTry: boolean = false): void {
5356

5457
const { default: messages } = resolveLang(options.resolve, lang);
5558

56-
if (Object.keys(messages).length < 1) {
57-
if (/[-_]/g.test(lang) && !dashLangTry) {
58-
return loadLanguage(
59-
lang.replace(/[-_]/g, (char) => (char === '-' ? '_' : '-')),
60-
true
61-
);
62-
}
63-
64-
if (lang !== options.fallbackLang) {
65-
return loadLanguage(options.fallbackLang);
66-
}
67-
}
68-
69-
const data: LanguageInterface = { lang, messages }
70-
loaded.push(data)
71-
setLanguage(data)
59+
applyLanguage(lang, messages, dashLangTry, loadLanguage);
7260
}
7361

7462
/**
@@ -81,23 +69,37 @@ export function loadLanguageAsync(lang: string, dashLangTry = false): Promise<st
8169
return Promise.resolve(setLanguage(loadedLang))
8270
}
8371

84-
return resolveLangAsync(options.resolve, lang).then(({ default: messages }) => {
85-
if (Object.keys(messages).length < 1) {
86-
if (/[-_]/g.test(lang) && !dashLangTry) {
87-
return loadLanguageAsync(
88-
lang.replace(/[-_]/g, (char) => (char === '-' ? '_' : '-')),
89-
true
90-
)
91-
}
92-
if (lang !== options.fallbackLang) {
93-
return loadLanguageAsync(options.fallbackLang)
94-
}
72+
return resolveLangAsync(options.resolve, lang).then(({ default: messages }) =>
73+
applyLanguage(lang, messages, dashLangTry, loadLanguageAsync)
74+
)
75+
}
76+
77+
/**
78+
* Applies the language data and saves it to the loaded storage.
79+
*/
80+
function applyLanguage(
81+
lang: string,
82+
messages: { [key: string]: string },
83+
dashLangTry: boolean = false,
84+
callable: Function
85+
): string {
86+
if (Object.keys(messages).length < 1) {
87+
if (/[-_]/g.test(lang) && !dashLangTry) {
88+
return callable(
89+
lang.replace(/[-_]/g, (char) => (char === '-' ? '_' : '-')),
90+
true
91+
);
92+
}
93+
94+
if (lang !== options.fallbackLang) {
95+
return callable(options.fallbackLang);
9596
}
97+
}
9698

97-
const data: LanguageInterface = { lang, messages }
98-
loaded.push(data)
99-
return setLanguage(data)
100-
})
99+
const data: LanguageInterface = { lang, messages }
100+
loaded.push(data)
101+
102+
return setLanguage(data)
101103
}
102104

103105
/**
@@ -171,6 +173,9 @@ function setLanguage({ lang, messages }: LanguageInterface): string {
171173
return lang
172174
}
173175

176+
/**
177+
* It resolves the language file or data, from direct data, syncrone.
178+
*/
174179
function resolveLang(callable: Function, lang: string, data: { [key: string]: string } = {}): LanguageJsonFileInterface {
175180
if (! Object.keys(data).length) {
176181
data = avoidException(callable, lang)
@@ -192,29 +197,29 @@ function resolveLang(callable: Function, lang: string, data: { [key: string]: st
192197
async function resolveLangAsync(callable: Function, lang: string): Promise<LanguageJsonFileInterface> {
193198
let data = avoidException(callable, lang)
194199

195-
if (data instanceof Promise) {
196-
if (hasPhpTranslations(isServer)) {
197-
const phpLang = await avoidExceptionOnPromise(callable(`php_${lang}`))
198-
const jsonLang = await avoidExceptionOnPromise(data)
199-
200-
return new Promise((resolve) =>
201-
resolve({
202-
default: {
203-
...phpLang,
204-
...jsonLang
205-
}
206-
})
207-
)
208-
}
200+
if (! (data instanceof Promise)) {
201+
return resolveLang(callable, lang, data);
202+
}
203+
204+
if (hasPhpTranslations(isServer)) {
205+
const phpLang = await avoidExceptionOnPromise(callable(`php_${lang}`))
206+
const jsonLang = await avoidExceptionOnPromise(data)
209207

210-
return new Promise(async (resolve) =>
208+
return new Promise((resolve) =>
211209
resolve({
212-
default: await avoidExceptionOnPromise(data)
210+
default: {
211+
...phpLang,
212+
...jsonLang
213+
}
213214
})
214215
)
215216
}
216217

217-
return resolveLang(callable, lang, data);
218+
return new Promise(async (resolve) =>
219+
resolve({
220+
default: await avoidExceptionOnPromise(data)
221+
})
222+
)
218223
}
219224

220225
/**

0 commit comments

Comments
 (0)