Skip to content

Commit 7a93a22

Browse files
committed
Refactored defaults, LanguageRouter and added middleware handler for NextJS
1 parent 152fa7e commit 7a93a22

1 file changed

Lines changed: 52 additions & 30 deletions

File tree

src/index.ts

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,20 @@ export const DEFAULTS: {
5858
/** Default domain the cookie will be set for (can be null to not set property). */
5959
COOKIE_DOMAIN: string | null,
6060

61-
/** Default cookie setting if cookie should be set containing detected language.
62-
* (otherwise cookie just get read if 'DEFAULTS.COOKIE_NAME' is valid). */
61+
/**
62+
* Default cookie setting if cookie should be set containing detected language.
63+
* (otherwise cookie just get read if 'DEFAULTS.COOKIE_NAME' is valid).
64+
*/
6365
COOKIE_UPDATE: boolean,
6466

6567

6668
/** Name of the output attribute added to the request object that tells which language is requested. */
6769
REQUEST_ADD_LANGUAGE_ATTRIBUTE: string,
6870

69-
/** Name of the output attribute added to the request object that points to a key/value map with the translations in the requested language
70-
* (empty string to disable). */
71+
/**
72+
* Name of the output attribute added to the request object that points to a key/value map with the translations in the requested language
73+
* (empty string to disable).
74+
*/
7175
REQUEST_ADD_TRANSLATIONS_ATTRIBUTE: string,
7276

7377
/** Name of the attribute added to the request object containing the path of the URI without the language prefix and without the query string. */
@@ -449,29 +453,39 @@ type LanguageRouterOptions = {
449453
languages?: string[],
450454

451455

452-
/** Path to next.config.js file from which languages will be loaded from path i18n.locales
453-
* (empty string to disable, if not defined 'DEFAULTS.USE_NEXT_CONFIG_LANGUAGES' will be used). */
456+
/**
457+
* Path to next.config.js file from which languages will be loaded from path i18n.locales
458+
* (empty string to disable, if not defined 'DEFAULTS.USE_NEXT_CONFIG_LANGUAGES' will be used).
459+
*/
454460
useNextConfigLanguages?: string,
455461

456-
/** If supported languages should be automatically loaded from found files in translations directory
457-
* (if not defined 'DEFAULTS.LANGUAGES_FROM_TRANSLATIONS_DIR' will be used). */
462+
/**
463+
* If supported languages should be automatically loaded from found files in translations directory
464+
* (if not defined 'DEFAULTS.LANGUAGES_FROM_TRANSLATIONS_DIR' will be used).
465+
*/
458466
languagesFromTranslationsDir?: boolean,
459467

460-
/** Relative path from project root or absolute path to a directory containing translation files.
468+
/**
469+
* Relative path from project root or absolute path to a directory containing translation files.
461470
* Translation files are json files with the name of a language code e.g. 'en.json'
462-
* (if not defined 'DEFAULTS.TRANSLATIONS_DIR' will be used, if null option is disabled). */
471+
* (if not defined 'DEFAULTS.TRANSLATIONS_DIR' will be used, if null option is disabled).
472+
*/
463473
translationsDir?: string,
464474

465-
/** Order of language detection methods. Entries can be removed if those methods should not be used.
466-
* (if not defined 'DEFAULTS.LANGUAGE_DETECTION_METHODS' will be used). */
475+
/**
476+
* Order of language detection methods. Entries can be removed if those methods should not be used.
477+
* (if not defined 'DEFAULTS.LANGUAGE_DETECTION_METHODS' will be used).
478+
*/
467479
languageDetectionMethods?: LanguageDetectionMethod[],
468480

469481

470482
/** If root document should be redirected to language prefixed root (if not defined 'DEFAULTS.REDIRECT_ROOT' will be used). */
471483
redirectRoot?: boolean,
472484

473-
/** HTTP response code that will be sent if root document gets redirected to language prefixed root
474-
* (if not defined 'DEFAULTS.REDIRECT_ROOT_RESPONSE_CODE' will be used). */
485+
/**
486+
* HTTP response code that will be sent if root document gets redirected to language prefixed root
487+
* (if not defined 'DEFAULTS.REDIRECT_ROOT_RESPONSE_CODE' will be used).
488+
*/
475489
redirectRootResponseCode?: number,
476490

477491

@@ -487,21 +501,29 @@ type LanguageRouterOptions = {
487501
/** Domain the cookie will be set for (can be null to not set property, if not defined 'DEFAULTS.COOKIE_DOMAIN' will be used). */
488502
cookieDomain?: string,
489503

490-
/** If cookie should be set containing detected language (otherwise cookie just get read if 'DEFAULTS.COOKIE_NAME' is valid,
491-
* if not defined 'DEFAULTS.COOKIE_UPDATE' will be used). */
504+
/**
505+
* If cookie should be set containing detected language (otherwise cookie just get read if 'DEFAULTS.COOKIE_NAME' is valid,
506+
* if not defined 'DEFAULTS.COOKIE_UPDATE' will be used).
507+
*/
492508
cookieUpdate?: boolean,
493509

494510

495-
/** Name of the attribute added to the req object that will contain to the detected language string
496-
* (if not defined 'DEFAULTS.REQUEST_ADD_LANGUAGE_ATTRIBUTE' will be used). */
511+
/**
512+
* Name of the attribute added to the req object that will contain to the detected language string
513+
* (if not defined 'DEFAULTS.REQUEST_ADD_LANGUAGE_ATTRIBUTE' will be used).
514+
*/
497515
requestAddLanguageAttribute?: string,
498516

499-
/** Name of the attribute added to the req object that will point to a key/value map with all translations in the detected language
500-
* (empty string to disable, if not defined 'DEFAULTS.REQUEST_ADD_TRANSLATIONS_ATTRIBUTE' will be used). */
517+
/**
518+
* Name of the attribute added to the req object that will point to a key/value map with all translations in the detected language
519+
* (empty string to disable, if not defined 'DEFAULTS.REQUEST_ADD_TRANSLATIONS_ATTRIBUTE' will be used).
520+
*/
501521
requestAddTranslationsAttribute?: string,
502522

503-
/** Name of the attribute added to the req object containing the path of the URI without the language prefix and without the query string
504-
* (if not defined 'DEFAULTS.REQUEST_ADD_PATH_ATTRIBUTE' will be used). */
523+
/**
524+
* Name of the attribute added to the req object containing the path of the URI without the language prefix and without the query string
525+
* (if not defined 'DEFAULTS.REQUEST_ADD_PATH_ATTRIBUTE' will be used).
526+
*/
505527
requestAddPathAttribute?: string,
506528

507529
/** If the locale prefix should be remove from the req.url attribute (if not defined 'DEFAULTS.REQUEST_URL_REMOVE_LANGUAGE_PREFIX' will be used). */
@@ -615,7 +637,7 @@ export const LanguageRouter = (
615637
}
616638

617639

618-
function detectLanguage(uri: string, headers: any): { uri: string, lang: string, path: string } {
640+
function detectLanguage(uri: string, headers: any): { uri: string, lang: string, pathUri: string } {
619641
let lang: string | null = null;
620642
const lowerUri = uri.toLowerCase();
621643
let updatedUri = false;
@@ -657,9 +679,9 @@ export const LanguageRouter = (
657679

658680
lang = (lang || defaultLang).toLowerCase();
659681
uri = updatedUri ? uri : (lowerUri.startsWith('/' + lang) ? uri.substring(lang.length + 1) : (lowerUri.startsWith(lang) ? uri.substring(lang.length) : uri));
660-
let queryIdx = uri.indexOf('?');
682+
const queryIdx = uri.indexOf('?');
661683

662-
return { uri, lang, path: queryIdx>=0 ? uri.substring(0, queryIdx) : uri };
684+
return { uri, lang, pathUri: queryIdx>=0 ? uri.substring(0, queryIdx) : uri };
663685
};
664686

665687

@@ -671,13 +693,13 @@ export const LanguageRouter = (
671693
*/
672694
const nextJsMiddlewareHandler = (req: NextRequest): LanguageNextResponse => {
673695
if(!loadedLangs) preloadSync();
674-
const { uri, lang, path } = detectLanguage(req.url, req.headers);
696+
const { uri, lang, pathUri } = detectLanguage(req.url, req.headers);
675697
const isRoot = req.url.length <= 1;
676698

677699
const response: LanguageNextResponse = {
678700
language: lang,
679701
languages: [...languagesSorted],
680-
path: path,
702+
path: pathUri,
681703
};
682704

683705
// redirect root if not language prefixed
@@ -710,15 +732,15 @@ export const LanguageRouter = (
710732
}
711733

712734
// add path attribute to request object
713-
if(pathAttr) (req as any)[pathAttr] = path;
735+
if(pathAttr) (req as any)[pathAttr] = pathUri;
714736

715737
return response;
716738
};
717739

718740

719741
const expressHandler = async (req: any, res: any, next?: any) => {
720742
if(!loadedLangs) await preload();
721-
const { uri, lang, path } = await detectLanguage(req.url, req.headers);
743+
const { uri, lang, pathUri } = await detectLanguage(req.url, req.headers);
722744
const isRoot = req.url.length <= 1;
723745

724746
// update cookie
@@ -749,7 +771,7 @@ export const LanguageRouter = (
749771
if(translationsAttr) req[translationsAttr] = _getTranslations(lang, defaultLang, [], translationsDir);
750772

751773
// add path attribute to request object
752-
if(pathAttr) req[pathAttr] = path;
774+
if(pathAttr) req[pathAttr] = pathUri;
753775

754776
// remove language prefix from url
755777
if(updateUrlParam) req.url = uri;

0 commit comments

Comments
 (0)