GithubHelp home page GithubHelp logo

lypanov / jquery-localize Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coderifous/jquery-localize

1.0 2.0 1.0 103 KB

a jQuery plugin that makes it easy to internationalize your web site.

JavaScript 100.00%

jquery-localize's Introduction

jquery.localize.js - a jQuery plugin that makes it easy to internationalize your web site.

Synopsis

  • Lazily loads JSON translation files based on a simple naming convention.
  • By default, applies the translations to your document based on simple attribute convention.

Basic Usage

Step 1. Use the "rel" attribute on tags whose content you want to be translated

Somewhere in your html:

<h1 rel="localize[greeting]"> Hello! </h1>

Step 2. Provide a JSON language file that has translations:

example-fr.json:

{
  greeting: "Bonjour!"
}

Step 3. Use the localize plugin.

// In a browser where the language is set to French
$("rel*=localize").localize("example")

// You can also override the language detection, and pass in a language code
$("rel*=localize").localize("example", { language: "fr" })

Gory Details

Language file loading

The first argument of the localize method is the name of the language pack. You might have a different language pack for different parts of your website.

Here's an example of loading several language packs:

$("rel*=localize")
  .localize("header")
  .localize("sidebar")
  .localize("footer")

If the language of the browser were set to "fr", then the plugin would try to load:

  • header-fr.json
  • sidebar-fr.json
  • footer-fr.json

if the language of the browser also had a country code, like "fr-FR", then the plugin would ALSO try to load:

  • header-fr-FR.json
  • sidebar-fr-FR.json
  • footer-fr-FR.json

This let's you define partial language refinements for different regions. For instance, you can have the base language translation file for a language that translates 100 different phrases, and for countries were maybe a some of those phrases would be out of place, you can just provide a country-specific file with just those special phrases defined.

Skipping Languages (aka Optimizing for My Language)

This is useful if you've got a default language. For example, if all of your content is served in english, then you probably don't want the overhead of loading up unecessary (and probably non-existant) english langauge packs (foo-en.json)

You can tell the localize plugin to always skip certain languages using the skipLanguage option:

# using a string will skip ONLY if the language code matches exactly
# this would prevent loading only if the language was "en-US"
$("rel*=localize").localize("example", { skipLanguage: "en-US" })

# using a regex will skip if the regex matches
# this would prevent loading of any english language translations
$("rel*=localize").localize("example", { skipLanguage: /^en/ })

# using an array of strings will skip if any of the strings matches exactly
$("rel*=localize").localize("example", { skipLanguage: ["en", "en-US"] })

Applying the language file

If you rely on the default callback and follow the "rel" attribute conventions then the changes will be applied for you.

Examples:

HTML:

<p rel="localize[title]">Tracker Pro XT Deluxe</p>
<p rel="localize[search.placeholder]">Search...</p>
<p rel="localize[search.button]">Go!</p>
<p rel="localize[footer.disclaimer]">Use at your own risk.</p>
<p rel="localize[menu.dashboard]">Dashboard</p>
<p rel="localize[menu.list]">Bug List</p>
<p rel="localize[menu.logout]">Logout</p>

application-es.json (fake spanish)

{
  title: "Tracker Pro XT Deluxo",
  search: {
    placeholder: "Searcho...",
    button: "Vamos!"
  },
  footer: {
    disclaimer: "Bewaro."
  },
  menu: {
    dashboard: "Dashboardo",
    list: "Bug Listo",
    logout: "Exito"
  }
}

Localize it!

$("rel*=localize").localize("application", { language: "es" })

Callbacks

You can provide a callback if you want to augment or replace the default callback provided by the plugin. Your callback should take at least 1 argument: the language data (contents of your json file). It can optionally accept a second argument, which is a reference to the default callback function. This is handy if you still want the default behavior, but also need to do something else with the language data.

$("rel*=localize").localize("application", { 
  language: "es",
  callback: function(data, defaultCallback){
    data.title = data.title + currentBugName();
    defaultCallback(data)
  }
})

See the tests for working examples.

Credits & Licensing

Copyright (c) Jim Garvin (http://github.com/coderifous), 2008.

Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.

Written by Jim Garvin (@coderifous) for use on LMGTFY.com. Please use it, and contribute changes.

http://github.com/coderifous/jquery-localize

Based off of Keith Wood's Localisation jQuery plugin. http://keith-wood.name/localisation.html

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.