Skip to content
Open
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
25 changes: 12 additions & 13 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const debug = require('debug')('i18n:debug')
const warn = require('debug')('i18n:warn')
const error = require('debug')('i18n:error')
const Mustache = require('mustache')
const Messageformat = require('@messageformat/core')
const IntlMessageFormat = require('intl-messageformat').default
const MakePlural = require('make-plural')
const parseInterval = require('math-interval-parser').default

Expand All @@ -27,7 +27,7 @@ const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') /
* create constructor function
*/
const i18n = function I18n(_OPTS = false) {
const MessageformatInstanceForLocale = {}
const messageFormatCacheForLocale = new Map()
const PluralsForLocale = {}
let locales = {}
const api = {
Expand Down Expand Up @@ -300,7 +300,7 @@ const i18n = function I18n(_OPTS = false) {
}

i18n.__mf = function i18nMessageformat(phrase) {
let msg, mf, f
let msg, compiledFunctions, f
let targetLocale = defaultLocale
const argv = parseArgv(arguments)
const namedValues = argv[0]
Expand Down Expand Up @@ -328,21 +328,20 @@ const i18n = function I18n(_OPTS = false) {

// now head over to Messageformat
// and try to cache instance
if (MessageformatInstanceForLocale[targetLocale]) {
mf = MessageformatInstanceForLocale[targetLocale]
if (messageFormatCacheForLocale.has(targetLocale)) {
compiledFunctions = messageFormatCacheForLocale.get(targetLocale)
} else {
mf = new Messageformat(targetLocale)

mf.compiledFunctions = {}
MessageformatInstanceForLocale[targetLocale] = mf
compiledFunctions = new Map()
messageFormatCacheForLocale.set(targetLocale, compiledFunctions)
}

// let's try to cache that function
if (mf.compiledFunctions[msg]) {
f = mf.compiledFunctions[msg]
if (compiledFunctions.has(msg)) {
f = compiledFunctions.get(msg)
} else {
f = mf.compile(msg)
mf.compiledFunctions[msg] = f
const format = new IntlMessageFormat(msg, targetLocale)
f = format.format.bind(format)
compiledFunctions.set(msg, f)
}

return postProcess(f(namedValues), namedValues, args)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"lib": "."
},
"dependencies": {
"@messageformat/core": "^3.4.0",
"debug": "^4.4.3",
"fast-printf": "^1.6.10",
"intl-messageformat": "^10.7.18",
"make-plural": "^7.4.0",
"math-interval-parser": "^2.0.1",
"mustache": "^4.2.0"
Expand Down
108 changes: 59 additions & 49 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion test/i18n.writenewPhrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('when i18n gets a new phrase', () => {
msg += 'other{others for #}}'

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

should.deepEqual(getJson('en')[msg], msg)
should.deepEqual(getJson('de')[msg], msg)
Expand Down
Loading