Skip to content

Commit

Permalink
Merge branch 'fix-messageformat' of https://github.com/calmonr/i18n-node
Browse files Browse the repository at this point in the history
 into calmonr-fix-messageformat

# Conflicts:
#	package-lock.json
#	package.json
  • Loading branch information
mashpie committed Jan 23, 2022
2 parents 6018b9f + 03b20b5 commit 0faeee0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 59 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ __n('%s cat', 21) // --> 21 кошка
### i18n.__mf()
Supports the advanced MessageFormat as provided by excellent [messageformat module](https://www.npmjs.com/package/messageformat). You should definetly head over to [messageformat.github.io](https://messageformat.github.io) for a guide to MessageFormat. i18n takes care of `new MessageFormat('en').compile(msg);` with the current `msg` loaded from it's json files and cache that complied fn in memory. So in short you might use it similar to `__()` plus extra object to accomplish MessageFormat's formatting. Ok, some examples:
Supports the advanced MessageFormat as provided by excellent [messageformat module](https://www.npmjs.com/package/@messageformat/core). You should definetly head over to [messageformat.github.io](http://messageformat.github.io/messageformat/api/core/) for a guide to MessageFormat. i18n takes care of `new MessageFormat('en').compile(msg);` with the current `msg` loaded from it's json files and cache that complied fn in memory. So in short you might use it similar to `__()` plus extra object to accomplish MessageFormat's formatting. Ok, some examples:
```js
// assume res is set to german
Expand All @@ -571,7 +571,7 @@ res.__mf('Hello {name}', { name: 'Marcus' }) // --> Hallo Marcus
res.__mf('Hello {name}, how was your %s?', 'test', { name: 'Marcus' }) // --> Hallo Marcus, wie war dein test?
// now check out a plural rule
res.__mf('{N, plural, one{# cat} few{# cats} many{# cats} others{# cats}}', {
res.__mf('{N, selectordinal, one{# cat} two{# cats} few{# cats} other{# cats}}', {
N: 1
})
Expand Down Expand Up @@ -612,7 +612,7 @@ But MessageFormat can handle more! You get ability to process:
* Simple Variable Replacement (similar to mustache placeholders)
* SelectFormat (ie. switch msg based on gender)
* PluralFormat (see above and [ranges](#ranged-interval-support))
* [PluralFormat](http://messageformat.github.io/messageformat/guide/#pluralformat) (see above and [ranges](#ranged-interval-support))
Combinations of those give superpower, but should get tested well (contribute your use case, please!) on integration.
Expand Down
4 changes: 1 addition & 3 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var debug = require('debug')('i18n:debug')
var warn = require('debug')('i18n:warn')
var error = require('debug')('i18n:error')
var Mustache = require('mustache')
var Messageformat = require('messageformat')
var Messageformat = require('@messageformat/core')
var MakePlural = require('make-plural')
var parseInterval = require('math-interval-parser').default

Expand Down Expand Up @@ -328,8 +328,6 @@ const i18n = function I18n(_OPTS = false) {
} else {
mf = new Messageformat(targetLocale)

// @see https://messageformat.github.io/messageformat/MessageFormat#disablePluralKeyChecks__anchor
mf.disablePluralKeyChecks()
mf.compiledFunctions = {}
MessageformatInstanceForLocale[targetLocale] = mf
}
Expand Down
96 changes: 65 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"lib": "."
},
"dependencies": {
"@messageformat/core": "^3.0.0",
"debug": "^4.3.3",
"make-plural": "^7.0.0",
"math-interval-parser": "^2.0.1",
"messageformat": "^2.3.0",
"mustache": "^4.2.0",
"sprintf-js": "^1.1.2"
},
Expand Down
25 changes: 11 additions & 14 deletions test/i18n.mf.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ describe('parsing Messageformat phrases', function () {

it('should work with plurals', function () {
var msg = 'In {lang} there {NUM, plural,'
msg += 'zero{are zero for #}'
msg += 'one{is one for #}'
msg += 'two{is two for #}'
msg += 'few{are a few for #}'
msg += 'many{are many for #}'
msg += '=0{are zero for #}'
msg += 'one {is one for #}'
msg += 'other{others for #}}'

mfTest.setLocale('en')
should.equal(
'In english there others for 0',
'In english there are zero for 0',
mfTest.__mf(msg, { NUM: 0, lang: 'english' })
)
should.equal(
Expand Down Expand Up @@ -88,7 +85,7 @@ describe('parsing Messageformat phrases', function () {

mfTest.setLocale('de')
should.equal(
'In german there others for 0',
'In german there are zero for 0',
mfTest.__mf(msg, { NUM: 0, lang: 'german' })
)
should.equal(
Expand Down Expand Up @@ -118,7 +115,7 @@ describe('parsing Messageformat phrases', function () {

mfTest.setLocale('fr')
should.equal(
'In french there is one for 0',
'In french there are zero for 0',
mfTest.__mf(msg, { NUM: 0, lang: 'french' })
)
should.equal(
Expand Down Expand Up @@ -148,31 +145,31 @@ describe('parsing Messageformat phrases', function () {

mfTest.setLocale('ru')
should.equal(
'In russian there are many for 0',
'In russian there are zero for 0',
mfTest.__mf(msg, { NUM: 0, lang: 'russian' })
)
should.equal(
'In russian there is one for 1',
mfTest.__mf(msg, { NUM: 1, lang: 'russian' })
)
should.equal(
'In russian there are a few for 2',
'In russian there others for 2',
mfTest.__mf(msg, { NUM: 2, lang: 'russian' })
)
should.equal(
'In russian there are a few for 3',
'In russian there others for 3',
mfTest.__mf(msg, { NUM: 3, lang: 'russian' })
)
should.equal(
'In russian there are a few for 4',
'In russian there others for 4',
mfTest.__mf(msg, { NUM: 4, lang: 'russian' })
)
should.equal(
'In russian there are many for 5',
'In russian there others for 5',
mfTest.__mf(msg, { NUM: 5, lang: 'russian' })
)
should.equal(
'In russian there are many for 6',
'In russian there others for 6',
mfTest.__mf(msg, { NUM: 6, lang: 'russian' })
)
should.equal(
Expand Down
10 changes: 3 additions & 7 deletions test/i18n.writenewPhrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,9 @@ describe('when i18n gets a new phrase', function () {

it('should add translations with messageformat by use of __mf()', function (done) {
var msg = 'In {language} there {N, plural,'
msg += 'zero{are zero for # }'
msg += 'one{is one for # }'
msg += 'two{is two for # }'
msg += 'few{are a few for # }'
msg += 'many{are many for # }'
msg += 'other{others for # }'
msg += '}'
msg += '=0{are zero for #}'
msg += 'one {is one for #}'
msg += 'other{others for #}}'

// this should just add that string
TestScope.__mf(msg, { N: 1 })
Expand Down

0 comments on commit 0faeee0

Please sign in to comment.