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

fix: translate news when there's a lang #408

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 10 additions & 1 deletion lib/News.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const WorldstateObject = require('./WorldstateObject');
const updateReg = /(update|hotfix)/i;
const primeAccessReg = /(access)/i;
const streamReg = /(devstream|prime-time|warframeinternational|stream)/i;
const langString = /\/Lotus\/Language\\/i;

/**
* Represents a game news item
Expand All @@ -15,10 +16,11 @@ class News extends WorldstateObject {
* @param {Object} data The news data
* @param {Dependency} deps The dependencies object
* @param {MarkdownSettings} deps.mdConfig The markdown settings
* @param {Translator} deps.translator The string translator
* @param {TimeDateFunctions} deps.timeDate The time and date functions
* @param {locale} deps.locale Locale to use for determining language
*/
constructor(data, { mdConfig, timeDate, locale }) {
constructor(data, { mdConfig, translator, timeDate, locale }) {
super(data, { timeDate });

/**
Expand All @@ -42,6 +44,9 @@ class News extends WorldstateObject {
* @type {string}
*/
this.message = (data.Messages.find((msg) => msg.LanguageCode === locale) || { Message: '' }).Message;
if (langString.test(this.message)) {
this.message = translator.languageString(this.message, locale);
}

/**
* The link to the forum post
Expand Down Expand Up @@ -115,6 +120,10 @@ class News extends WorldstateObject {
*/
this.translations = {};
data.Messages.forEach((message) => {
if (langString.test(message)) {
this.translations[message.LanguageCode] = translator.languageString(message, message.LanguageCode);
}

this.translations[message.LanguageCode] = message.Message;
});

Expand Down
8 changes: 8 additions & 0 deletions test/data/LanguageNews.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"_id": { "$oid": "1234sg" },
"Activation": { "sec": 1 },
"Expiry": { "sec": 3 },
"Messages": [{ "Message": "/Lotus/Language/test/test" }],
"Date": { "sec": 1000 },
"Prop": "https://forums.warframe.com/"
}
11 changes: 7 additions & 4 deletions test/unit/news.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ const timeDate = require('../mocks/timeDate');

const testData = require('../data/News.json');
const realTestData = require('../data/RealNews.json');
const languageTestData = require('../data/LanguageNews.json');

const locale = 'en';

const translator = {
node: () => 'node',
};
const translator = require('../../lib/translation');

describe('News', () => {
describe('#constructor()', () => {
Expand Down Expand Up @@ -151,6 +149,11 @@ describe('News', () => {
n.getTitle('en').should.equal('test');
});

it('Should return a lang when a lang is supplied', () => {
const n = new News(languageTestData, { mdConfig, timeDate, translator });
n.getTitle('en').should.equal('/Lotus/Language/test/test');
});
TobiTenno marked this conversation as resolved.
Show resolved Hide resolved

it('should return the localized message when a translation is present', () => {
const n = new News(realTestData[0], {
mdConfig,
Expand Down