Skip to content

Commit

Permalink
Add fallback to english translation if translations is missing (#1461)…
Browse files Browse the repository at this point in the history
… (minor)

* Add fallback to english for translations

* Show translation key if no translation is available
  • Loading branch information
Falke-Design authored Mar 24, 2024
1 parent 275e1e7 commit f551fa7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
32 changes: 32 additions & 0 deletions cypress/integration/tooltips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,36 @@ describe('Shows Tooltips', () => {
expect(el).to.have.text('Click first marker to finish');
});
});

it('Add fallback to english for translations', () => {
cy.window().then(({ map, L }) => {
// we set the language to 'custom'
// to make sure that it has no fallback we overwrite the fallback with 'xx'
map.pm.setLang(
'custom',
{
tooltips: {
mytext: 'Some Text',
},
},
'xx'
);

expect(L.PM.Utils.getTranslation('tooltips.mytext')).to.eq('Some Text');
expect(L.PM.Utils.getTranslation('tooltips.placeMarker')).to.eq(
'Click to place marker'
);
});
});

it('shows key if no translation is available', () => {
cy.window().then(({ L }) => {
expect(L.PM.Utils.getTranslation('tooltips.placeMarker')).to.eq(
'Click to place marker'
);
expect(L.PM.Utils.getTranslation('tooltips.mytext')).to.eq(
'tooltips.mytext'
);
});
});
});
11 changes: 3 additions & 8 deletions src/js/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import get from 'lodash/get';
import has from 'lodash/has';
import translations from '../../assets/translations';

export function getTranslation(path) {
let lang = L.PM.activeLang;

if (!has(translations, lang)) {
lang = 'en';
}

return get(translations[lang], path);
const lang = L.PM.activeLang;
// if translation is not found, fallback to english
return get(translations[lang], path) || get(translations.en, path) || path;
}

export function hasValues(list) {
Expand Down

0 comments on commit f551fa7

Please sign in to comment.