All Projects → signavio → i18n

signavio / i18n

Licence: other
Minimalist gettext style i18n for JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to i18n

mobx-react-intl
A connector between mobx-react and react-intl
Stars: ✭ 32 (+128.57%)
Mutual labels:  i18n, translation, react-intl
gettext-extractor
A flexible and powerful Gettext message extractor with support for JavaScript, TypeScript, JSX and HTML.
Stars: ✭ 82 (+485.71%)
Mutual labels:  i18n, translation, po-files
Frenchkiss.js
The blazing fast lightweight internationalization (i18n) module for javascript
Stars: ✭ 776 (+5442.86%)
Mutual labels:  i18n, translation, interpolation
awrora-starter
Landing page template built with one of most popular javascript library Vue.JS, Vuetify (Material Design) and Nuxt.JS with SSR.
Stars: ✭ 38 (+171.43%)
Mutual labels:  i18n, translation
deepl-api-connector
Connector library for deepl.com rest translation api
Stars: ✭ 12 (-14.29%)
Mutual labels:  i18n, translation
mobility-actiontext
Translate Rails Action Text rich text with Mobility.
Stars: ✭ 27 (+92.86%)
Mutual labels:  i18n, translation
pydantic-i18n
pydantic-i18n is an extension to support an i18n for the pydantic error messages.
Stars: ✭ 32 (+128.57%)
Mutual labels:  i18n, translation
admin-template-for-react
🌏 Admin template for React, React Redux, Redux Saga, React Router, i18n and integrated OAuth login
Stars: ✭ 83 (+492.86%)
Mutual labels:  react-intl, react-i18n
storybook-addon-intl
Addon to provide a locale switcher and react-intl for storybook
Stars: ✭ 84 (+500%)
Mutual labels:  i18n, react-intl
extract-react-intl
Extract react-intl messages
Stars: ✭ 18 (+28.57%)
Mutual labels:  i18n, react-intl
translation
👅 Translations (symfony/translation) to Nette Framework (@nette)
Stars: ✭ 55 (+292.86%)
Mutual labels:  i18n, translation
django-i18nfield
Store internationalized strings in Django models with full forms support
Stars: ✭ 32 (+128.57%)
Mutual labels:  i18n, translation
fluent-vue
Internationalization plugin for Vue.js
Stars: ✭ 137 (+878.57%)
Mutual labels:  i18n, translation
i18n.cr
Internationalization API ( i18n ) for Crystal!
Stars: ✭ 36 (+157.14%)
Mutual labels:  i18n, translation
cldr-engine
Internationalization and localization in Typescript with Unicode CLDR, batteries included
Stars: ✭ 34 (+142.86%)
Mutual labels:  i18n, pluralization
i18n
internationalize projects to Arabic
Stars: ✭ 67 (+378.57%)
Mutual labels:  i18n, translation
django-languages-plus
Provides models and fixtures for working with both common languages and 'culture codes' or locale codes, like pt-BR.
Stars: ✭ 21 (+50%)
Mutual labels:  i18n, translation
msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (+28.57%)
Mutual labels:  i18n, po-files
cakephp-translate
A CakePHP plugin to manage translations of your static content the easy way via web backend.
Stars: ✭ 18 (+28.57%)
Mutual labels:  i18n, po-files
inlang
Open Source Localization Solution for Software.
Stars: ✭ 160 (+1042.86%)
Mutual labels:  i18n, translation

@signavio/i18n

CircleCI npm package

Minimalist gettext style i18n for JavaScript

Features

Installation

yarn add @signavio/i18n

Setup

Add a section like the following to your packages.json:

{
  "scripts": {
    "i18n-init": "cd src/locales && msginit --no-translator --input messages.pot --locale",
    "i18n": "i18n-extract \"src/**/*.js\" src/locales/messages.pot && i18n-merge src/locales/messages.pot src/locales/*.po"
  }
}

Create the file .i18nrc and add a configuration object for gettext message extraction:

{
  "headers": "<POT_HEADERS>",
  "fileName": "<PATH_TO_POT>",
  "baseDirectory": "<PATH_TO_BASEDIR>"
}

More available options are documented here: https://github.com/getsentry/babel-gettext-extractor

Optionally, you can also define your babel configuration in the .i18nrc file. This allows you to ignore your project's .babelrc file when extracting messages, which is helpful if your project is using a legacy version of babel (<6).

{
  "fileName": "<PATH_TO_POT>",
  "babel": {
    "babelrc": false,
    // other babel settings
  }
}

Usage

Add the translations to the PO files, and initialize the i18n module in your application using the init function:

import i18n, { init, setLocale } from '@signavio/i18n'

function getLangLoader(locale) {
  // Lazy load the translation bundles
  return require(`bundle?lazy!json!po!./locales/${locale}.po`)
}

const config = {
  // the default locale to use if the browser preference locale is not available
  default: 'en_US',
  // optional mapping of locales
  map: {
    en: 'en_US',
    de: 'de_DE',
  },
  // optional regular expression pattern for custom interpolation syntax
  interpolationPattern: '__(\\w+)__', // this is the default value
}

init(getLangLoader, config).then(() => {
  // promise will be resolved when the translation bundle for the active locale has been loaded
  alert(i18n('Hello world!'))
  // >> Hello world!

  // switch to another language
  setLocale('de').then(() => {
    alert(i18n('Hello world!'))
    // >> Hallo Welt!
  })
})

Interpolations

Interpolations make it easier to include variable content into messages without confusing translators. For instance, if you want to include a computed number in a message, you can do it like this:

const available = 100
const count = available / 10

i18n('Showing __count__ of __available__ entires.', { count, available })

For your convenience interpolations also support React elements. So you can do things like:

i18n('Contact __supportLink__', {
  supportLink: <a href="mailto:[email protected]">Support</a>,
})

The default syntax for interpolations is a group of characters or numbers (\w+) wrapped in double underscores (__). If you require a different syntax this can be customized using the init option interpolationPattern. Internally, pattern value will be used to create a regular expression for matching interpolation placeholder like this:

new RegExp(interpolationPattern, 'g')

It must contain a capturing group ((\w+)) for capturing the interpolation key.

Pluralization

Often times you get to the situation that the same message needs to look slightly different depending on whether you talk about one or more things. Handling this can add quite a lot of unnecessary code. You can circumvent this with the built in support for pluralizations.

i18n('Showing __count__ item', 'Showing __count__ items', { count })

To use this feature simply pass two different translations to the i18n function. The first string is used for the singular case and the second one for the plural case. Note that you have to hand in a variable called count. This variable is used to decide which version of the translation to choose.

Message context

Sometimes the same translation key can have different meanings based on the context in which is it used. Message context offers a solution to this problem. If you specify the optional context parameter you can have different translations for the same translation key.

i18n('Ok', { context: 'button' })

Markdown

Another convenience of @signavio/i18n is the optional support for markdown in translations. By default this is turned off, but you can activate it by setting the markdown option to true.

i18n('I want _this_ to be **bold**', {
  markdown: true,
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].