updated fluent to 0.8

This commit is contained in:
Danny Coates 2018-09-04 13:30:11 -07:00
parent 59224516b7
commit 7c6aabc388
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 15 additions and 15 deletions

View File

@ -1,4 +1,4 @@
const { MessageContext } = require('fluent'); const { FluentBundle } = require('fluent');
const fs = require('fs'); const fs = require('fs');
function toJSON(map) { function toJSON(map) {
@ -26,10 +26,10 @@ module.exports = function(source) {
require.resolve('../public/locales/en-US/send.ftl'), require.resolve('../public/locales/en-US/send.ftl'),
'utf8' 'utf8'
); );
const en = new MessageContext('en-US'); const en = new FluentBundle('en-US');
en.addMessages(en_ftl); en.addMessages(en_ftl);
// pre-parse the ftl // pre-parse the ftl
const context = new MessageContext(locale); const context = new FluentBundle(locale);
context.addMessages(source); context.addMessages(source);
const merged = merge(en._messages, context._messages); const merged = merge(en._messages, context._messages);
@ -39,14 +39,14 @@ if (typeof window === 'undefined') {
var fluent = require('fluent'); var fluent = require('fluent');
} }
(function () { (function () {
var ctx = new fluent.MessageContext('${locale}', {useIsolating: false}); var bundle = new fluent.FluentBundle('${locale}', {useIsolating: false});
ctx._messages = new Map(${toJSON(merged)}); bundle._messages = new Map(${toJSON(merged)});
function translate(id, data) { function translate(id, data) {
var msg = ctx.getMessage(id); var msg = bundle.getMessage(id);
if (typeof(msg) !== 'string' && !msg.val && msg.attrs) { if (typeof(msg) !== 'string' && !msg.val && msg.attrs) {
msg = msg.attrs.title || msg.attrs.alt msg = msg.attrs.title || msg.attrs.alt
} }
return ctx.format(msg, data); return bundle.format(msg, data);
} }
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
module.exports = translate; module.exports = translate;

View File

@ -14,7 +14,7 @@ The development environment includes all locales in `public/locales` via the `L1
## Code ## Code
In `app/` we use the `state.translate()` function to translate strings to the best matching language base on the user's `Accept-Language` header. It's a wrapper around fluent's [MessageContext.format](http://projectfluent.org/fluent.js/fluent/MessageContext.html). It works the same for both server and client side rendering. In `app/` we use the `state.translate()` function to translate strings to the best matching language base on the user's `Accept-Language` header. It's a wrapper around fluent's [FluentBundle.format](http://projectfluent.org/fluent.js/fluent/FluentBundle.html). It works the same for both server and client side rendering.
### Examples ### Examples

12
package-lock.json generated
View File

@ -2481,7 +2481,7 @@
}, },
"buffer": { "buffer": {
"version": "4.9.1", "version": "4.9.1",
"resolved": "http://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz",
"integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=",
"requires": { "requires": {
"base64-js": "^1.0.2", "base64-js": "^1.0.2",
@ -5351,9 +5351,9 @@
"dev": true "dev": true
}, },
"fluent": { "fluent": {
"version": "0.6.4", "version": "0.8.0",
"resolved": "https://registry.npmjs.org/fluent/-/fluent-0.6.4.tgz", "resolved": "https://registry.npmjs.org/fluent/-/fluent-0.8.0.tgz",
"integrity": "sha512-EXfMJmnGbUgaIC1myIzDk5akAF6+1JrI7KVnNCba2ou7WCKc/2CWa8QshfhImVtettOvEs0z0UVdMrS6zX7pxA==" "integrity": "sha512-bZfthhubEH1lKgGIi0fIDeNkZrfEOu3MrLbi284LdxNG+9Q5gq2KsuoocumqNPStVtWo3S3/1p8RIqd34u3Mzw=="
}, },
"fluent-intl-polyfill": { "fluent-intl-polyfill": {
"version": "0.1.0", "version": "0.1.0",
@ -6282,7 +6282,7 @@
"dependencies": { "dependencies": {
"minimist": { "minimist": {
"version": "1.1.3", "version": "1.1.3",
"resolved": "http://registry.npmjs.org/minimist/-/minimist-1.1.3.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.1.3.tgz",
"integrity": "sha1-O+39kaktOQFvz6ocaB6Pqhoe/ag=", "integrity": "sha1-O+39kaktOQFvz6ocaB6Pqhoe/ag=",
"dev": true "dev": true
} }
@ -13845,7 +13845,7 @@
}, },
"sax": { "sax": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "http://registry.npmjs.org/sax/-/sax-1.2.1.tgz", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz",
"integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o="
}, },
"schema-utils": { "schema-utils": {

View File

@ -126,7 +126,7 @@
"convict": "^4.3.2", "convict": "^4.3.2",
"express": "^4.16.3", "express": "^4.16.3",
"express-ws": "^4.0.0", "express-ws": "^4.0.0",
"fluent": "^0.6.4", "fluent": "^0.8.0",
"fluent-langneg": "^0.1.0", "fluent-langneg": "^0.1.0",
"helmet": "^3.13.0", "helmet": "^3.13.0",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",