This plugin helps managing browser languages.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
spec
src
.gitattributes
.gitignore
.travis.yml
CONTRIBUTING.md
Gruntfile.coffee
LICENSE
README.md
bower.json
package.json

README.md

jquery.language Build Status Code Climate

This plugin helps managing browser languages. It looks for an url param or the navigator language and stores the language in a cookie.

Usage

javascript

$.language({
  active: 'auto',
  fallback: 'de',
  available: ['en', 'de']
});

console.log($.language()); //prints de or en

$.language('de') //sets the language

It also exposes the class JQueryLanguage for manual instantiating.

coffee script

$.language
  active: 'auto'
  fallback: 'de'
  available: [
    'en'
    'de'
  ]

console.log $.language() #prints de or en

$.language 'de' #sets the language

It also exposes the class JQueryLanguage for manual instantiating and extending.

Parameter

active

auto - default, means try to detect from url param, cookie, navigator or use fallback
or a available language

fallback

language that is used if no language could be detected

available

array of allowed languages, default is ['en']

urlParam

name of the url param to watch for, default is 'language'

cookieName

name of the cookie for language storing, default is 'language'

Functions

$.language(configuration|language|null)

Initialize the plugin.

  • with a configuration object.
  • or with a language string
  • If no parameter is given it returns the actual language.

$.language.config(configuration|null)

Updates the configuration.

  • calls $.language.autodetectLanguage() and writes result to cookie.
  • if no parameter is given returns the actual configuration

$.language.setLanguage(possibleLanguage)

Normalizes, validates and stores the possibleLanguage if it is different from the actual language.

  • triggers event language.invalid with parameter {invalid: possibleLanguage, active: activeLanguage} if the possibleLang is invalid
  • triggers event language.changed' with parameter {active: activeLanguage, old: oldLanguage}` if language was successfully changed

$.language.autodetectLanguage()

Returns the auto detected language by looking first

  • at the url param with the name given by config.urlParam
  • then look for a cookie with the name config.cookieName
  • then try to get the first language from navigator.language and navigator.userLanguage
  • or finally returns config.fallback

$.language.isValid(language)

  • checks if a language is defined in config.available

$.language.normalize(language)

Normalizes a language string.

  • trim string
  • only use the first to chars
  • return the lower case equivalent

Dependencies

Resources

Authors

Tom Hanoldt

Contributing

Check out the Contributing Guidelines