From ac2691d22199e6d535e73ada9a50f4768c493181 Mon Sep 17 00:00:00 2001 From: William Durand Date: Mon, 21 Aug 2017 21:54:01 +0200 Subject: [PATCH 1/2] Add i18n.addonType helper --- src/core/i18n/utils.js | 33 ++++++++++++++++++++++++++++++ tests/unit/core/i18n/test_utils.js | 32 ++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/core/i18n/utils.js b/src/core/i18n/utils.js index 7260b3e1090..b4d9d22f28c 100644 --- a/src/core/i18n/utils.js +++ b/src/core/i18n/utils.js @@ -5,6 +5,15 @@ import Jed from 'jed'; import moment from 'moment'; import log from 'core/logger'; +import { + ADDON_TYPE_COMPLETE_THEME, + ADDON_TYPE_DICT, + ADDON_TYPE_EXTENSION, + ADDON_TYPE_LANG, + ADDON_TYPE_OPENSEARCH, + ADDON_TYPE_THEME, + validAddonTypes, +} from 'core/constants'; const defaultLang = config.get('defaultLang'); const langs = config.get('langs'); @@ -287,6 +296,30 @@ export function makeI18n( oneLineTranslationString(pluralKey), val); }; + // The `addonType` we use internally is not translated, so we use this helper + // to return translated add-on type names. + i18n.addonType = function addonType(type) { + switch (type) { + case ADDON_TYPE_DICT: + return i18n.gettext('dictionary'); + case ADDON_TYPE_LANG: + return i18n.gettext('language pack'); + case ADDON_TYPE_OPENSEARCH: + return i18n.gettext('search plugin'); + case ADDON_TYPE_THEME: + case ADDON_TYPE_COMPLETE_THEME: + return i18n.gettext('theme'); + case ADDON_TYPE_EXTENSION: + return i18n.gettext('extension'); + default: + if (!validAddonTypes.includes(type)) { + throw new Error(`Unknown extension type: ${type}`); + } + log.warn(`Using generic prompt for add-on type: ${type}`); + return i18n.gettext('add-on'); + } + }; + // We add a translated "moment" property to our `i18n` object // to make translated date/time/etc. easy. i18n.moment = moment; diff --git a/tests/unit/core/i18n/test_utils.js b/tests/unit/core/i18n/test_utils.js index 52c51e83c49..a44fb7f3fdc 100644 --- a/tests/unit/core/i18n/test_utils.js +++ b/tests/unit/core/i18n/test_utils.js @@ -3,6 +3,14 @@ import moment from 'moment'; import { oneLine } from 'common-tags'; import * as utils from 'core/i18n/utils'; +import { + ADDON_TYPE_COMPLETE_THEME, + ADDON_TYPE_DICT, + ADDON_TYPE_EXTENSION, + ADDON_TYPE_LANG, + ADDON_TYPE_OPENSEARCH, + ADDON_TYPE_THEME, +} from 'core/constants'; const defaultLang = config.get('defaultLang'); @@ -394,7 +402,10 @@ describe('i18n utils', () => { describe('makeI18n', () => { class FakeJed { constructor(i18nData) { - return i18nData; + return { + ...i18nData, + gettext: (key) => key, + }; } } @@ -475,5 +486,24 @@ describe('i18n utils', () => { sinon.assert.calledWith(toLocaleStringSpy, 'fr'); sinon.assert.notCalled(numberFormatSpy); }); + + describe('addonType()', () => { + it('translates internal addon types', () => { + const i18n = utils.makeI18n({}, 'en', FakeJed); + expect(i18n.addonType(ADDON_TYPE_COMPLETE_THEME)).toEqual('theme'); + expect(i18n.addonType(ADDON_TYPE_DICT)).toEqual('dictionary'); + expect(i18n.addonType(ADDON_TYPE_EXTENSION)).toEqual('extension'); + expect(i18n.addonType(ADDON_TYPE_LANG)).toEqual('language pack'); + expect(i18n.addonType(ADDON_TYPE_OPENSEARCH)).toEqual('search plugin'); + expect(i18n.addonType(ADDON_TYPE_THEME)).toEqual('theme'); + }); + + it('throws an error if the given type is not valid', () => { + const i18n = utils.makeI18n({}, 'en', FakeJed); + expect(() => { + i18n.addonType('unknown-type'); + }).toThrowError(/Unknown extension type/); + }); + }); }); }); From 6877eb9f2db00e4b5984b25d91412e770d7ed73c Mon Sep 17 00:00:00 2001 From: William Durand Date: Mon, 21 Aug 2017 22:25:43 +0200 Subject: [PATCH 2/2] use i18n.addonType() --- src/amo/components/Addon/index.js | 2 +- src/amo/components/RatingManager/index.js | 40 ++++------------------- 2 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/amo/components/Addon/index.js b/src/amo/components/Addon/index.js index 300f27dd49e..7b7295543e8 100644 --- a/src/amo/components/Addon/index.js +++ b/src/amo/components/Addon/index.js @@ -226,7 +226,7 @@ export class AddonBase extends React.Component { return (