Giter VIP home page Giter VIP logo

miniprogram-i18n-plus's Introduction

miniprogram-i18n-plus

Quick Start

npm install miniprogram-i18n-plus -S

example

// app.js
import i18nInstance from "./miniprogram_npm/miniprogram-i18n-plus";

App({
  onLaunch() {
    const locales = {
      zh_CN: {
        test: "测试",
      },
      en_US: {
        test: "Test",
      },
    };

    i18nInstance.setLocale("zh_CN");

    i18nInstance.loadTranslations(locales);
  },
});

Page index

// index.ts

import i18nInstance from "./miniprogram_npm/miniprogram-i18n-plus";

Page({
  onLoad() {
    i18nInstance.effect(this);
  },
});
<view>{{ $language.test }}</view>

webview will be render 测试

API

  • i18nInstance.setLocale(locale: string)

    set current language name. like, en_US,zh_CN

  • i18nInstance.getLocale

    get current language name

  • i18nInstance.loadTranslations(locales: ILocales)

    load translations for i18nInstance, the params like this

      const locales: ILocales = {
        zh_CN: {
          key: "value",
        },
        en_US: {
          key: "value",
        },
      };
      interface ILocales {
        [x: string]: IObject;
      }
    
      interface IObject {
        [x: string]: string;
      }
  • i18nInstance.mergeTranslations(locales: ILocales)

    merge new locales into origin locales.

  • i18nInstance.effect(context: any)

    context is must be this in Page or Component.

    This effect api will set a $language property in this.data

  • i18nInstance.getLanguage()

    get language config object in locales by locale.

  • i18nInstance.toggleLanguage(locale: string)

    toggle language quickly. this api is combine width setLocale and effect

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.