Skip to content

Commit

Permalink
feat(react-dictionary): add DictionaryProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Sep 26, 2023
1 parent e11e7fe commit 2953b14
Show file tree
Hide file tree
Showing 17 changed files with 394 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-news-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/react-dictionary": minor
---

experimental release
217 changes: 188 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/react-dictionary/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.turbo
coverage
dist
node_modules
9 changes: 9 additions & 0 deletions packages/react-dictionary/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2022 Code d'Azur Interactive B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions packages/react-dictionary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @codedazur/react-dictionary
42 changes: 42 additions & 0 deletions packages/react-dictionary/components/DictionaryProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FunctionComponent, ReactNode, useMemo } from "react";
import { Dictionary, dictionaryContext } from "../contexts/dictionaryContext";
import { DictionaryKey, Locale } from "@codedazur/react-dictionary";

export interface DictionaryProviderProps {
locale?: Locale;
dictionaries: Record<Locale, Record<DictionaryKey, string>>;
children: ReactNode;
}

export const DictionaryProvider: FunctionComponent<DictionaryProviderProps> = ({
locale = Locale.en_US,
dictionaries,
children,
}) => {
const map = useMemo(
() =>
new Map<Locale, Dictionary>(
Object.entries(dictionaries).map(([dictionaryLocale, translations]) => [
dictionaryLocale,
new Map<DictionaryKey, string>(
Object.entries(translations) as unknown as [
DictionaryKey,
string,
][],
),
]) as [Locale, Dictionary][],
),
[dictionaries],
);

return (
<dictionaryContext.Provider
value={{
locale,
entries: map,
}}
>
{children}
</dictionaryContext.Provider>
);
};
Loading

0 comments on commit 2953b14

Please sign in to comment.