Skip to content

Commit 60d6d9e

Browse files
authored
Remove I18nProvider in favor of GrunnmurenProvider (#746)
* rename I18nProvider to GrunnmurenProvider * Explicity support nb, sv, and en locales. * Add `@react-aria/optimize-locales-plugin` to Storybook
1 parent 1473c35 commit 60d6d9e

File tree

9 files changed

+70
-19
lines changed

9 files changed

+70
-19
lines changed

.changeset/honest-items-tap.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@obosbbl/grunnmuren-react": minor
3+
---
4+
5+
Rename `<I18nProvider />` to `<GrunnmurenProvider />`. Explicitly set supported languages to `nb,`, `sv` and `en`, with `nb` as the default.

.storybook/main.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const path = require('path');
2-
const { mergeConfig } = require('vite');
1+
import { mergeConfig } from 'vite';
2+
import path from 'path';
3+
import optimizeLocales from '@react-aria/optimize-locales-plugin';
34

45
module.exports = {
56
stories: ['../packages/react/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
@@ -22,6 +23,15 @@ module.exports = {
2223
async viteFinal(config) {
2324
// Merge custom configuration into the default config
2425
return mergeConfig(config, {
26+
plugins: [
27+
{
28+
...optimizeLocales.vite({
29+
// Keep only the supported locales
30+
locales: ['nb', 'sv', 'en'],
31+
}),
32+
enforce: 'pre',
33+
},
34+
],
2535
resolve: {
2636
alias: {
2737
'@': path.resolve(__dirname, '../src/'),

.storybook/preview.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from 'react';
22
import { Preview } from '@storybook/react';
3-
import { I18nProvider } from '../packages/react/src/index';
3+
import { GrunnmurenProvider } from '../packages/react/src';
44

55
import './storybook.css';
66

77
const preview: Preview = {
88
decorators: [
99
(Story) => (
10-
<I18nProvider locale="nb">
10+
<GrunnmurenProvider locale="nb">
1111
<Story />
12-
</I18nProvider>
12+
</GrunnmurenProvider>
1313
),
1414
],
1515
};

form-demo/app/layout.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GrunnmurenProvider } from '@obosbbl/grunnmuren-react';
12
import './globals.css';
23

34
export const metadata = {
@@ -9,11 +10,15 @@ export default function RootLayout({
910
}: {
1011
children: React.ReactNode;
1112
}) {
13+
const locale = 'nb';
14+
1215
return (
13-
<html lang="no">
14-
<body>
15-
<main className="container-prose">{children}</main>
16-
</body>
17-
</html>
16+
<GrunnmurenProvider locale={locale}>
17+
<html lang={locale}>
18+
<body>
19+
<main className="container-prose">{children}</main>
20+
</body>
21+
</html>
22+
</GrunnmurenProvider>
1823
);
1924
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"@changesets/cli": "2.27.1",
2121
"@obosbbl/grunnmuren-tailwind": "workspace:*",
22+
"@react-aria/optimize-locales-plugin": "1.0.2",
2223
"@storybook/addon-actions": "7.6.17",
2324
"@storybook/addon-controls": "7.6.17",
2425
"@storybook/addon-docs": "7.6.17",

packages/react/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ npm install @obosbbl/grunnmuren-react@canary
1414
pnpm add @obosbbl/grunnmuren-react@canary
1515
```
1616

17-
## Localization configuration
17+
## Setup
1818

1919
Grunnmuren uses [React Aria Components](https://react-spectrum.adobe.com/react-aria/) under the hood. RAC has built in translation strings for non visible content (for accessibility reasons). It also automatically detects the language based on the browser or system language.
2020

21-
To ensure that the language of the page content matches the accessibility strings you should wrap your application in a `I18nProvider`. This will override RAC's automatic locale selection.
21+
To ensure that the language of the page content matches the accessibility strings you must wrap your application in a `GrunnmurenProvider` with a `locale` prop. This will override RAC's automatic locale selection.
2222

2323
In [Next.js](https://nextjs.org/) you can do this in the root [root layout](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required).
2424

25-
Valid locales for Norwegian are `nb-NO` or `nb`, Swedish is `sv-SE` or `sv`.
25+
Valid locales are `nb`, `sv` or `en`. The provider defaults to `nb` if unspecified.
2626

2727
```js
2828
// app/layout.tsx
29-
import { I18nProvider } from '@obosbbl/grunnmuren-react';
29+
import { GrunnmurenProvider } from '@obosbbl/grunnmuren-react';
3030

3131

3232
export default function RootLayout({
@@ -35,15 +35,15 @@ export default function RootLayout({
3535
children: React.ReactNode
3636
}) {
3737

38-
// Either 'nb' or 'sv'
38+
// Either 'nb', 'sv' or 'en'
3939
const locale = 'nb';
4040

4141
return (
42-
<I18nProvider locale={locale}>
42+
<GrunnmurenProvider locale={locale}>
4343
<html lang={locale}>
4444
<body>{children}</body>
4545
</html>
46-
</I18nProvider>
46+
</GrunnmurenProvider>
4747
)
4848
}
4949
```
@@ -76,7 +76,7 @@ module.exports = {
7676
optimizeLocales.webpack({
7777
// If you have a multitenant app, include both Norwegian and Swedish
7878
// If your app only serves one language, adjust accordingly
79-
locales: ['nb-NO', 'sv-SE'],
79+
locales: ['nb', 'sv'],
8080
}),
8181
);
8282
return config;
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use client';
2+
import { I18nProvider } from 'react-aria-components';
3+
4+
type GrunnmurenProviderProps = {
5+
children: React.ReactNode;
6+
/**
7+
* The locale to apply to the children.
8+
* @default nb
9+
*/
10+
locale?: 'nb' | 'sv' | 'en';
11+
};
12+
13+
function GrunnmurenProvider({
14+
children,
15+
locale = 'nb',
16+
}: GrunnmurenProviderProps) {
17+
return <I18nProvider locale={locale}>{children}</I18nProvider>;
18+
}
19+
20+
export { type GrunnmurenProviderProps, GrunnmurenProvider };

packages/react/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export { Form, I18nProvider } from 'react-aria-components';
1+
export { Form } from 'react-aria-components';
22

3+
export * from './GrunnmurenProvider';
34
export * from './button';
45
export * from './checkbox';
56
export * from './combobox';

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)