Skip to content

Commit

Permalink
⚡ improvement(formatter): interpolate messages without values (#282) by
Browse files Browse the repository at this point in the history
  • Loading branch information
cb8 authored and kazupon committed Jan 24, 2018
1 parent 0e63178 commit b792ce2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ declare interface I18n {
};

declare interface Formatter {
interpolate (message: string, values: any): Array<any>
interpolate (message: string, values?: any): Array<any>
};
3 changes: 3 additions & 0 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default class BaseFormatter {
}

interpolate (message: string, values: any): Array<any> {
if (!values) {
return [message]
}
let tokens: Array<Token> = this._caches[message]
if (!tokens) {
tokens = parse(message)
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default class VueI18n {
ret = this._link(locale, message, ret, host, interpolateMode, values)
}

return !values ? ret : this._render(ret, interpolateMode, values)
return this._render(ret, interpolateMode, values)
}

_link (
Expand Down
16 changes: 16 additions & 0 deletions test/unit/format_custom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ describe('custom formatter', () => {
})
i18n.t('message.hello', 'ja', { name: 'joe' })
})

it('should interpolate messages without values', done => {
class CustomFormatter {
interpolate (message, values) {
assert(values === null)
done()
}
}
const formatter = new CustomFormatter()
const i18n = new VueI18n({
locale: 'en',
messages,
formatter
})
i18n.t('message.hello')
})
})

describe('via vue instance calling', () => {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare namespace VueI18n {
type NumberFormatResult = string;

interface Formatter {
interpolate(message: string, values: Values): any[];
interpolate(message: string, values?: Values): any[];
}

type MissingHandler = (locale: Locale, key: Path, vm?: Vue) => void;
Expand Down

0 comments on commit b792ce2

Please sign in to comment.