npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dd-i18n

v1.0.5

Published

vue国际化 Vue internationalization 国际化插件 i18n Internationalization plugin

Downloads

28

Readme

dd-i18n

demo

use

npm i dd-i18n

import DI18n from 'dd-i18n';

var di18n = new DI18n({
  // System current language
  local: 'zh-CN',
  // Default language for translation
  defaultLang: 'zh-CN',
  // Language pack
  messages
})

dd-i18n Analysis

  • dl:According to the incoming string, get the corresponding translation. If the language package is not set, return the incoming string.

  • dEvent:Global observer method object

    • dEvent.trigger make an announcement
    this.dEvent.trigger('lang','en');
    • dEvent.listen Subscription message
    this.dEvent.listen('lang',msg => {
         console.log(msg);  // Print en
    });

Language pack format

var messages = {
  'zh-CN': {
      helloWorld: '你好世界',
      youtoo: {
          msg: '你也是'
      },
      this: {
          di18n:{
              trans: '国际化翻译'
          }
      },
      list: {
          today: '今天',
          acquired: '后天',
          time: '时间'
      }
  }, 
  'zh-HK': {
      helloWorld: '你好世界',
      youtoo: {
          msg: '你也是'
      },
      this: {
          di18n:{
              trans: '國際化翻譯'
          }
      },
      list: {
          today: '今天',
          acquired: '後天',
          time: '時間'
      }
  }, 
  'en': {
      helloWorld: 'Hello world',
      youtoo: {
          msg: 'You too'
      },
      this: {
          di18n:{
              trans: 'International translation'
          }
      },
      list: {
          today: 'today',
          acquired: 'acquired',
          time: 'time'
      }
  }
}

js Use in

var di18n = new DI18n({
  // System current language
  local: 'zh-CN',
  // Default language for translation
  defaultLang: 'zh-CN',
  // Language pack
  messages
})
// Set the current system language
di18n.local = 'en'; 
console.log(di18n.dl('你好世界'));//Hello world

Use in vue

main.js

Vue.prototype.$di18n = di18n;
Vue.prototype.dl = di18n.dl;
Vue.prototype.dEvent = di18n.dEvent;

Hello.vue

// Usage in the template
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <div>
      <span>{{dl('国际化翻译')}}</span>
    </div>
    <br><br>
    <ul>
      <li v-for="item in list" :key="item.id">
        {{item.name}}
      </li>
    </ul>

    <div>
      <el-radio-group v-model="lang">
        <el-radio :label="'zh-CN'">中文-简体</el-radio>
        <el-radio :label="'en'">英语</el-radio>
        <el-radio :label="'zh-HK'">中文-繁体</el-radio>
      </el-radio-group>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: this.dl('你好世界'),
      lang: 'zh-CN',
      list: [
        {
          id: 1,
          name: this.dl('今天'),
          flag: true
        },
        {
          id: 2,
          name: this.dl('后天'),
          flag: false
        },
        {
          id:3,
          name: this.dl('时间'),
          flag: true
        }
      ]
    }
  },
  mounted() {
    // Subscription message
    this.dEvent.listen('lang', msg => {
      this.msg = this.dl('你好世界');
      this.list = [
        {
          id: 1,
          name: this.dl('今天'),
          flag: true
        },
        {
          id: 2,
          name: this.dl('后天'),
          flag: false
        },
        {
          id:3,
          name: this.dl('时间'),
          flag: true
        }
      ];
    })
  },
  watch:{
    lang:function(newLang,oldLang) {

      this.$di18n.local = newLang;
      // make an announcement
      this.dEvent.trigger('lang',newLang);
    }
  }
}
</script>


<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>