Lisan

Lisan

  • Docs
  • API
  • Try it out
  • Examples
  • GitHub

›Guides & Tips

About

  • Lisan
  • How it works?
  • Performance
  • Lisan Compiler
  • Lisan CLI
  • Lisan Locales
  • Plugins
  • Adapters

Components

  • Translations
  • Dictionary
  • Conditional Groups
  • Formatters

Plugins

  • Localization
  • Loader

Guides & Tips

  • Pluralization
  • JSX Interpolation
  • Multiple Instances
  • Write Plugins

API

  • Lisan
  • Lisan Compiler
  • Lisan CLI
Edit

How to write Lisan plugins?

If you need to add new methods to lisan instance, you can write your custom plugin.

Plugin Function

Lisan plugins passed as an agrument to the lisan.use() method.

The lisan instance will be provided to the plugin function as an argument, after that, you can mutate lisan instance or chance Lisan class prototype.

If you'd like to create a plugin and share it with the community,
please use lisan-plugin-<nameOfThePlugin> naming convention.

Example 1 - Adding alias to for t method

Here we are adding lisan._t() alias for lisan.t() method.

const { lisan } = require('lisan');

const myCustomPlugin = lisanInstance => {
  lisanInstance._t = lisanInstance.t.bind(lisanInstance);
};

lisan.use(myCustomPlugin);

lisan._t('key');

Adding Typescript Definitions

When you write your plugins, typescript compiler will complain about the non-existent methods.

You can add the type definitions as below:

import 'lisan';
import * as TSLisan from 'lisan-types';

declare module 'lisan' {
  interface Lisan {
    _t: TSLisan.TMethod;
  }
}

Example 2 - Logging suspicious entries

Here we are adding some console statements in case there are unwanted behaviour.

const { lisan } = require('lisan');

const myCustomPlugin = lisanInstance => {
  // Cloning the t function
  const t = lisanInstnace.t.bind(lisanInstance);

  // Overwriting the t method:
  lisanInstance.t = (key, placeholders) => {
    const result = t(key, placeholders);

    if (result === key) {
      console.log(`Entry is equal to its key: "${key}"`);
    }

    if (result === '') {
      console.log(`Entry is empty: "${key}"`);
    }

    return result;
  };
};

lisan.use(myCustomPlugin);

lisan.t('aSuspiciousKey');
Last updated on 3/21/2020
← Multiple InstancesLisan →
  • Plugin Function
  • Example 1 - Adding alias to for t method
    • Adding Typescript Definitions
  • Example 2 - Logging suspicious entries
Lisan
Docs
AboutComponentsPluginsGuide & TipsAPI Reference
Ecosystem
LisanLisan LocalesLisan CompilerLisan CLILocalization PluginLoader PluginLisan Types
More
IssuesGitHubSupport Us