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

BC-6672 fix incorrect translation in alert #3427

Merged
merged 8 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
52 changes: 2 additions & 50 deletions helpers/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const { Configuration } = require('@hpi-schul-cloud/commons');
const logger = require('./logger');
const api = require('../api');
const uk = require('../locales/calendar/uk.json');
const momentHelper = require('./momentHelper');

const i18nDebug = Configuration.get('I18N__DEBUG');
const fallbackLanguage = Configuration.get('I18N__FALLBACK_LANGUAGE');
Expand Down Expand Up @@ -89,57 +89,9 @@ const getInstance = () => (key, options = {}) => i18next.t(key, {
...options,
});

/*
// const CONFIG_ORIG = i18nMoment().locale('en').localeData()._relativeTime;
const relativeTime = {
d: 'a day',
dd: '%d days',
future: 'in %s',
h: 'an hour',
hh: '%d hours',
m: 'a minute',
M: 'a month',
mm: '%d minutes',
MM: '%d months',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
w: 'a week',
ww: '%d weeks',
y: 'a year',
};
*/
const createCustomRelativeTimeConfig = (localFile) => ({
d: `${localFile['moment.relativeTime.aDay']}`,
dd: `%d ${localFile['moment.relativeTime.days']}`,
future: `${localFile['moment.relativeTime.futureIn']} %s`,
h: `${localFile['moment.relativeTime.anHour']}`,
hh: `%d ${localFile['moment.relativeTime.hours']}`,
m: `${localFile['moment.relativeTime.aMinute']}`,
M: `${localFile['moment.relativeTime.aMonths']}`,
mm: `%d ${localFile['moment.relativeTime.minutes']}`,
MM: `%d ${localFile['moment.relativeTime.months']}`,
past: `%s ${localFile['moment.relativeTime.pastAgo']}`,
s: `${localFile['moment.relativeTime.aFewSecondes']}`,
ss: `%d ${localFile['moment.relativeTime.seconds']}`,
w: `${localFile['moment.relativeTime.aWeek']}`,
ww: `%d ${localFile['moment.relativeTime.weeks']}`,
y: `${localFile['moment.relativeTime.aYear']}`,
});

const selectMomentOptions = (langAttribute) => {
const options = {};

if (langAttribute === 'uk') {
options.relativeTime = createCustomRelativeTimeConfig(uk);
}

return options;
};

const changeLanguage = (langAttribute) => {
if (availableLanguages.includes(langAttribute)) {
const momentOptions = selectMomentOptions(langAttribute);
const momentOptions = momentHelper.selectMomentOptions(langAttribute);
i18nMoment.locale(langAttribute, momentOptions);
return i18next.changeLanguage(langAttribute);
}
Expand Down
58 changes: 58 additions & 0 deletions helpers/momentHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const uk = require('../locales/calendar/uk.json');
const es = require('../locales/calendar/es.json');

/*
// const CONFIG_ORIG = i18nMoment().locale('en').localeData()._relativeTime;
const relativeTime = {
d: 'a day',
dd: '%d days',
future: 'in %s',
h: 'an hour',
hh: '%d hours',
m: 'a minute',
M: 'a month',
mm: '%d minutes',
MM: '%d months',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
w: 'a week',
ww: '%d weeks',
y: 'a year',
};
*/

const createCustomRelativeTimeConfig = (localFile) => ({
d: `${localFile['moment.relativeTime.aDay']}`,
dd: `%d ${localFile['moment.relativeTime.days']}`,
future: `${localFile['moment.relativeTime.futureIn']} %s`,
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
h: `${localFile['moment.relativeTime.anHour']}`,
hh: `%d ${localFile['moment.relativeTime.hours']}`,
m: `${localFile['moment.relativeTime.aMinute']}`,
M: `${localFile['moment.relativeTime.aMonths']}`,
mm: `%d ${localFile['moment.relativeTime.minutes']}`,
MM: `%d ${localFile['moment.relativeTime.months']}`,
past: `%s ${localFile['moment.relativeTime.pastAgo']}`,
s: `${localFile['moment.relativeTime.aFewSecondes']}`,
ss: `%d ${localFile['moment.relativeTime.seconds']}`,
w: `${localFile['moment.relativeTime.aWeek']}`,
ww: `%d ${localFile['moment.relativeTime.weeks']}`,
y: `${localFile['moment.relativeTime.aYear']}`,
});

const selectMomentOptions = (langAttribute) => {
const options = {};

if (langAttribute === 'uk') {
options.relativeTime = createCustomRelativeTimeConfig(uk);
}
if (langAttribute === 'es') {
options.relativeTime = createCustomRelativeTimeConfig(es);
}

return options;
};

module.exports = {
selectMomentOptions,
};
17 changes: 17 additions & 0 deletions locales/calendar/es.json
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"moment.relativeTime.aDay": "un día",
"moment.relativeTime.days": "días",
"moment.relativeTime.futureIn": "en",
"moment.relativeTime.anHour": "una hora",
"moment.relativeTime.hours": "horas",
"moment.relativeTime.aMinute": "un minuto",
"moment.relativeTime.aMonths": "un mes",
"moment.relativeTime.minutes": "minutos",
"moment.relativeTime.months": "meses",
"moment.relativeTime.pastAgo": "hace",
"moment.relativeTime.aFewSecondes": "unos segundos",
"moment.relativeTime.seconds": "segundos",
"moment.relativeTime.aWeek": "una semana",
"moment.relativeTime.weeks": "semanas",
"moment.relativeTime.aYear": "un año"
}
5 changes: 5 additions & 0 deletions static/scripts/helpers/AlertMessageController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import moment from 'moment';

const datetime = require('../datetime/datetime');
const momentHelper = require('../../../helpers/momentHelper');

function getIconTag(status) {
switch (status) {
Expand All @@ -22,6 +25,8 @@ class AlertMessageController {
buildMessage(message) {
const icon = getIconTag(message.status);

const getLangAttribute = () => document.querySelector('html').getAttribute('lang');
moment.locale(getLangAttribute(), momentHelper.selectMomentOptions(getLangAttribute()));
const item = document.createElement('div');
if (this.loggedin) {
item.className = 'alert-item';
Expand Down
Loading