Git Product home page Git Product logo

localization-bar's Introduction

Localization Bar

npm

Detect a user's browser language and show a message for the user to view pages in their preferred language.

Installation

npm install localization-bar

Usage

JavaScript

import 'localization-bar'
// or
import LocalizationBar from 'localization-bar'

Import the class into your javascript. You'll then need to pass an object containing the options for the languages you want to use. Each language needs the following options.

  • message - the copy shown in the bar
  • cta - an object that contains
  • text - text displayed for the cta
  • url - url to the translated pages

Here is an example of what the object would look like.

const languageMap = {
    es: {
      message: 'Ver el sitio en',
        cta: {
          text: 'español',
          url: '/es'
      }
  },
  fr: {
      message: 'Voir le site en',
      cta: {
          text: 'français',
          url: '/fr'
      }
  }
};

By default the localization-bar will be appended to the top of the body element. If you would rather insert the bar into another part of the page, simply pass the selector of the element you want to insert before.

You can also override the browser's preferred language setting and pass a preferred language as an parameter when instantiating the class.

When you are ready to add the bar to the page call the check() method.

Example:

const localizationBar = new LocalizationBar({
    es: {
        message: 'Ver el sitio en',
        cta: {
            text: 'español',
            url: '/es'
        }
    },
    fr: {
        message: 'Voir le site en',
        cta: {
            text: 'français',
            url: '/fr'
        }
    }
}, {
    insertSelector: '.wrapper',
    language: 'es'
});

localizationBar.check();

localization-bar's People

Contributors

alejandroperezmartin avatar antonio-laguna avatar jgallen23 avatar mturnwall avatar orthagonal avatar

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.