Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11430 from openshift-cherrypick-robot/cherry-pick…
…-11348-to-release-4.9 [release-4.9] Bug 2081389: Translate Extensions On Each Language Change
- Loading branch information
Showing
5 changed files
with
97 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
frontend/packages/console-plugin-sdk/src/utils/useTranslatedExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from 'react'; | ||
import { Extension, LoadedExtension } from '@console/dynamic-plugin-sdk/src/types'; | ||
import { isTranslatableString, translateExtensionDeep } from './extension-i18n'; | ||
import useTranslationExt from './useTranslationExt'; | ||
|
||
/** | ||
* `translateExtensionDeep` mutates the extension for translations. We need to store a | ||
* semi-permanent mapping of the translation keys values. | ||
* | ||
* Structured as: { [extension.UID]: { [propertyPathToTranslation]: translationKey } } | ||
*/ | ||
const translationKeyMap: Record<string, Record<string, string>> = {}; | ||
|
||
const useTranslatedExtensions = <E extends Extension>( | ||
extensions: LoadedExtension<E>[], | ||
): typeof extensions => { | ||
const { t } = useTranslationExt(); | ||
|
||
React.useMemo( | ||
// Mutate "extensions" parameter only if changed (i.e. a flag-enabled or translations changed) | ||
() => | ||
extensions.forEach((e) => { | ||
const UID = e.uid; | ||
translateExtensionDeep( | ||
e, | ||
(value, path): value is string => { | ||
let translatableString = value; | ||
if (translationKeyMap[UID]?.[path]) { | ||
translatableString = translationKeyMap[UID][path]; | ||
} | ||
return isTranslatableString(translatableString); | ||
}, | ||
(value, key, obj, path) => { | ||
if (!translationKeyMap[UID]) { | ||
translationKeyMap[UID] = {}; | ||
} | ||
if (!translationKeyMap[UID][path]) { | ||
translationKeyMap[UID][path] = value; | ||
} | ||
// TODO: Fix mutation of extension - mirrors work done in translateExtension | ||
// @see translateExtension() | ||
obj[key] = t(translationKeyMap[UID][path]); | ||
}, | ||
); | ||
}), | ||
[t, extensions], | ||
); | ||
|
||
return extensions; | ||
}; | ||
|
||
export default useTranslatedExtensions; |