Skip to content

Commit

Permalink
ErrorSummary: add translation support (#3245)
Browse files Browse the repository at this point in the history
* ErrorSummary: add translation support

* Update @navikt/core/react/src/form/error-summary/ErrorSummary.tsx

Co-authored-by: Halvor Haugan <[email protected]>

* Update @navikt/core/react/src/util/i18n/locales/nn.ts

Co-authored-by: Halvor Haugan <[email protected]>

* ErrorSummary: fix broken object notation (thanks github conflict webGUI! 😂)

* ErrorSummary: use proper casing for translations object

---------

Co-authored-by: Halvor Haugan <[email protected]>
  • Loading branch information
JulianNymark and HalvorHaugan authored Oct 30, 2024
1 parent 6e49de8 commit bbc06a3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions @navikt/core/react/src/form/error-summary/ErrorSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { HTMLAttributes, forwardRef, useRef } from "react";
import { BodyShort, Heading } from "../../typography";
import { composeEventHandlers } from "../../util/composeEventHandlers";
import { useMergeRefs } from "../../util/hooks";
import { useI18n } from "../../util/i18n/i18n.context";
import ErrorSummaryItem from "./ErrorSummaryItem";

export interface ErrorSummaryProps
Expand Down Expand Up @@ -72,11 +73,12 @@ export const ErrorSummary = forwardRef<HTMLDivElement, ErrorSummaryProps>(
className,
size = "medium",
headingTag = "h2",
heading = "Du må rette disse feilene før du kan fortsette:",
heading,
...rest
},
ref,
) => {
const translate = useI18n("ErrorSummary");
const wrapperRef = useRef<HTMLDivElement>(null);
const headingRef = useRef<HTMLHeadingElement>(null);

Expand Down Expand Up @@ -105,7 +107,7 @@ export const ErrorSummary = forwardRef<HTMLDivElement, ErrorSummaryProps>(
ref={headingRef}
tabIndex={-1}
>
{heading}
{heading ?? translate("heading")}
</Heading>
<BodyShort as="ul" size={size} className="navds-error-summary__list">
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Meta, StoryObj } from "@storybook/react";
import { expect, userEvent, within } from "@storybook/test";
import React, { useRef } from "react";
import { VStack } from "../../layout/stack";
import UNSAFE_AkselLanguageProvider from "../../provider/i18n/LanguageProvider";
import nn from "../../util/i18n/locales/nn";
import { ErrorSummary } from "./ErrorSummary";

export default {
Expand Down Expand Up @@ -134,3 +136,18 @@ export const Chromatic: Story = {
chromatic: { disable: false },
},
};

export const ProvidedTranslations: Story = {
render: () => {
return (
<UNSAFE_AkselLanguageProvider translations={nn}>
<ErrorSummary size="small">
<ErrorSummary.Item href="#1">Checkbox må fylles ut</ErrorSummary.Item>
<ErrorSummary.Item href="#2">
Tekstfeltet må ha en godkjent e-mail
</ErrorSummary.Item>
</ErrorSummary>
</UNSAFE_AkselLanguageProvider>
);
},
};
3 changes: 3 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default {
labelSuffix: "delete",
},
},
ErrorSummary: {
heading: "You must correct the following errors before you can continue:",
},
Loader: {
title: "Waiting…",
},
Expand Down
3 changes: 3 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export default {
labelSuffix: "slett",
},
},
ErrorSummary: {
heading: "Du må rette disse feilene før du kan fortsette:",
},
Loader: {
title: "Venter…",
},
Expand Down
3 changes: 3 additions & 0 deletions @navikt/core/react/src/util/i18n/locales/nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default {
labelSuffix: "slett",
},
},
ErrorSummary: {
heading: "Du må rette desse feila før du kan halde fram:",
},
Loader: {
title: "Ventar…",
},
Expand Down

0 comments on commit bbc06a3

Please sign in to comment.