GithubHelp home page GithubHelp logo

mjgreen145 / localise-url Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 160 KB

Automatically localise your URLs based on user's location and language preferences.

License: Apache License 2.0

JavaScript 100.00%

localise-url's Introduction

localise-url Build Status: Linux

Express middleware for localising your URLs based on user's location and language preferences.

Easily redirect unlocalised URLs to localised ones, for example:

www.site.com/path

becomes

www.site.com/gb/en/path

Getting started

Install this modue with the following command:

npm install --save localise-url

Usage

This module is used as middleware for Express. It requires the express cookie-parser middleware to be executed first for each request.

The module checks whether the requested URL is localised (based on the options you provide it), and just moves on to the next middleware if it is. If it isn't, it results in a 302 redirect to a localised URL. This allows you to combine multiple types of path into one block of code:

var localiseUrl = require('localise-url')(options);
app.get(['/path', '/:country/:lang/path'], 
        localiseUrl,
        //more middleware
);

Alternatively, they can be separated:

var localiseUrl = require('localise-url')(options);
app.get('/path', 
        localiseUrl);
        
app.get('/:country/:lang/path', function() {
    // process request
});

Options

separator

Type: String Default: '/'

The country and language parts of the path will be joined using this string.

reverse

Type: Boolean Default: false

Reverse the country and language parts of the URL, so that language comes first.

countryUpper

Type: Boolean Default: false

Makes the country part of the URL uppercase when generated. This does not force the module to match an uppercase country in the URL when seeing if the URL is already localised.

languageUpper

Type: Boolean Default: false

Makes the country part of the URL uppercase when generated. This does not force the module to match an uppercase language in the URL when seeing if the URL is already localised.

countryCookie

Type: String Default: country_iso

The name of the cookie to look for when determining country.

langCookie

Type: String Default: lang_iso

The name of the cookie to look for when determining language.

httpCode

Type: Number Default: 302

The HTTP code to return with the redirect. This must be a number between 300 and 308. Anything else will cause the module to default to a 302 redirect.

Determining Country

The user's country is determined using the following rules:

  1. The value of the 'country_iso' cookie, if it exists
  2. The value of the 'GEO' HTTP header, which should take the form "COUNTRIES:US", for example.
  3. Defaults to 'gb'

Determining Language

The user's language is determined using the following rules:

  1. The value of the 'lang_iso' cookie, if it exists
  2. First supported language found in Accept-Language HTTP header, if present. Supported languages are ['en', 'de', 'fr', 'zh']
  3. Default language for the user's country (as determined above).
  4. Default to 'en'

Copyright

Copyright (c) 2015 Matthew Green. See LICENSE for details.

localise-url's People

Contributors

mjgreen145 avatar

Watchers

James Cloos avatar  avatar  avatar

localise-url's Issues

Options for redirect

Hello Matthew,

Could you please give us the option so we could have a different status code per redirect?
Use case, maybe some redirects are permanent 301, other temporary 302?

Thanks,
Robin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.