New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

locale-url-solver

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

locale-url-solver

check an url according to rules patterns, returns the locale

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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 = require('locale-url-solver');

Init

Init with a litteral object :

LocaleUrlSolver({
    default: 'en',
    locales: {
        'fr': /^http(s)?:\/\/fr\.[^\/]+($|\/)(.*)$/,
        'de|es': /^http(s)?:\/\/([^\/]*\/LANG($|\/)|LANG\.[^\/]+($|\/)(.*)$)/
    }
});

Solve an url to get the locale

Simple patterns

A key is a locale

LocaleUrlSolver({
    default: 'en',
    locales: {
        'de': /^http(s)?:\/\/[^\/]*((.de)|\/de)($|\/)/,
        'fr': /^http(s)?:\/\/fr\.[^\/]+($|\/)(.*)$/
    }
});

LocaleUrlSolver.solve('http://www.website.com');
>> en
LocaleUrlSolver.solve('http://fr.website.com');
>> fr
LocaleUrlSolver.solve('http://fr.website.com/path/is/long');
>> fr
LocaleUrlSolver.solve('http://www.website.de');
>> de
LocaleUrlSolver.solve('http://www.website.com/de');
>> 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({
    default: 'en',
    locales: {
        'de|fr|es': /^http(s)?:\/\/([^\/]*\/LANG($|\/)|LANG\.[^\/]+($|\/)(.*)$)/
    }
});

LocaleUrlSolver.solve('http://www.website.com/es/path/is/long/');
>> es

It looks for the 'LANG' word, you can change this keyword

LocaleUrlSolver({
    default: 'en',
    locales: {
        'de|fr|es': /^http(s)?:\/\/([^\/]*\/STUFF($|\/)|STUFF\.[^\/]+($|\/)(.*)$)/
    },
    search:'STUFF'
});
Mix simple patterns and grouped patterns
LocaleUrlSolver({
    default: 'en',
    locales: {
        'fr': /^http(s)?:\/\/fr\.[^\/]+($|\/)(.*)$/,
        'de|es': /^http(s)?:\/\/([^\/]*\/LANG($|\/)|LANG\.[^\/]+($|\/)(.*)$)/
    }
});

Set and get default locale

Default locale can be explicitly set

LocaleUrlSolver({
    default: 'en',
    locales: {
        'fr': /^http(s)?:\/\/fr\.[^\/]+($|\/)(.*)$/,
        'de|es': /^http(s)?:\/\/([^\/]*\/LANG($|\/)|LANG\.[^\/]+($|\/)(.*)$)/
    }
});

LocaleUrlSolver.getDefault();
>> en

If not specified, the default locale is the first key

LocaleUrlSolver({
    locales: {
        'fr': /^http(s)?:\/\/fr\.[^\/]+($|\/)(.*)$/,
        'de|es': /^http(s)?:\/\/([^\/]*\/LANG($|\/)|LANG\.[^\/]+($|\/)(.*)$)/
    }
});

LocaleUrlSolver.getDefault();
>> fr

Get the locale keys list

LocaleUrlSolver.getLocales();
>> [ 'de', 'en', 'fr', 'it' ]

Is a locale set in my rules ?

LocaleUrlSolver.isSet('it');
>> 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

Keywords

FAQs

Package last updated on 09 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc