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

@galtproject/vue-locale

v0.1.10

Published

Plugin for localization for VueJs applications. Allows to add v-locale directive to any html tag for place locale content that bases on current language. Supports hot locales changing and remote json locales content placement.

Downloads

13

Readme

VueJs locale plugin and directive

Plugin for localization for VueJs applications. Allows to add v-locale directive to any html tag for place locale content that bases on current language. Supports hot locales changing and remote json locales content placement.

Usage example

Locales:

{
  "my_container": {
    "label": "Hello <%= username %>",
    "description": "This is <b>awesome</b> locales\n\nTry it!"
  }
}

Component:

export default {
    template: `
    <div class="my-container">
        <span class="label" v-locale="['my_container.label', {username}]"></span>
        <pre class="description" v-locale="'my_container.description'"></pre>
    </div>
    `,
    data() {
        return {
          username: 'Test user'
        }
    }
}

How to use:

  1. Install by npm or yarn:
npm i -s @galtproject/vue-locale

or

yarn add @galtproject/vue-locale
  1. Include plugin to Vue:
import Vue from 'vue';
import * as Vuex from "vuex";

import locale from '@galtproject/vue-locale';

Vue.use(locale.plugin, {Vuex});
  1. Init plugin inside main component created event:
new Vue({
  el: "#app",
  created() {
    this.$locale.init({lang: 'en', url: '/locale/'});
  }
});
  1. Place files with locales JSON to public folder /locale/:

Example of /locale/en.json file:

{
  "my_container": {
    "label": "Hello <%= username %>",
    "description": "This is <b>awesome</b> locales\n\nTry it!"
  }
}
  1. Use v-locale directive in templates
export default {
    template: `
    <div class="my-container">
        <span class="label" v-locale="['my_container.label', {username}]"></span>
        <pre class="description" v-locale="'my_container.description'"></pre>
    </div>
    `,
    data() {
        return {
          username: 'Test user'
        }
    }
}

Using extend option

Plugin supports url or/and extend fields as source for locales. It means that you can include JSON or simple object as locales, or both in cases when you want to extend base locales with additional modules.

this.$locale.init({lang: 'en', url: '/locale/', extend: {
  'en': {
    "another_container": {
      "label": "Hello"
    }
  },
  'es': {
    "another_container": {
      "label": "Hola"
    }
  }
}});

In this case - locales will be loaded from JSON in /locale/ directory from file with current language name(/locale/en.json for example), and then - locales data will be extended with another_container field that placed by current language name field(en for example, all data inside en will be added to data from JSON).

Init method options

| Field | Type | Description | | --- | --- | --- | | lang | string | Default lang. Required. Can be replaced by 'lang' values from localStorage if persist. | | extend | object or array | Object with locales separated by fields: en, ru, etc. Optional (extend or url required). | | url | string or array | Url to locales js folder with locales names by languages (en.json, es.json, etc.). Optional (path or url required). | | cacheBuster | string | Id for adding as parameter to json url. Useful for avoid caching json for new builds of application. |

Api

You can access api methods by plugin object inside every vue component inside your project:

export default {
  name: 'main',
  methods: {
    changeLang(lang) {
      this.$locale.setLang(lang);
    }
  }
}

| Method | Description | | --- | --- | | init(options?) | Initializing library with options that described above. Return promise that resolved on locales ready. | | get(key, options?) | Get locale content by key. Dots in key is separator for access fields of objects. Options - it is object that passes as lodash template variables, and can be accesses inside locales by <%= myVariable %> syntax. | | setLang(lang) | Change current lang. All directives in page will be change contents by current language locales automatically.| | isLoaded() | Returns true if locales is loaded and false if not.| | waitForLoad() | Returns promise that resolves when locale will be loaded(useful for locales by url), or returns empty value immediately if locales already loaded.|

Storage

Storage is available inside vue components by field $localeStore and contains lang, isLoaded and changed fields.

this.$localeStore.state.lang; // string: current language
this.$localeStore.state.isLoaded; // bool: is locale loaded
this.$localeStore.state.changed; // bool: trigger for subscribe to locales changing, useful in watch and compound functions

More examples

Articles:

  1. How add localization toVueJS app