Introduction
locale-url-solver is a nodejs module.
It helps you to to get the locale related to your urls. But first you have to specify your own policy.
It can be used :
- server side with your prefered framework
- in the browser if you require('locale-url-solver') it in your javascript blender
Requirements
You have to be familiar with regular expressions
Install
$ npm install locale-url-solver
How to use ?
const LocaleUrlSolver = ;
Init
Init with a litteral object :
;
Solve an url to get the locale
Simple patterns
A key is a locale
; LocaleUrlSolver;>> enLocaleUrlSolver;>> frLocaleUrlSolver;>> frLocaleUrlSolver;>> deLocaleUrlSolver;>> de
Grouped pattern
A key is a group of locales
You may have a global url policy, you can simplify with one pattern
- locale keys are separated by '|'
- the 'LANG' keyword represents a locale
; LocaleUrlSolver;>> es
It looks for the 'LANG' word, you can change this keyword
;
Mix simple patterns and grouped patterns
;
Set and get default locale
Default locale can be explicitly set
; LocaleUrlSolver;>> en
If not specified, the default locale is the first key
; LocaleUrlSolver;>> fr
Get the locale keys list
LocaleUrlSolver;>> 'de' 'en' 'fr' 'it'
Is a locale set in my rules ?
LocaleUrlSolver;>> true
Perfomances
The first match returns the locale. So the order of the rules is important:
- add a default locale rule at the first position.
- order the rules according to your website traffic.
Test coverage
This module is 100% covered by 33 tests