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

Add fallback formatting #637

Merged
merged 7 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class VueI18n {
_missing: ?MissingHandler
_exist: Function
_silentTranslationWarn: boolean
_formatFallbackMessages: boolean
_silentFallbackWarn: boolean
_dateTimeFormatters: Object
_numberFormatters: Object
Expand Down Expand Up @@ -79,6 +80,9 @@ export default class VueI18n {
this._silentTranslationWarn = options.silentTranslationWarn === undefined
? false
: !!options.silentTranslationWarn
this._formatFallbackMessages = options.formatFallbackMessages === undefined
? false
: !!options.formatFallbackMessages
this._silentFallbackWarn = options.silentFallbackWarn === undefined
? false
: !!options.silentFallbackWarn
Expand Down Expand Up @@ -274,6 +278,13 @@ export default class VueI18n {
)
}
}

if (this._formatFallbackMessages) {
const parsedArgs = parseArgs(...values)

return this._render(key, 'string', parsedArgs.params, key)
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/kazupon/vue-i18n/pull/637/files#diff-1fdf421c05c1140f6d71444ea2b27638R266
We need to change from _warnDefault , due to have some behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't exactly understand what you mean. Do you mean we should change the wording here, or?

return key
}

Expand Down
54 changes: 54 additions & 0 deletions test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,60 @@ describe('basic', () => {
})
})

describe('format arguments of fallback', () => {
describe('if activated', () => {
describe('named', () => {
it('should return replaced string', () => {
i18n = new VueI18n({
locale: 'en',
fallbackLocale: 'en',
formatFallbackMessages: true
})

assert.strictEqual(
i18n.t('Hello {name}, how are you?', { name: 'kazupon' }),
'Hello kazupon, how are you?'
)
})
})

describe('list', () => {
it('should return replaced string', () => {
i18n = new VueI18n({
locale: 'en',
fallbackLocale: 'en',
formatFallbackMessages: true
})

assert.strictEqual(
i18n.t('Hello {0}, how are you?', ['kazupon']),
'Hello kazupon, how are you?'
)
})
})
})

describe('if not activated', () => {
describe('named', () => {
it('should not return replaced string', () => {
assert.strictEqual(
i18n.t('Hello {name}, how are you?', { name: 'kazupon' }),
'Hello {name}, how are you?'
)
})
})

describe('list', () => {
it('should not return replaced string', () => {
assert.strictEqual(
i18n.t('Hello {0}, how are you?', ['kazupon']),
'Hello {0}, how are you?'
)
})
})
})
})

describe('locale argument', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.t('message.hello', 'ja'), messages.ja.message.hello)
Expand Down