Giter Club home page Giter Club logo

y18n-browser's Introduction

y18n-browser

Build Status Coverage Status NPM version js-standard-style

The bare-bones internationalization library.

Inspired by i18n. and y18n.

Examples

simple string translation:

var __ = require('y18n').__

console.log(__('my awesome string %s', 'foo'))

output:

my awesome string foo

pluralization support:

var __n = require('y18n').__n

console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'))

output:

2 fishes foo

JSON Language Files

The JSON language files can be stored anywhere as long as you can include them when you instantiate a new Y18N. File names correspond to locales, e.g., en.json, pirate.json.

When strings are observed for the first time they will be added to the JSON file corresponding to the current locale.

Usage

It is recommended to create a helper file/module to handle constructing a new Y18N instance like so:

y18n.js

import Y18N from 'y18n-browser'

export default new Y18N({
	sources: {
		en: require('./path/to/en.json'),
		pirate: require('./path/to/pirate.json'),
	}
})

Create an instance of y18n with the config provided, options include:

  • sources: an object mapping to existing json source files
  • writeLocalUpdates: should newly observed strings be written to local storage, default false.
  • locale: what locale should be used.
  • fallbackToLanguage: should fallback to a language-only file (e.g. en.json) be allowed if a file matching the locale does not exist (e.g. en_US.json), default true.

Methods

import y18n from './my/instance/y18n'

Now you can use your configured instance anywhere.

y18n.__(str, arg, arg, arg)

Print a localized string, %s will be replaced with args.

y18n.__n(singularString, pluralString, count, arg, arg, arg)

Print a localized string with appropriate pluralization. If %d is provided in the string, the count will replace this placeholder.

y18n.setLocale(str)

Set the current locale being used.

y18n.getLocale()

What locale is currently being used?

y18n.updateLocale(obj)

Update the current locale with the key value pairs in obj.

Example locale json file

{
  "Hello": "Hello!",
  "Hello %s %s": "Hello %s %s",
  "%d cat": {
    "one": "%d cat",
    "other": "%d cats"
  },
  "%d %s cat": {
    "one": "%d %s cat",
    "other": "%d %s cats"
  },
  "There is one monkey in the %s": {
    "one": "There is one monkey in the %s",
    "other": "There are %d monkeys in the %s"
  }
}

License

ISC

y18n-browser's People

Contributors

bcoe avatar greenkeeper[bot] avatar greenkeeperio-bot avatar maxrimue avatar nemophrost avatar nexdrew avatar

Stargazers

 avatar

Watchers

 avatar  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.