Ghost/ghost/email-service/test/email-helpers.test.js
Cathy Sarisky 1d429b8b09
🌐Added i18n for newsletter strings (#21433)
no issue

This PR adds the ability to translate the strings that appear in the
newsletter as boilerplate text, using i18next.

Variables are in single mustaches ( `{date}` ) in the translation
strings (rather than `{{date}}`), because these strings occur both the
email template.hbs and also .js files. That necessitated a separate
namespace.

This PR also includes changes to the newsletter button ("more like
this", "less like this", "comment") that were previously delivered on
desktop as images that included the text. @sanne-san provided a rework
that removed text-as-image from the desktop buttons, and allows more
shared code between the two layouts, along with making the buttons
translatable.

Example usage - handlebars
```
<h3 class="latest-posts-header">{{t 'Keep reading'}}</h3>

{{{t 'By {authors}' authors=post.authors }}} 
```
(NOTE: triple { required because of possible & )

Example usage - javascript
```
                getValue: (member) => {
                    if (member.status === 'comped') {
                        return t('complimentary');
                    }
                    if (this.isMemberTrialing(member)) {
                        return t('trialing');
                    }
                    // other possible statuses: t('free'), t('paid') //
                    return t(member.status);
                }
```

---------

Co-authored-by: Sanne de Vries <sannedv@protonmail.com>
Co-authored-by: Steve Larson <9larsons@gmail.com>
2024-10-31 08:41:39 -05:00

220 lines
5.5 KiB
JavaScript

const assert = require('assert/strict');
const {registerHelpers} = require('../lib/helpers/register-helpers');
// load the i18n module
const i18nLib = require('@tryghost/i18n');
const i18n = i18nLib('fr', 'newsletter');
const t = (key, options) => {
return i18n.t(key, options);
};
describe('registerHelpers', function () {
it('registers helpers', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
assert.ok(handlebars.if);
assert.ok(handlebars.and);
assert.ok(handlebars.not);
assert.ok(handlebars.or);
assert.ok(handlebars.hasFeature);
assert.ok(handlebars.t);
});
it('if helper returns true', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.if(true, {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, true);
});
it('if helper returns false', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.if(false, {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, false);
});
it('and helper returns true', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.and(true, true);
assert.equal(result, true);
});
it('hasFeature helper returns true', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return true;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.hasFeature('test', {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, true);
});
it('hasFeature helper returns false', function () {
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
const labs = {
isSet: function () {
return false;
}
};
registerHelpers(handlebars, labs);
const result = handlebars.hasFeature('test', {
fn: function () {
return true;
},
inverse: function () {
return false;
}
});
assert.equal(result, false);
});
it('t helper returns key', function () {
const labs = {
isSet: function () {
return false;
}
};
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
registerHelpers(handlebars, labs, t);
const result = handlebars.t('test');
assert.equal(result, 'test');
});
it('t helper returns translation', function () {
const labs = {
isSet: function () {
return false;
}
};
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
registerHelpers(handlebars, labs, t);
const result = handlebars.t('Name');
assert.equal(result, 'Nom');
});
it('t helper returns translation with hash', function () {
const labs = {
isSet: function () {
return false;
}
};
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
registerHelpers(handlebars, labs, t);
const result = handlebars.t('By {authors}', {hash: {authors: 'fred'}});
assert.equal(result, 'Par fred');
});
it('t helper returns translation with options', function () {
const labs = {
isSet: function () {
return false;
}
};
const handlebars = {
registerHelper: function (name, fn) {
this[name] = fn;
}
};
registerHelpers(handlebars, labs, t);
const result = handlebars.t('By {authors}', {authors: 'fred'});
assert.equal(result, 'Par fred');
});
});