-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update i18n module and adapt config folder and trad files (#203)
* feat: update i18n module and adapt config folder and trad files * chore: add comment * feat: add detectBrowserLanguage and default files message * feat: clean i18n detectBrowserLanguage functionnality * chore: update en trad * chore: remove default translation files * feat(i18n): move constants outside of config file * feat(i18n): use available locales provided by Nuxt i18n * fix(i18n): detect browser language * feat: add useRoadizDetectBrowserLanguage * chore: remove comments * refactor: remove detect browser lang const * chore: remove debug data * refactor: improve useRoadizDetectBrowserLanguage() readability --------- Co-authored-by: Manuel Odelain <[email protected]>
- Loading branch information
1 parent
d464605
commit 2a2c9fe
Showing
13 changed files
with
517 additions
and
404 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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
<script lang="ts" setup> | ||
const { locale } = useI18n() | ||
locale.value = 'en' | ||
</script> | ||
|
||
<template> | ||
<NuxtStory> | ||
<NuxtStoryVariant title="Single"> | ||
<VTime datetime="2023-06-28T22:00:00+02:00" /> | ||
</NuxtStoryVariant> | ||
<NuxtStoryVariant title="Single (with minutes)"> | ||
<VTime datetime="2023-06-28T22:15:00+02:00" /> | ||
</NuxtStoryVariant> | ||
<NuxtStoryVariant title="Range (multi times / same day)"> | ||
<VTime :datetime="['2023-06-28T19:30:00+02:00', '2023-06-28T22:30:00+02:00']" /> | ||
</NuxtStoryVariant> | ||
<NuxtStoryVariant title="Range (multi days / same month)"> | ||
<VTime :datetime="['2023-06-28T22:30:00+02:00', '2023-06-29T22:30:00+02:00']" /> | ||
</NuxtStoryVariant> | ||
<NuxtStoryVariant title="Range (multi days / multi months)"> | ||
<VTime :datetime="['2023-06-28T22:30:00+02:00', '2023-07-12T22:30:00+02:00']" /> | ||
</NuxtStoryVariant> | ||
<NuxtStoryVariant title="Range (multi years)"> | ||
<VTime :datetime="['2023-06-28T22:30:00+02:00', '2024-07-12T22:30:00+02:00']" /> | ||
</NuxtStoryVariant> | ||
</NuxtStory> | ||
</template> |
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,19 @@ | ||
import { localeCodes, nuxtI18nOptions } from '#build/i18n.options.mjs' | ||
|
||
// Workaround for useCookieLocale() returning empty string | ||
// @see https://github.com/nuxt-modules/i18n/issues/2975 | ||
export function useI18nCookie(): Ref<string> { | ||
const locale: Ref<string> = ref('') | ||
const detect = nuxtI18nOptions.detectBrowserLanguage | ||
|
||
if (detect && detect.useCookie) { | ||
const cookieKey = detect.cookieKey | ||
const code = useCookie<string>(cookieKey).value ?? null | ||
|
||
if (code && localeCodes.includes(code)) { | ||
locale.value = code | ||
} | ||
} | ||
|
||
return locale | ||
} |
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,26 @@ | ||
import type { RoadizAlternateLink } from '@roadiz/types' | ||
import { nuxtI18nOptions } from '#build/i18n.options.mjs' | ||
|
||
export async function useRoadizDetectBrowserLanguage({ locale, alternateLinks }: { locale: string, alternateLinks: RoadizAlternateLink[] }) { | ||
const { detectBrowserLanguage } = nuxtI18nOptions | ||
|
||
if (!detectBrowserLanguage) return | ||
|
||
const isRootPath = useRoute().path === '/' || alternateLinks.some(link => link.url === '/') | ||
|
||
if (!isRootPath && detectBrowserLanguage.redirectOn === 'root') return | ||
|
||
const cookieLocale = useI18nCookie().value | ||
|
||
if (cookieLocale && detectBrowserLanguage.alwaysRedirect !== true) return | ||
|
||
const { $i18n } = useNuxtApp() | ||
const browserLocale = useBrowserLocale() | ||
const preferredLocale = detectBrowserLanguage.useCookie ? (cookieLocale || browserLocale) : browserLocale | ||
|
||
if (preferredLocale && preferredLocale !== locale && $i18n.locales.value.find(availableLocale => availableLocale.code === preferredLocale)) { | ||
const alternateLink = alternateLinks.find(link => link.locale === preferredLocale) | ||
|
||
if (alternateLink) await navigateTo(alternateLink.url, { replace: true, external: true }) | ||
} | ||
} |
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,3 @@ | ||
export const I18N_LOCALES = ['fr'] as const | ||
export const I18N_DEFAULT_LOCALE = 'fr' | ||
export const I18N_DEFAULT_TIMEZONE = 'Europe/Paris' |
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
File renamed without changes.
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,2 @@ | ||
{ | ||
} |
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
Oops, something went wrong.