Skip to content

Commit

Permalink
Merge pull request #9588 from nucleogenesis/align-common-string-modules
Browse files Browse the repository at this point in the history
Align common string modules
  • Loading branch information
rtibbles authored Aug 5, 2022
2 parents bc3f4c5 + 28de4ee commit c1a06b6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 48 deletions.
15 changes: 15 additions & 0 deletions docs/i18n.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ Then messages are available from the ``$tr`` method on the translator object:
console.log(translator.$tr('helloWorld'));
common*String modules
~~~~~~~~~~~~~~~~~~~~~

A pattern we use in order to avoid having to define the same string across multiple Vue or JS files is to define "common" strings translator. These common translators are typically used within plugins for strings common to that plugin alone. However, there is also a "core" set of common strings available to be used throughout the application.

In order to avoid bloating the common modules, we typically will not add a string we are duplicating to a common module unless it is being used across three or more files.

Common strings modules should typically have the following components:

- A translator created using the ``createTranslator`` function in which strings are defined.
- An exported function that accepts a ``string`` and an ``object`` - which it then passes to the ``$tr()`` function to get a string from the translator in the module.
- An exported Vue mixin that exposes the exported function as a ``method``. This allows Vue components to use the mixin and have the exported function to get a translated string readily at hand easily.


ICU message syntax
~~~~~~~~~~~~~~~~~~

Expand Down
59 changes: 30 additions & 29 deletions kolibri/core/assets/src/mixins/commonCoreStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,41 +1128,42 @@ const MetadataLookup = invert(
METADATA.ResourcesNeededTypes
)
);
/**
* Return translated string for key defined in the coreStrings translator. Will map
* ID keys generated in the kolibri-constants library to their appropriate translations
* if available.
*
* @param {string} key - A key as defined in the coreStrings translator; also accepts keys
* for the object MetadataLookup.
* @param {object} args - An object with keys matching ICU syntax arguments for the translation
* string mapping to the values to be passed for those arguments.
*/
export function coreString(key, args) {
if (key === 'None of the above' || key === METADATA.NoCategories) {
return noneOfTheAboveTranslator.$tr('None of the above', args);
}

export default {
methods: {
/**
* Return translated string for key defined in the coreStrings translator. Will map
* ID keys generated in the kolibri-constants library to their appropriate translations
* if available.
*
* @param {string} key - A key as defined in the coreStrings translator; also accepts keys
* for the object MetadataLookup.
* @param {object} args - An object with keys matching ICU syntax arguments for the translation
* string mapping to the values to be passed for those arguments.
*/
coreString(key, args) {
if (key === 'None of the above' || key === METADATA.NoCategories) {
return noneOfTheAboveTranslator.$tr('None of the above', args);
}
if (key === 'overCertainNumberOfSearchResults') {
return overResultsTranslator.$tr(key, args);
}

if (key === 'overCertainNumberOfSearchResults') {
return overResultsTranslator.$tr(key, args);
}
const metadataKey = get(MetadataLookup, key, null);
key = metadataKey ? camelCase(metadataKey) : key;

const metadataKey = get(MetadataLookup, key, null);
key = metadataKey ? camelCase(metadataKey) : key;
if (nonconformingKeys[key]) {
return coreStrings.$tr(nonconformingKeys[key], args);
}

if (nonconformingKeys[key]) {
return coreStrings.$tr(nonconformingKeys[key], args);
}
if (nonconformingKeys[metadataKey]) {
return coreStrings.$tr(nonconformingKeys[metadataKey], args);
}

if (nonconformingKeys[metadataKey]) {
return coreStrings.$tr(nonconformingKeys[metadataKey], args);
}
return coreStrings.$tr(key, args);
}

return coreStrings.$tr(key, args);
},
export default {
methods: {
coreString,
/**
* Shows a specific snackbar notification from our notificationStrings translator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,17 @@ const MissingContentStrings = createTranslator('MissingContentStrings', {
},
});

function coachString(key, args) {
return coachStrings.$tr(key, args);
}

const coachStringsMixin = {
methods: {
coachString(key, args) {
return coachStrings.$tr(key, args);
},
coachString,
getMissingContentString(key, args) {
return MissingContentStrings.$tr(key, args);
},
},
};

export { coachStrings, coachStringsMixin };
export { coachString, coachStrings, coachStringsMixin };
10 changes: 5 additions & 5 deletions kolibri/plugins/device/assets/src/views/commonDeviceStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const deviceStrings = createTranslator('CommonDeviceStrings', {
},
});

export function deviceString(key, args) {
return deviceStrings.$tr(key, args);
}

export default {
methods: {
deviceString(key, args) {
return deviceStrings.$tr(key, args);
},
},
methods: { deviceString },
};
10 changes: 5 additions & 5 deletions kolibri/plugins/learn/assets/src/views/commonLearnStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export const learnStrings = createTranslator('CommonLearnStrings', {
},
});

export function learnString(key, args) {
return learnStrings.$tr(key, args);
}

export default {
methods: {
learnString(key, args) {
return learnStrings.$tr(key, args);
},
},
methods: { learnString },
};
10 changes: 5 additions & 5 deletions kolibri/plugins/user_auth/assets/src/views/commonUserStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const commonUserStrings = createTranslator('CommonUserPageStrings', {
},
});

export function userString(key, args) {
return commonUserStrings.$tr(key, args);
}

export default {
methods: {
userString(key, args) {
return commonUserStrings.$tr(key, args);
},
},
methods: { userString },
};

0 comments on commit c1a06b6

Please sign in to comment.