Skip to content

Commit

Permalink
Merge pull request #161 from Hexastack/160-bug-nlu-training-i18n-tran…
Browse files Browse the repository at this point in the history
…slation-issue

fix(frontend): NLU Training I18n translation issue
  • Loading branch information
marrouchi authored Oct 5, 2024
2 parents 9ce1f34 + b36dddd commit 090dcfc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/nlp/components/NlpTrainForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ const NlpDatasetSample: FC<NlpDatasetSampleProps> = ({
>
{!selection?.value
? t("button.select_some_text")
: t("button.add_nlp_entity", { defaultValue: selection.value })}
: t("button.add_nlp_entity", { 0: selection.value })}
</Button>

<Button
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/hooks/useTranslate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import { TOptionsBase } from "i18next";
import { useTranslation } from "react-i18next";

import { TTranslateProps, TTranslationKeys } from "@/i18n/i18n.types";
import {
TTranslateProps,
TTranslationKeys,
TOptionsBaseExtended,
} from "@/i18n/i18n.types";

export const useTranslate = () => {
const { t, i18n } = useTranslation();
Expand All @@ -21,13 +24,13 @@ export const useTranslate = () => {
// full key. For example: prop1 = label.buttons
if (typeof prop1 === "string" && prop1.includes(".")) {
const key = prop1 as TTranslationKeys;
const options = (prop2 || {}) as TOptionsBase;
const options = (prop2 || {}) as TOptionsBaseExtended;

return t(key, { defaultValue: "", ...options });
}

// key with nested key. For example: prop1=label prop2=buttons
const options = (prop3 || {}) as TOptionsBase;
const options = (prop3 || {}) as TOptionsBaseExtended;

return t([prop1, prop2].join(".") as TTranslationKeys, {
defaultValue: "",
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/i18n/i18n.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ export type TTranslationKeys = TFilterNestedKeysOfType<TEnTranslation> &
export type TNestedTranslation<T extends keyof TTranslation> =
TFilterNestedKeysOfType<TTranslation[T]>;

export type TOptionsBaseExtended = TOptionsBase & { 0?: string };

export type TTranslateProps = {
<K extends TTranslationKeys>(prop1: K, prop2?: TOptionsBase & object): string;
<K extends TTranslationKeys>(
prop1: K,
prop2?: TOptionsBaseExtended & object,
): string;
<K extends keyof TTranslation, N = TNestedTranslation<K>>(
prop1: K,
prop2: N & TNestedTranslation<K>,
Expand Down

0 comments on commit 090dcfc

Please sign in to comment.