From 2899f6926f8ac4cc3182b51dea55dfb2e3cd9cd3 Mon Sep 17 00:00:00 2001 From: tidnav <90849097+tidnav@users.noreply.github.com> Date: Wed, 18 Oct 2023 14:58:31 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Messages=20til=20utils=20(?= =?UTF-8?q?#173)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :recycle: flytt messages til egen fil i utils * :recycle: importer messages fra utils * :poop: hello pello Co-authored-by: Halvor Grizzly Bjørn Co-authored-by: Tor Idland * :bug: fjerner test-poop commit som ikke skulle vært her --------- Co-authored-by: Tor Idland Co-authored-by: Halvor Grizzly Bjørn --- lib/translations/locales.ts | 4 ++- lib/utils/messages.ts | 30 +++++++++++++++++++++++ pages/_app.tsx | 49 +++++++------------------------------ 3 files changed, 42 insertions(+), 41 deletions(-) create mode 100644 lib/utils/messages.ts diff --git a/lib/translations/locales.ts b/lib/translations/locales.ts index 6de6ae84..9aff9070 100644 --- a/lib/translations/locales.ts +++ b/lib/translations/locales.ts @@ -1 +1,3 @@ -export const SUPPORTED_LOCALE = ['nb', 'nn']; +import { DecoratorLocale } from '@navikt/nav-dekoratoren-moduler'; + +export const SUPPORTED_LOCALE: DecoratorLocale[] = ['nb', 'nn']; diff --git a/lib/utils/messages.ts b/lib/utils/messages.ts new file mode 100644 index 00000000..a85154f8 --- /dev/null +++ b/lib/utils/messages.ts @@ -0,0 +1,30 @@ +import messagesNb from 'lib/translations/nb.json'; +import messagesNn from 'lib/translations/nn.json'; +import { DecoratorLocale } from '@navikt/nav-dekoratoren-moduler'; + +type GenericMessageObject = { + [key: string]: any; +}; +function flattenMessages(nestedMessages: GenericMessageObject, prefix = ''): Record { + return Object.keys(nestedMessages).reduce((messages, key) => { + let value = nestedMessages[key]; + let prefixedKey = prefix ? `${prefix}.${key}` : key; + + if (typeof value === 'string') { + messages[prefixedKey] = value; + } else { + Object.assign(messages, flattenMessages(value, prefixedKey)); + } + + return messages; + }, {}); +} + +type Messages = { + [K in DecoratorLocale]?: { [name: string]: string }; +}; + +export const messages: Messages = { + nb: flattenMessages(messagesNb), + nn: flattenMessages(messagesNn), +}; diff --git a/pages/_app.tsx b/pages/_app.tsx index 80e622e7..71d49241 100755 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -3,52 +3,24 @@ import '@navikt/aap-felles-css'; import 'styles/globals.css'; import type { AppProps } from 'next/app'; import { useEffect } from 'react'; -import messagesNb from 'lib/translations/nb.json'; -import messagesNn from 'lib/translations/nn.json'; import { IntlProvider } from 'react-intl'; import { useRouter } from 'next/router'; -import { DecoratorLocale } from '@navikt/nav-dekoratoren-moduler'; import { SUPPORTED_LOCALE } from 'lib/translations/locales'; import { NavDecorator } from 'components/NavDecorator/NavDecorator'; import { initializeFaro } from '@grafana/faro-web-sdk'; +import { messages } from 'lib/utils/messages'; +import { DecoratorLocale } from '@navikt/nav-dekoratoren-moduler'; -function flattenMessages(nestedMessages: object, prefix = ''): Record { - return Object.keys(nestedMessages).reduce((messages, key) => { - // @ts-ignore - let value = nestedMessages[key]; - let prefixedKey = prefix ? `${prefix}.${key}` : key; - - if (typeof value === 'string') { - // @ts-ignore - messages[prefixedKey] = value; - } else { - Object.assign(messages, flattenMessages(value, prefixedKey)); - } - - return messages; - }, {}); -} - -const getLocaleOrFallback = (locale?: string) => { +const getLocaleOrFallback = (locale?: DecoratorLocale) => { if (locale && SUPPORTED_LOCALE.includes(locale)) { return locale; } - return 'nb'; }; -type Messages = { - [K in DecoratorLocale]?: { [name: string]: string }; -}; - -export const messages: Messages = { - nb: flattenMessages(messagesNb), - nn: flattenMessages(messagesNn), -}; - function MyApp({ Component, pageProps }: AppProps) { const router = useRouter(); - const locale = getLocaleOrFallback(router.locale); + const locale = getLocaleOrFallback(router.locale as DecoratorLocale); useEffect(() => { if (process.env.NEXT_PUBLIC_FARO_URL) { @@ -63,14 +35,11 @@ function MyApp({ Component, pageProps }: AppProps) { }, []); return ( - <> - {/* @ts-ignore */} - - - - - - + + + + + ); }