New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gettext-translator

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gettext-translator

Javascript gettext translator

  • 3.0.2
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Gettext translator

Javascript gettext translations replacement to use with gettext/gettext. Use gettext/json to generate the json data.

Installation

npm install gettext-translator

Usage

Use the Json generator gettext/json library to export the translations to json:

use Gettext\Loader\PoLoader;
use Gettext\Generator\JsonGenerator;

//Load the po file with the translations
$translations = (new PoLoader())->loadFile('locales/gl.po');

//Export to a json file
(new JsonGenerator())->generateFile($translations, 'locales/gl.json');

Load the json file in your browser

import Translator from 'gettext-translator';

async function getTranslator() {
    const response = await fetch('locales/gl.json');
    const translations = await response.json();

    return new Translator(translations);
}

const t = await getTranslator();

t.gettext('hello world'); //ola mundo

Variables

You can add variables to the translations. For example:

t.gettext('hello :who', {':who': 'world'}); //ola world

There's also a basic support o sprintf (only %s and %d)

t.gettext('hello %s', 'world'); //ola world

To customize the translator formatter, just override the format method:

t.format = function (text, ...args) {
    //Your custom format here
}

Short names

Like in the php version, there are the __ functions that are alias of the long version:

//Both functions does the same

t.gettext('Foo');
t.__('Foo');

API

Long nameShort nameDescription
gettext__Returns a translation
ngettextn__Returns a translation with singular/plural variations
dngettextdn__Returns a translation with domain and singular/plural variations
npgettextnp__Returns a translation with context and singular/plural variations
pgettextp__Returns a translation with a specific context
dgettextd__Returns a translation with a specific domain
dpgettextdp__Returns a translation with a specific domain and context
dnpgettextdnp__Returns a translation with a specific domain, context and singular/plural variations

Keywords

FAQs

Package last updated on 19 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc