initial commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
// Type Imports
|
||||
import type { Locale } from '@configs/i18n'
|
||||
|
||||
const dictionaries = {
|
||||
en: () => import('@/data/dictionaries/en.json').then(module => module.default),
|
||||
fr: () => import('@/data/dictionaries/fr.json').then(module => module.default),
|
||||
ar: () => import('@/data/dictionaries/ar.json').then(module => module.default)
|
||||
}
|
||||
|
||||
export const getDictionary = async (locale: Locale) => dictionaries[locale]()
|
||||
@@ -0,0 +1,3 @@
|
||||
// Returns initials from string
|
||||
export const getInitials = (string: string) =>
|
||||
string.split(/\s/).reduce((response, word) => (response += word.slice(0, 1)), '')
|
||||
@@ -0,0 +1,17 @@
|
||||
// Config Imports
|
||||
import { i18n } from '@configs/i18n'
|
||||
|
||||
// Util Imports
|
||||
import { ensurePrefix } from '@/utils/string'
|
||||
|
||||
// Check if the url is missing the locale
|
||||
export const isUrlMissingLocale = (url: string) => {
|
||||
return i18n.locales.every(locale => !(url.startsWith(`/${locale}/`) || url === `/${locale}`))
|
||||
}
|
||||
|
||||
// Get the localized url
|
||||
export const getLocalizedUrl = (url: string, languageCode: string): string => {
|
||||
if (!url || !languageCode) throw new Error("URL or Language Code can't be empty")
|
||||
|
||||
return isUrlMissingLocale(url) ? `/${languageCode}${ensurePrefix(url, '/')}` : url
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export const ensurePrefix = (str: string, prefix: string) => (str.startsWith(prefix) ? str : `${prefix}${str}`)
|
||||
export const withoutSuffix = (str: string, suffix: string) =>
|
||||
str.endsWith(suffix) ? str.slice(0, -suffix.length) : str
|
||||
export const withoutPrefix = (str: string, prefix: string) => (str.startsWith(prefix) ? str.slice(prefix.length) : str)
|
||||
Reference in New Issue
Block a user