/i18n

Internationalization packages of galatajs framework

Primary LanguageTypeScriptMIT LicenseMIT


@galatajs/i18n

Internationalization package of galatajs framework.

What Is It?

This package is the i18n package of the galatajs framework.

With this package, you can perform http requests, websocket requests or anything else with i18n support.

Features:

  • %100 support wherever galatajs works. (Websocket, Http, Microservice, Cli and more)
  • Fast and parametric usage
  • Customizable settings
  • %100 support for modular development with dynamic module support.
  • Fully tested, reliable.

Installation

Note: This package is 1st degree dependent on galatajs to work. Please take a look at @galatajs/app first if you haven't.

npm install @galatajs/i18n

or with yarn

yarn add @galatajs/i18n

Basic Usage

import { createApp } from '@galatajs/app';
import { createI18n } from "@galatajs/i18n";

const app = createApp();
const i18n = createI18n();
app.register(i18n);

(async() => {
  await app.start();

  const msg = i18n.translate({
    key: 'hello',
  })
  console.log('msg -> ', msg); // msg -> hello
})()

Usage With Custom Config

The i18n package comes with some default settings that will probably apply to everyone, but you can change this.

Options

type I18nConfigParams = {
  localesDir?: string; // folder path for language files
  separator?: string; // separator to separate each keys
  fallback?: string; // fallback language
  options?: I18nOptions[]; // options for to catch the default language
};

Default Options

const defaultConfig: I18nConfig = {
  fallback: "en",
  localesDir: path.resolve(__dirname, "locales"),
  options: [I18nOptions.AcceptLanguage, I18nOptions.Query, I18nOptions.Cookie],
  separator: ".",
};

Custom Options Example

const i18n = createI18n({
  fallback: 'tr'
})
const i18n = createI18n({
  fallback: 'tr',
  localesDir: path.resolve(__dirname, "i18n"),
})
const i18n = createI18n({
  separator: '_'
})

In this way, examples can be increased.

With Parameters

The i18n package allows you to work parametrically with performance.

Example

json:

// locales/en.json

{
  "greeting": "Hello, {name}!"
}

ts:

import { createApp } from '@galatajs/app';
import { createI18n } from "@galatajs/i18n";

const app = createApp();
const i18n = createI18n();
app.register(i18n);

(async() => {
  await app.start();

  const msg = i18n.translate({
    key: 'greeting',
    params: {
      'name': 'John Doe'
    }
  })
  console.log('msg -> ', msg) // Hello, John Doe!
})()

Load Dynamic Module

The I18n package has been developed for the galatajs framework. galatajs framework supports modular development %100. However, we cannot develop language files modularly in similar modular frameworks. galatajs differs from its competitors here. If you want, you can externally host and load the language files of the product module!

In addition, dynamic loading operations can be performed with this feature. Because no installation is performed until you call the code.

Example

import { createApp } from '@galatajs/app';
import { createI18n } from "@galatajs/i18n";

const app = createApp();
const i18n = createI18n();
app.register(i18n);

(async() => {
  await app.start();

  // product.module.ts
  i18n.loadModule({
      dir: path.resolve(__dirname, "./locales"),
      key: "product",
  })

  const msg = i18n.translate({
    key: 'product.title',
  })
})()