generated from Blazity/next-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from Blazity/g-translations
feat: add global translations
- Loading branch information
Showing
17 changed files
with
164 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pickBy from "lodash/pickBy" | ||
import { cache } from "react" | ||
import { GetGlobalTranslationsQuery } from "@/gql/graphql" | ||
import { getGlobalTranslations } from "@/lib/client" | ||
import { Locale } from "./i18n" | ||
|
||
type NonNullableProperty<T> = { [P in keyof T]: NonNullable<T[P]> } | ||
type RequiredNonNullable<T> = Required<NonNullableProperty<T>> | ||
export type GlobalTranslations = RequiredNonNullable< | ||
Omit<NonNullable<NonNullable<GetGlobalTranslationsQuery["translationsSingleton"]>["translations"]>, "__typename"> | ||
> | ||
|
||
export const defaultTranslations = { | ||
showMore: "Show More (fallback)", | ||
showing: "Showing", | ||
resultsFor: "results for", | ||
searchCategory: "Search Category", | ||
search: "Search (fallback)", | ||
selectTag: "Select tag (fallback)", | ||
shareOnSocial: "Share on social", | ||
relatedArticles: "Related articles", | ||
noTagsFound: "No tags found.", | ||
noResultsFor: "No results for", | ||
loading: "Loading", | ||
} | ||
|
||
const getCache = cache(() => { | ||
const value: { translations: GlobalTranslations } = { | ||
translations: defaultTranslations, | ||
} | ||
return value | ||
}) | ||
|
||
export async function setTranslations(locale: Locale) { | ||
const translations = await getGlobalTranslations({ locale }) | ||
|
||
const merged = translations | ||
? { ...defaultTranslations, ...pickBy(translations, (val) => Boolean(val)) } | ||
: defaultTranslations | ||
getCache().translations = merged | ||
return merged | ||
} | ||
|
||
export function getTranslations() { | ||
return getCache().translations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use client" | ||
import { createContext, ReactNode, useContext } from "react" | ||
import { GlobalTranslations } from "./setTranslations" | ||
|
||
const TranslationsContext = createContext<GlobalTranslations | null>(null) | ||
|
||
export const TranslationsProvider = ({ | ||
children, | ||
translations, | ||
}: { | ||
children: ReactNode | ||
translations: GlobalTranslations | ||
}) => { | ||
return <TranslationsContext.Provider value={translations}>{children}</TranslationsContext.Provider> | ||
} | ||
|
||
export const useTranslations = () => { | ||
const context = useContext(TranslationsContext) | ||
if (!context) { | ||
throw new Error("useTranslations must be used within a TranslationsProvider") | ||
} | ||
|
||
return context | ||
} |
Oops, something went wrong.