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

rollup-plugin-gettext-vue

v1.2.9

Published

Rollup plugin Gettext extractor for Vue components, JS

Downloads

8

Readme

rollup-plugin-gettext-vue

Is a plugin to translate js, vue files with gettext. It relies on the gettext-extractor.

Installation

npm install rollup-plugin-gettext-vue

Configuration rollup config

import vue from 'rollup-plugin-vue';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import requireContext from 'rollup-plugin-require-context';
import gettext from 'rollup-plugin-gettext-vue';

const plugins = [
    resolve(),
    commonjs(),
    requireContext(),
    gettext({
        exclude: 'node_modules/**',
        include: '**/*@(vue|js)',
        languageFiles: 'uk',
        output: './locale/messages.pot',
        language: 'ru',
        translations: 'messages.po',
    }),
    vue({
        template: {
            isProduction: false,
            compilerOptions: { preserveWhitespace: false }
        }
    })
];

export default {
    input: './example/app.js',
    plugins,
    output: {
        globals : { vue: 'Vue' },
        external: ['vue'],
        file: 'example/dist/app.js',
        format: 'iife',
        name: 'WUJS',
    }
};

Options

options.include: '**/*@(vue|js)'

options.exclude: 'node_modules/**'

Patterns of files for filter. Both of which can be a minimatch pattern or an array of minimatch patterns. If options.include is omitted or of zero length, files should be included by default; otherwise they should only be included if the ID matches one of the patterns.

options.languageFiles: 'en'

Default: 'en'. The source language.

options.output: string

Path to pot file with found messages

options.language: 'uk'

Translation language. You can use "uk", "ru", "en"

options.translations: string

Name of file with translations in po format. The file must be in ./locale/uk_UA/LC_MESSAGES/* . uk_UA is the name of the folder that will be obtained from short name options.language.

Configuration rollup with multiple languages

import vue from 'rollup-plugin-vue';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import requireContext from 'rollup-plugin-require-context';

const extractor = require('./dist/index.js');

var locales = ['uk','ru'];

const plugins = (ln) => [
    resolve(),
    commonjs(),
    requireContext(),
    extractor({
        exclude: 'node_modules/**',
        include: '**/*@(vue|js)',
        languageFiles: 'uk',
        language: ln,
        translations: 'messages.po',
    }),
    vue({
        template: {
            isProduction: false,
            compilerOptions: { preserveWhitespace: false }
        }
    })
];

export default locales.map((ln) => {
    return {
        input: './example/app.js',
        plugins: plugins(ln),
        output: {
            globals : { vue: 'Vue' },
            external: ['vue'],
            file: 'example/dist/app.'+ln+'.js',
            format: 'iife',
            name: 'WUJS',
        }
    }
});

Usage example in js files

// formatted strings
const name = _('Mike');
const text = gettext(`Hello ${name}`);

// plurals (works for en locale out of the box)
const n = 5;
const msg = ngettext(`${ n } task left`, `${ n } tasks left`, n);

// context formatted strings
const chelloMike = pgettext('context', `Hello ${name}`);

// context plurals (works for en locale out of the box)
const msg = npgettext('context', `${ n } task left`, `${ n } tasks left`, n);

Basic installation with vue components

For using in vue components files add mixin methods

Vue.use(function(Vue){
    Vue.prototype.$gettext = gettext;
    Vue.prototype.$pgettext = pgettext;
    Vue.prototype.$ngettext = ngettext;
    Vue.prototype.$npgettext = npgettext;
});

Usage example in vue components template

<template>
    <div>
        {{_('Test')}}
        <br>
        {{$gettext('Test')}}
        <br>
        {{$pgettext('context', 'Test')}}
        <br>
        {{$ngettext(`${n} test`, `${n} tests`, n)}}
        <br>
        {{$ngettext('context', `${n} test`, `${n} tests`, n)}}
    </div>
</template>

Usage example in vue components options and methods

export default
{
    mounted() {
        const text = this.$gettext(`Hello ${name}`);

        // plurals (works for en locale out of the box)
        const n = 5;
        const msg = this.$ngettext(`${ n } task left`, `${ n } tasks left`, n);

        // context formatted strings
        const chelloMike = this.$pgettext('context', `Hello ${name}`);

        // context plurals (works for en locale out of the box)
        const msg = this.$npgettext('context', `${ n } task left`, `${ n } tasks left`, n);
    }
};

Create translations po files

You can use the msginit program to create and merge po files.

Create po file

msginit --input=messages.pot --no-translator --locale=uk_UA --output-file=uk_UA/LC_MESSAGES/translations.po

Merge po file

msgmerge -o "uk_UA/LC_MESSAGES/translations.po" "uk_UA/LC_MESSAGES/translations.po" "messages.pot"

Configuration aliases

For usage another aliases of name gettext, pgettext, ngettext, npgettext you can add name of alias in file config.js

const calleeNames = {
    gettext: ['_','$gettext','[this].$gettext'],
    pgettext: ['$pgettext','[this].$pgettext'],
    ngettext: ['$ngettext','[this].$ngettext'],
    npgettext: ['$npgettext', '[this].$npgettext']
};

Compile info

The gettext, ngettext, pgettext, npgettext functions will be added to the resulting bundle. Function calls gettext and pgettext will be replaced to the translated strings in the resulting bundle.