Skip to content

Commit

Permalink
feat: Add useLocale and useTimeZone (#67)
Browse files Browse the repository at this point in the history
* feat: Add `useLocale` and `useTimeZone`

* Add docs

* Improve wording
  • Loading branch information
amannn committed Nov 24, 2021
1 parent 65194c1 commit 7833f4a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,18 @@ function getMessageFallback({namespace, key, error}) {
<App />
</NextIntlProvider>
```

## Retrieving provider config

As a convenience, two hooks exist that allow to read configuration that was passed to the provider:

```js
// Returns either an explicitly configured locale from the
// provider or if internationalized routing is set up, it
// returns the configured locale from Next.js.
const locale = useLocale();

// Note that this will be `undefined` if no explicit
// `timeZone` was configured on the provider.
const timeZone = useTimeZone();
```
2 changes: 2 additions & 0 deletions packages/use-intl/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export {
RichTranslationValues
} from './TranslationValues';
export {default as useIntl} from './useIntl';
export {default as useLocale} from './useLocale';
export {default as useNow} from './useNow';
export {default as useTimeZone} from './useTimeZone';
export {default as Formats} from './Formats';
export {default as DateTimeFormatOptions} from './DateTimeFormatOptions';
export {default as NumberFormatOptions} from './NumberFormatOptions';
Expand Down
5 changes: 5 additions & 0 deletions packages/use-intl/src/useLocale.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import useIntlContext from './useIntlContext';

export default function useLocale() {
return useIntlContext().locale;
}
5 changes: 5 additions & 0 deletions packages/use-intl/src/useTimeZone.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import useIntlContext from './useIntlContext';

export default function useTimeZone() {
return useIntlContext().timeZone;
}
17 changes: 17 additions & 0 deletions packages/use-intl/test/useLocale.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {render, screen} from '@testing-library/react';
import React from 'react';
import {IntlProvider, useLocale} from '../src';

it('returns the current locale', () => {
function Component() {
return <>{useLocale()}</>;
}

render(
<IntlProvider locale="en">
<Component />
</IntlProvider>
);

screen.getByText('en');
});
31 changes: 31 additions & 0 deletions packages/use-intl/test/useTimeZone.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {render, screen} from '@testing-library/react';
import React from 'react';
import {IntlProvider, useTimeZone} from '../src';

it('returns the time zone when it is configured', () => {
function Component() {
return <>{useTimeZone()}</>;
}

render(
<IntlProvider locale="de" timeZone="Europe/Berlin">
<Component />
</IntlProvider>
);

screen.getByText('Europe/Berlin');
});

it('returns undefined when no time zone is configured', () => {
function Component() {
return <>{useTimeZone()}</>;
}

const {container} = render(
<IntlProvider locale="de">
<Component />
</IntlProvider>
);

expect(container.textContent).toBe('');
});

0 comments on commit 7833f4a

Please sign in to comment.