nceregao/rollup-plugin-gettext-vue(新しいタブで開きます)
主要言語: JavaScript
Is a plugin to translate js, vue files
- star 数
- 1
- watcher 数
- 1
- fork 数
- 2
- issue 数
- 1
- 最終更新
- 2023/08/08
主要言語: JavaScript
Is a plugin to translate js, vue files
Is a plugin to translate js, vue files with gettext. It relies on the gettext-extractor.
npm install rollup-plugin-gettext-vue
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.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.
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', } } });
// 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);
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; });
<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>
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); } };
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"
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'] };
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.