Socket
Socket
Sign inDemoInstall

mobx-translate

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-translate

Simple translations library for mobx


Version published
Weekly downloads
4
decreased by-78.95%
Maintainers
1
Weekly downloads
 
Created
Source

MobX Translate

Installation:

npm install mobx-translate --save

Usage

First, create interface for your translations:

export interface TranslationKeys {
  list: {
    HEADING
    MORE
  },
  widget: {
    HELLO
    BYE
  }
}

Then, define your strings. With code completion for keys, and full Mustache template support in values:

import {TranslationKeys} from './translation-keys';

export const EN:TranslationKeys = {
  list: {
    HEADING: 'Available jobs',
    MORE: 'More'
  },
  widget: {
    HELLO: 'Hello {{name}}',
    BYE: 'Bye {{name}}'
  }
}

export const DE:TranslationKeys = {
  list: {
    HEADING: "Verfügbare Berufe",
    MORE: "Mehr"
  },
  widget: {
    HELLO: "Gutten tag {{name}}",
    BYE: "Auf wiedersehn {{name}}"
  }
}

Initialize MobxTranslate, pass your translations interface as a generic:

import {TranslationKeys} from './strings/translation-keys';
import {MobxTranslate} from './mobx-translate';

import {EN} from './strings/en';
import {DE} from './strings/de';

const translateInstance = new MobxTranslate<TranslationKeys>();

translateInstance.loadStrings('EN', EN);
translateInstance.loadStrings('DE', DE);
translateInstance.setLanguage('EN');

export const trans = translateInstance;

And then easily switch languages and translate strings in React components. Again, with code completion for translation keys!:

import * as React from 'react';
import {observer} from 'mobx-react';

import {trans} from '../stores/translate';

@observer
export class Foo extends React.Component<{}, {}> {
  render() {
    return (
    <div className="container">
      <h1>{trans.key.widget.HELLO({name: 'Joe'})}</h1>
      <button onClick={trans.setLanguage.bind(trans,'EN')}>EN</button>
      <button onClick={trans.setLanguage.bind(trans,'DE')}>DE</button>
      <h1>{trans.key.list.HEADING()}</h1>
    </div>)
  }
}

Keywords

FAQs

Package last updated on 13 Sep 2016

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