All Projects → FallenMax → A18n

FallenMax / A18n

Licence: mit
Automated I18n solution for JavaScript/TypeScript/React

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to A18n

Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (-4.92%)
Mutual labels:  i18n, internationalization
Tourism Demo
Flutter app backed by Redux, shows animations, internationalization (i18n), ClipPath, fonts and others...
Stars: ✭ 232 (-4.92%)
Mutual labels:  i18n, internationalization
Jquery.ime
jQuery based input methods library
Stars: ✭ 145 (-40.57%)
Mutual labels:  i18n, internationalization
Dom I18n
Provides a very basic HTML multilingual support using JavaScript
Stars: ✭ 125 (-48.77%)
Mutual labels:  i18n, internationalization
Serge
Continuous localization platform
Stars: ✭ 212 (-13.11%)
Mutual labels:  i18n, internationalization
Jquery I18next
i18next plugin for jquery usage
Stars: ✭ 143 (-41.39%)
Mutual labels:  i18n, internationalization
Node Gettext
A JavaScript implementation of gettext, a localization framework.
Stars: ✭ 175 (-28.28%)
Mutual labels:  i18n, internationalization
Traduora
Ever® Traduora - Open-Source Translation Management Platform
Stars: ✭ 1,580 (+547.54%)
Mutual labels:  internationalization, i18n
I18next Express Middleware
[deprecated] can be replaced with i18next-http-middleware
Stars: ✭ 195 (-20.08%)
Mutual labels:  i18n, internationalization
Elefant
Elefant, the refreshingly simple PHP CMS and web framework.
Stars: ✭ 188 (-22.95%)
Mutual labels:  i18n, internationalization
Vue I18n Extract
Manage vue-i18n localization with static analysis
Stars: ✭ 123 (-49.59%)
Mutual labels:  i18n, internationalization
Weblate
Web based localization tool with tight version control integration.
Stars: ✭ 2,719 (+1014.34%)
Mutual labels:  i18n, internationalization
React Native Localize
🌍 A toolbox for your React Native app localization
Stars: ✭ 1,682 (+589.34%)
Mutual labels:  i18n, internationalization
Date
🗓 A library to help you work with dates in multiple languages, based on Carbon.
Stars: ✭ 1,773 (+626.64%)
Mutual labels:  i18n, internationalization
Phabricator zh hans
Phabricator zh-Hans Translation & Tools.
Stars: ✭ 113 (-53.69%)
Mutual labels:  i18n, internationalization
Formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Stars: ✭ 12,869 (+5174.18%)
Mutual labels:  i18n, internationalization
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (-58.61%)
Mutual labels:  i18n, internationalization
Pseudo Localization
Dynamic pseudo-localization in the browser and nodejs
Stars: ✭ 109 (-55.33%)
Mutual labels:  i18n, internationalization
React Translated
A dead simple way to add complex translations (i18n) in a React (DOM/Native) project 🌎🌍🌏
Stars: ✭ 176 (-27.87%)
Mutual labels:  i18n, internationalization
Vue Inter
Simple yet powerful 1kB i18n library for Vue.js
Stars: ✭ 198 (-18.85%)
Mutual labels:  i18n, internationalization

a18n

npm package build Coverage

English | 中文

Automated I18n solution for JavaScript/TypeScript/React.

This lib wraps and extracts text in js/ts/jsx/tsx files using AST manipulation, making adding I18n support a breeze.

Screen Recoding

Features

  • CLI (for code modification):
    • Wrap texts with translation calls (a18n wrap)

      Note: "Wrap" is only applied to non-ascii texts (e.g. Chinese, Japanese, etc), so texts in English are not supported for now. Discussion is welcomed

    • Extract texts from translation calls (a18n extract)
    • Check for untranslated text in code and resources (a18n check)
    • Support dynamic texts in ES6 Template String
    • Support TypeScript
    • Support React, or any framework that uses JSX
    • Ignore lines or files with annotation comments
    • Preserves original code formatting while modifying code (though prettier is still recommended)
    • Bonus: in case you regret using this lib (please tell us why), you can remove a18n calls (a18n purge)
  • API (for text translation):
    • Translate static and dynamic texts using provided locale resource
    • Tiny (~ 200 loc)
    • Fast, dynamic texts are compiled into template at first run

Getting Started

