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

@shellybits/vue-i18next

v0.1.2

Published

i18next integration for Vue 2.x

Downloads

34

Readme

@shellybits/vue-i18next

Why

"Why? There are already two other packages by that name..so?"

We wanted an i18next integration that fulfills these criteria:

  • As small as possible
  • Integrates well with Rails, i.e. make it possible to re-use i18n YML files
  • Load on demand using webpack's code splitting
  • Reactive in Vue

Usage example

For a Rails application with webpack, Vue, code splitting, dynamically loaded locales.

  • Dependencies in package.json

    "@shellybits/vue-i18next": "^0.1.2",
    "i18next": "^8.4.3",
    "yml-loader": "^2.1.0"

    This package has only peer dependencies von Vue and i18next, so you have to include those yourself.

  • In webpack.conf.js:

    ...
    module: {
    	rules: [
    		{
    			test: /\.ya?ml$/,
    			loader: 'yml-loader'
    		},
    
    		...
    	]
    }
    ...
  • In the entry or commons file

    import Vue from 'vue'
    import VueI18Next from '@shellybits/vue-i18next'
    
    VueI18Next(Vue).init({
    	fallbackLng: 'en',
    	interpolation: {
    		prefix: "${",
    		suffix: "}"
    	},
    	backend: {
    		load(language, ns) {
    			// delete if you don't have namespaces
    			if (ns != 'translation')
    				return import(`../../../config/locales/${ns}/${language}.yml`)
    
    			return import(`../../../config/locales/${language}.yml`)
    		}
    	}
    })

    VueI18Next is just a function, exported by this module. It takes Vue as its single argument and returns the i18next, set up with a Promise based backend. This backend needs a load() function supplied that returns a Promise.

    When using the webpack import function, the limitations described in the webpack documentation apply. Namely it's a string, not an expression. During compile time, the above example is basically interpreted as a glob pattern ../../../config/locales/*.yml. For this reason, it's not (easily) possible to use a configuration option pointing at the files.

    An alternative to use webpack code splitting and the yml-loader is to load JSON files by other means like, e.g. using Axios.

  • Translation in Vue components

    In any Vue template, the function i18next.t is available as $t, and it's reactive when the language is changed:

    <p>{{ $t('rails.key') }}</p>

    This kinda implies that in Vue JS code, it's accessible as this.$t.

  • Loading an additional namespace

    Namespaces can be dynamically loaded. Whenever a translation from a namespace is needed:

    ...
    beforeMount() {
    	this.$i18n.loadNamespaces('name')
    }
    ...

    When executed, the backend will automatically fetch the translation (if required) by triggering the supplied load() function.

    This also shows that the i18next instance is available in each Vue instance as $i18n.

License

This project is released under the MIT license.