-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 931-docs-add-examples
- Loading branch information
Showing
15 changed files
with
2,360 additions
and
3,295 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { computed, ref } from "vue"; | ||
import localeData from "~/locales"; | ||
import i18nConfig from "~/i18n.config"; | ||
|
||
type Locale = { | ||
code: string; | ||
iso: string; | ||
name: string; | ||
}; | ||
|
||
const localeCode = ref<string>(i18nConfig.defaultLocale); | ||
|
||
const fallbackLocaleCode = ref<string>(i18nConfig.defaultLocale); | ||
|
||
const locales: Locale[] = i18nConfig.locales; | ||
|
||
const localeExists = (locale: string): boolean => { | ||
return locales.some((localeItem) => localeItem.code === locale); | ||
}; | ||
|
||
const currentLocale = computed(() => | ||
locales.find((locale) => locale.code === localeCode.value), | ||
); | ||
|
||
const fallbackLocale = computed(() => | ||
locales.find((locale) => locale.code === fallbackLocaleCode.value), | ||
); | ||
|
||
const messages = ref(new Map<string, string>()); | ||
|
||
const fallbackMessages = ref(new Map<string, string>()); | ||
|
||
const currentPagePath = ref("/"); | ||
|
||
type MessagesStructure = { [key: string]: string | MessagesStructure }; | ||
|
||
const iterateMessages = ( | ||
prefix: string | null, | ||
obj: MessagesStructure, | ||
map: Map<string, string>, | ||
): void => { | ||
for (const [key, value] of Object.entries(obj)) { | ||
const keyName = prefix ? `${prefix}.${key}` : key; | ||
if (typeof value === "string") { | ||
map.set(keyName, value); | ||
} else { | ||
iterateMessages(keyName, value, map); | ||
} | ||
} | ||
}; | ||
|
||
const loadMessages = (): void => { | ||
messages.value = new Map(fallbackMessages.value); | ||
if (!Object.prototype.hasOwnProperty.call(localeData, localeCode.value)) { | ||
return; | ||
} | ||
const localeMessage: MessagesStructure = | ||
localeData[localeCode.value as keyof typeof localeData]; | ||
iterateMessages(null, localeMessage, messages.value); | ||
}; | ||
|
||
const loadFallbackMessages = (): void => { | ||
if ( | ||
!Object.prototype.hasOwnProperty.call( | ||
localeData, | ||
fallbackLocaleCode.value, | ||
) | ||
) { | ||
return; | ||
} | ||
const fallbackLocaleMessages: MessagesStructure = | ||
localeData[fallbackLocaleCode.value as keyof typeof localeData]; | ||
iterateMessages(null, fallbackLocaleMessages, fallbackMessages.value); | ||
}; | ||
|
||
const t = (key: string): string => { | ||
return messages.value.get(key) || ""; | ||
}; | ||
|
||
const hasPathLocale = (path: string): boolean => { | ||
return locales.some( | ||
(locale) => | ||
path.startsWith(`/${locale.code}/`) || path === `/${locale.code}`, | ||
); | ||
}; | ||
|
||
const getLocalePath = (path: string): string => { | ||
const hasLocale = hasPathLocale(path); | ||
if (localeCode.value === fallbackLocale.value?.code) { | ||
const newPath = hasLocale | ||
? path.replace(`/${localeCode.value}`, "") | ||
: path; | ||
return newPath === "" ? "/" : newPath; | ||
} | ||
return hasLocale ? path : `/${localeCode.value}${path}`; | ||
}; | ||
|
||
const setLocaleCode = (code: string): void => { | ||
if (code === localeCode.value || !localeExists(code)) { | ||
return; | ||
} | ||
localeCode.value = code; | ||
loadMessages(); | ||
}; | ||
|
||
const setDefaultLocaleCode = (): void => { | ||
localeCode.value = fallbackLocale.value?.code || "en"; | ||
messages.value = new Map(fallbackMessages.value); | ||
}; | ||
|
||
const initI18n = (): void => { | ||
if (fallbackMessages.value.size > 0) return; | ||
loadFallbackMessages(); | ||
}; | ||
|
||
export default function useI18n() { | ||
return { | ||
localeCode, | ||
fallbackLocaleCode, | ||
localeExists, | ||
currentLocale, | ||
fallbackLocale, | ||
loadMessages, | ||
loadFallbackMessages, | ||
t, | ||
getLocalePath, | ||
setLocaleCode, | ||
setDefaultLocaleCode, | ||
currentPagePath, | ||
initI18n, | ||
}; | ||
} |
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 @@ | ||
# Enterprise |
Empty file.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"urls": { | ||
"url": { | ||
"about": "/o-agdb", | ||
"contact": "/kontakt" | ||
}, | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"urls": { | ||
"url": { | ||
"about": "/about", | ||
"contact": "/contact" | ||
}, | ||
|
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,7 @@ | ||
import en from "./en.json"; | ||
import cs from "./cs.json"; | ||
|
||
export default { | ||
en, | ||
cs, | ||
}; |
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.