WARNING: existing project code will be modified, backup or commit before proceed

Install as dependency (not devDependency, as a18n is both a CLI and a runtime)

cd that_legacy_codebase
npm install --save a18n # or: yarn add a18n

Scan and modify code files (.js, .ts, .jsx, .tsx) in src directory, this will wrap non-english text strings with translation calls:

npx a18n wrap src --write

Manually check for unintended modifications and fix them

  • use comment // @a18n-ignore to ignore next line
  • use comment /* @a18n-ignore-file */ to ignore entire file

Extract texts passed to translation calls (will generate zh-CN.json, en.json in ./locales directory):

npx a18n extract src ./locales --locales zh-CN,en

Translate resources under ./locales (e.g. from Chinese to English), after that we should have something like this: (key is added by a18n tool, value is filled by some human translator)

{
  // missing translation, will fallback to original key
  "no-translation": null,

  // static text
  "早上好": "Good morning",

  // dynamic text
  "%s是最好吃的": "pizza is better than %s",
}

Load translation resources and specify language at the start of your application, this must be done BEFORE running any other code

import a18n from 'a18n'
a18n.addLocaleResource('en', require('./en.json'))
a18n.setLocale('en')

// now, a18n() will produce translated result
a18n('早上好') // === "Good morning"

const food = 'A'
a18n`${food}是最好吃的` // === "pizza is better than A"

Documentation

API

a18n(text)

This function can/should be auto-added by a18n wrap command

Translate static text, text should be literal string (instead of variable), example:

a18n('你好') // good
a18n(greeting) // bad, `a18n extract` cannot extract "你好" by analyzing code

a18n`text${variable}`

This function can/should be auto-added by a18n wrap command

Translate dynamic text.

This is an ES6 syntax called Tagged Template Literal

a18n.x`text${variable}`

This function cannot be auto-added, and should be added by user.

Translate dynamic text, returns an array containing translated parts.

This method is useful for displaying mixed content.

const greeting = <div>{a18n.x`Hello ${<strong>Jimmy<strong>}`}</div>
// could evaluate to:
// <div>你好 <strong>Jimmy<strong></div>

a18n.setLocale(locale)

Set locale to use.

This method should be called BEFORE every a18n translation functions are called.

a18n use navigator.language as initial value

a18n.addLocaleResource(locale, resource)

Add resource for specified locale. Resource is usually extracted using a18n extract command.

Example resource:

{
  // missing translation, will fallback to original key
  "no-translation": null,

  // static text
  "早上好": "Good morning",

  // dynamic text
  "%s是最好吃的": "pizza is better than %s"
}

Will merge with existing resource and overwrite values that have same keys.

This method should be called BEFORE every a18n translation functions are called.

(Advanced) a18n.getA18n(namespace)

this method is usually auto added with a18n wrap command, with --namespace option

Get an a18n instance with specified namespace.

Difference namespaces will yield different a18n instances, each with independent locale/resources. Same namespaces will yield same a18n instance, with shared locale/resources.

This API enables:

  • Sharing a18n instance/resources from different parts of system (even from different copies of a18n code)
  • NOT sharing a18n instance/resources with other parts of system (even from same piece of a18n code)

Limitation:

  • Sharing only applies where a18n code are running in the same page and have same reference of globalThis, and cannot share across different browser tabs, web workers, embedded iframes.

See Q & A for more background.

CLI

See: npx a18n --help

Q & A

1. Why is it important to load translation resources and specify a locale **before ** all other code is run?

This can be illustrated with this example:

const s = a18n('apple') // we don't have locale resources for the moment
// so `s` is bound to 'apple', not '苹果' as we intended.

a18n.addLocaleResource('zh-CN', { apple: '苹果' }) // ...too late
a18n.setLocale('zh-CN') // ...too late

console.log(s) // 'apple'

2. When do I need to specify a namespace?

If there are multiple dependencies in the project that further depends on a18n, some bundling tools (webpack) may generate a bundle where they all share a single copy of the a18n code and a single copy of the a18n instance at runtime. Since a18n is a singleton, this may cause unintended locale resources sharing/conflict.

To solve this problem, different dependencies can get its own a18n instances, differentiated by namespace, using getA18n(namespace), and continue to have isolated resources and language configurations. It is also possible to acquire the same a18n instance by specifying same namespaces in order to share language and translation resources.

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