Code Monkey home page Code Monkey logo

i18n-client's Introduction

Introduction

The reason is that this package exists is that existing i18n packages in NPM focus on server side rendering, or are bloated with a lot of functionality that is difficult to configure and use. With this package we can roll out only the functions we need. Yet, currently the package is quite dumb:

Configuring your translation strings:

import { i18n } from "i18n-client";
  Meteor.startup(() => { // this line can be omitted if you are not a Meteor user
    i18n.add("en", {
       key: "me",
       deep: {
          in: "tran"
       }
    });
  });

Setting the language

import { i18n } from "i18n-client";
i18n.language = "en"

See below how this can be achieved reactively in Meteor.

Using translations string

import { __ } from "i18n-client";
let t = __("deep.in");

// or

import { i18n } from "i18n-client";
let t = i18n.translate("deep.in");

Reactively setting the language in Meteor.js

In you want setup translations as reactive you need to setup your computation. Therefore, you can use following code:

import { i18n } from "i18n-client";
import { Tracker } from "meteor/tracker";

class ReactiveI18n {
  constructor() {
    this.dep = new Tracker.Dependencyl
  }

  get __() {
    dep.depend();
    return i18n.translate;
  }

  set language(lang) {
    i18n.language = lang;
    dep.changed();
  }
}

When you setup your context, use this new reactive class:

export default context = {
  i18n: new ReactiveI18n();
}

And in your composer pass the i18n function from context (you can setup your own composer that will do this automatically):

compose({i18n}...) {
  onData(null, {
    __: i18n.__
  })
}

Typescript defintion of the package

This is the type definition of the package:

declare module "i18n-client" {
  export interface I18n {
    add(lang: string, translations: Object): void;
    translate(key: string, args?: any): string;
    initTranslator(prefix: string): (key: string) => string;
  }
  export let i18n: I18n;
  export function __(key: string, args?: any): string;
}

i18n-client's People

Contributors

tomitrescak avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.