Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/interpolation outside the vue #120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,10 @@ For convenience `translate` can be imported directly in JavaScript files for cas
```js
import {translate} from 'vue-gettext';

const {gettext: $gettext} = translate;
const {gettext: $gettext, gettextInterpolate} = translate;

let str = $gettext('Hello');
const str = $gettext('Hello, %{name}');
const interpolated = gettextInterpolate(str, { name: 'Jerom' })
```

# Elsewhere
Expand Down
2 changes: 1 addition & 1 deletion src/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const MUSTACHE_SYNTAX_RE = /\{\{((?:.|\n)+?)\}\}/g
*/
let interpolate = function (msgid, context = {}, disableHtmlEscaping = false) {

if (!_Vue.config.getTextPluginSilent && MUSTACHE_SYNTAX_RE.test(msgid)) {
if (_Vue && !_Vue.config.getTextPluginSilent && MUSTACHE_SYNTAX_RE.test(msgid)) {
console.warn(`Mustache syntax cannot be used with vue-gettext. Please use "%{}" instead of "{{}}" in: ${msgid}`)
}

Expand Down
14 changes: 14 additions & 0 deletions src/translate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import plurals from './plurals'
import interpolate from './interpolate'

const SPACING_RE = /\s{2,}/g

Expand Down Expand Up @@ -182,6 +183,19 @@ const translate = {
}
},

/**
* Allows to use interpolation outside the Vue
*
* @example
* import {translate} from 'vue-gettext';
*
* const {gettext, gettextInterpolate} = translate;
*
* let translated = gettext('%{ n } foos', n)
* let interpolated = gettextInterpolate(translated, {n: 5})
*/
gettextInterpolate: interpolate.bind(interpolate),

}

export default translate