Internationalization Library from the Simtopy Project
  • JavaScript 100%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Ewen Maréchal 94487141e5 update badge
2025-09-10 14:49:28 +00:00
conf extract & io renamed to tools 2021-10-10 11:35:59 +02:00
lib v0.4.1 2025-06-02 10:54:55 +02:00
src v0.4.1 2025-06-02 10:54:55 +02:00
tests v0.4.1 2025-06-02 10:54:55 +02:00
.gitignore add a static label function 2021-10-20 09:43:21 +02:00
.gitlab-ci.yml 0.3.1 - with build 2025-04-01 10:37:43 +02:00
.jsbeautifyrc initial commit 2021-10-06 17:54:43 +02:00
index.js version 0.1.7 2023-06-26 15:51:03 +02:00
LICENSE Add LICENSE 2022-08-26 10:45:23 +00:00
package-lock.json 0.0.3 2025-04-01 10:05:31 +02:00
package.json v0.4.1 2025-06-02 10:54:55 +02:00
README.md update badge 2025-09-10 14:49:28 +00:00

Translator

pipeline status coverage report

Features

  • Uses ES6 template literals for string formating.
  • Provides utilities to extract labels from source files
  • Provides utilities to read/write gettext (.po) files
  • No dependencies

Usage example

Extraction from source code

import { extract, writePoFile} from "translator"

// LabelSet extraction
let labelset = extract("path/to/src/")

// LabelSet export

await writePoFile("path/to/loc/en.po", labelset)

Loading from translation files


/**
 * server-side
 */

import { readPoFile } from "translator"

// the labelset can be read with the readPoFile function
let labelset = await readPoFile("path/to/loc/en.po")

// the labelset data can easily be serialized
app.get('/en', (req, res) => res.send(labelset.serialized))

/**
 * client-side
 */

import Translator from "translator"

// the translator can be created from serialized data
let translator = Translator.fromSerializedData(data)

Creating label tags

Sometimes, you want to create a label without returning the translated string. This can be done with ease using the static Translator.label helper function :

import Translator from "translator"

Translator.label `returns a ${label}` // "returns a %0"

Translating strings

// source files should import the Translator
import Translator from "translator"

const localeContext = React.createContext(translator);

// Example in a React functional component
// the translator instance is usually in a provider
function Example(props) {

  const {name, messages, gender} = props

  const tr = useContext(localeContext)

  // formated strings
  const greeting = tr.translate `Hello ${name}` /* inline comment for the translator */

  // pluralization
  const messages = tr.agreeWith(messages) `${helloName}, you have ${messages} new message`

  // custom agreement functions should return a variant index
  function genderAgreement(gender) {
    return gender === "male" ? 1 : 2
  }

  // simply add the agreement function as the second parameter
  const amazing = tr.agreeWith(gender, genderAgreement) `${messages}, you are an amazing ${gender} !`

  return <h1>amazing</h1>;
}

ChangeLog

0.4.1

  • Added an option "template" to the toPoString function
  • Added a writePOTfile function

0.4.0

  • Pluaral forms now uses rules and are handled according to the gettext standard

0.3.3

  • Now exposes a toPoString function in tools

0.3.2

  • Merging labelsets should also override flags

0.3

  • The regex should be used correctly to parse files
  • Adding a unit test for the getFiles function
  • Adding a changelog in the README