Skip to content

Commit 1e020cd

Browse files
committed
Extended getLanguageNames()
1 parent 22f67be commit 1e020cd

8 files changed

Lines changed: 53 additions & 17 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lup-language",
3-
"version": "2.2.6",
3+
"version": "2.3.0",
44
"description": "Node express middleware for detecting requested language",
55
"files": [
66
"lib/**/*"

src/__tests__/Basic.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import path from 'path';
12
import { ROOT } from 'lup-root';
23
import * as lupLang from '../index';
34

4-
const TRANSLATIONS_DIR = ROOT + '/src/__tests__/translations';
5+
const TRANSLATIONS_DIR = path.join(ROOT, 'src', '__tests__', 'translations');
56

67
test('Loading languages from translations directory', async () => {
78
await expect(lupLang.reloadTranslations(TRANSLATIONS_DIR)).resolves.not.toThrow();
@@ -21,11 +22,23 @@ describe('Checking loaded translations', () => {
2122
});
2223

2324
test('Language names correctly loaded', async () => {
24-
const languageNames = await lupLang.getLanguageNames(TRANSLATIONS_DIR);
25-
expect(languageNames).toBeInstanceOf(Object);
26-
expect(Object.keys(languageNames).length).toBe(2);
27-
expect(languageNames['de']).toEqual('Deutsch');
28-
expect(languageNames['en']).toEqual('English');
25+
const languageNativeNames = await lupLang.getLanguageNames(null, TRANSLATIONS_DIR);
26+
expect(languageNativeNames).toBeInstanceOf(Object);
27+
expect(Object.keys(languageNativeNames).length).toBe(2);
28+
expect(languageNativeNames['de']).toEqual('Deutsch');
29+
expect(languageNativeNames['en']).toEqual('English');
30+
31+
const languageNamesInDE = await lupLang.getLanguageNames('de', TRANSLATIONS_DIR);
32+
expect(languageNamesInDE).toBeInstanceOf(Object);
33+
expect(Object.keys(languageNamesInDE).length).toBe(2);
34+
expect(languageNamesInDE['de']).toEqual('Deutsch');
35+
expect(languageNamesInDE['en']).toEqual('Englisch');
36+
37+
const languageNamesInEN = await lupLang.getLanguageNames('en', TRANSLATIONS_DIR);
38+
expect(languageNamesInEN).toBeInstanceOf(Object);
39+
expect(Object.keys(languageNamesInEN).length).toBe(2);
40+
expect(languageNamesInEN['de']).toEqual('German');
41+
expect(languageNamesInEN['en']).toEqual('English');
2942
});
3043

3144
test('Load single translation that exists', async () => {

src/__tests__/LanguageRouter.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import path from 'path';
12
import { ROOT } from 'lup-root';
23
import { DEFAULTS, getLanguages, LanguageRouter } from '../index';
34

4-
const TRANSLATIONS_DIR = ROOT + '/src/__tests__/translations';
5+
const TRANSLATIONS_DIR = path.join(ROOT, 'src', '__tests__', 'translations');
56

67
var handle: Function | any;
78
var locales: string[];

src/__tests__/translations/de.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"LANGUAGE_NAME": "Deutsch",
3+
"LANGUAGE_NAME_EN": "Englisch",
4+
25
"HelloWorld": "Hallo Welt",
36
"OverwriteMe": "Hab ich gemacht"
47
}

src/__tests__/translations/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"LANGUAGE_NAME": "English",
2+
"LANGUAGE_NAME_DE": "German",
3+
"LANGUAGE_NAME_EN": "English",
4+
35
"HelloWorld": "Hello World",
46
"OverwriteMe": "Did it"
57
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
22
"NAME": "lup-language",
3-
"LANGUAGE_NAME_DE": "Deutsch",
4-
"LANGUAGE_NAME_EN": "Should get overwritten",
53
"OverwriteMe": "Overwrite me"
64
}

src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,24 +390,42 @@ export const checkLanguage = async (
390390
};
391391

392392
/**
393-
* Returns a map of all found languages and their native name.
394-
* Looksup following keys in the translations 'LANGUAGE_NAME_<lang>'.
393+
* Returns a map of all found languages either in their native name or in a specified language.
394+
* Looksup following keys in each translation file:
395+
* - 'LANGUAGE_NAME_<LANG>': Name of the language <LANG> in the current language the file represents e.g. in `en.json` the key `LANGUAGE_NAME_DE` would contain `German`.
396+
* - 'LANGUAGE_NAME': Native name of the language the file represents e.g. in `en.json` the key `LANGUAGE_NAME` would contain `English`.
397+
* @param inLang Language code in which the language names should be returned (if null the native names will be returned).
395398
* @param translationsDir Relative path to directory containing JSON files with translations
396399
* @returns Promise that resolves to a map of language codes and their native names.
397400
*/
398401
export const getLanguageNames = async (
402+
inLang: string | null = null,
399403
translationsDir: string = DEFAULTS.TRANSLATIONS_DIR,
400404
): Promise<{ [key: string]: string }> => {
401405
if (!translationsDir) translationsDir = DEFAULTS.TRANSLATIONS_DIR;
402406
if (!DICTONARY[translationsDir]) await reloadTranslations(translationsDir);
403407

408+
const PREFIX = 'LANGUAGE_NAME';
404409
const dict = DICTONARY[translationsDir];
410+
const langs = LANGUAGES[translationsDir];
405411
const names: { [key: string]: string } = {};
406-
const keyLocale = 'LANGUAGE_NAME';
407412

408-
for (const lang of LANGUAGES[translationsDir]) {
409-
const keyGlobal = 'LANGUAGE_NAME_' + lang.toUpperCase();
410-
names[lang] = dict[lang][keyLocale] || dict[lang][keyGlobal] || lang.toUpperCase();
413+
if (inLang) {
414+
inLang = inLang.trim().toLowerCase();
415+
for (const lang of langs) {
416+
const LANG = lang.toUpperCase();
417+
names[lang] =
418+
dict[inLang][PREFIX + '_' + LANG] ||
419+
(inLang === lang && dict[inLang][PREFIX]) || // name of language in given name
420+
dict[lang][PREFIX + '_' + LANG] ||
421+
dict[lang][PREFIX] || // native name of the language (backup)
422+
LANG; // fallback
423+
}
424+
} else {
425+
for (const lang of langs) {
426+
const LANG = lang.toUpperCase();
427+
names[lang] = dict[lang][PREFIX] || dict[lang][PREFIX + '_' + LANG] || LANG;
428+
}
411429
}
412430
return names;
413431
};

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"moduleResolution": "node",
77
"declaration": true,
88
"strict": true,
9+
"esModuleInterop": true
910
},
1011
"include": ["src"],
1112
"exclude": ["node_modules", "**/__tests__/*"]

0 commit comments

Comments
 (0)