Skip to content

Commit

Permalink
♻️ Messages til utils (#173)
Browse files Browse the repository at this point in the history
* ♻️ flytt messages til egen fil i utils

* ♻️ importer messages fra utils

* 💩 hello pello

Co-authored-by: Halvor Grizzly Bjørn <[email protected]>
Co-authored-by: Tor Idland <[email protected]>

* 🐛 fjerner test-poop commit som ikke skulle vært her

---------

Co-authored-by: Tor Idland <[email protected]>
Co-authored-by: Halvor Grizzly Bjørn <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2023
1 parent dbd5840 commit 2899f69
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
4 changes: 3 additions & 1 deletion lib/translations/locales.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const SUPPORTED_LOCALE = ['nb', 'nn'];
import { DecoratorLocale } from '@navikt/nav-dekoratoren-moduler';

export const SUPPORTED_LOCALE: DecoratorLocale[] = ['nb', 'nn'];
30 changes: 30 additions & 0 deletions lib/utils/messages.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> {
return Object.keys(nestedMessages).reduce<GenericMessageObject>((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),
};
49 changes: 9 additions & 40 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> {
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) {
Expand All @@ -63,14 +35,11 @@ function MyApp({ Component, pageProps }: AppProps) {
}, []);

return (
<>
{/* @ts-ignore */}
<IntlProvider locale={locale} messages={messages[locale]}>
<NavDecorator>
<Component {...pageProps} />
</NavDecorator>
</IntlProvider>
</>
<IntlProvider locale={locale} messages={messages[locale]}>
<NavDecorator>
<Component {...pageProps} />
</NavDecorator>
</IntlProvider>
);
}

Expand Down

0 comments on commit 2899f69

Please sign in to comment.