-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7664569
commit abe302c
Showing
19 changed files
with
2,172 additions
and
3,343 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import enLocale from '~/locales/en-US.json'; | ||
|
||
type lazyLocale = | ||
| Localization.Locale | ||
| Promise<Localization.Locale> | ||
| (() => Promise<Localization.Locale>); | ||
|
||
const locales: { | ||
'en-US': lazyLocale; | ||
'uk-UA': lazyLocale; | ||
} = { | ||
'en-US': enLocale, | ||
'uk-UA': () => import('~/locales/uk-UA.json').then((m) => m.default || m), | ||
}; | ||
|
||
const state = reactive<{ | ||
locale: Localization.Lang['value']; | ||
messages: Localization.Locale; | ||
}>({ | ||
locale: 'en-US', | ||
messages: enLocale, | ||
}); | ||
|
||
// TODO: use i18n | ||
export default function useLocalization() { | ||
return { | ||
locale: computed(() => state.locale), | ||
|
||
t(localeKey: string, fallback?: string): string { | ||
if (!state.messages) { | ||
console.log('missing messages'); | ||
|
||
return fallback || localeKey; | ||
} | ||
|
||
const text = extractByDottedKey<string>(localeKey, state.messages); | ||
|
||
if (!text) | ||
console.error( | ||
`Missing translation for ${localeKey} in ${state.locale}` | ||
); | ||
|
||
return text || fallback || localeKey; | ||
}, | ||
|
||
async setLocale(locale: Localization.Lang['value']) { | ||
const assetLoadingResult = await loadLocale(locale); | ||
|
||
if (assetLoadingResult) { | ||
state.locale = locale; | ||
state.messages = locales[locale] as Localization.Locale; | ||
} | ||
|
||
return true; | ||
}, | ||
}; | ||
} | ||
|
||
function extractByDottedKey<T>(key: string, obj: object): T | undefined { | ||
return key.split('.').reduce((o, k) => o?.[k], obj) as T; | ||
} | ||
|
||
async function loadLocale(locale: Localization.Lang['value']) { | ||
let asset = locales[locale]; | ||
|
||
if (!asset) return false; | ||
if (typeof asset === 'function') asset = asset(); | ||
if (asset instanceof Promise) asset = await asset; | ||
if (locales[locale] !== asset) locales[locale] = asset; | ||
|
||
return true; | ||
} |
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 |
---|---|---|
@@ -1,7 +1,27 @@ | ||
{ | ||
"filters": { | ||
"man": "Man", | ||
"woman": "Woman", | ||
"kids": "Kids" | ||
"genders": { | ||
"man": "Man", | ||
"woman": "Woman", | ||
"kids": "Kids" | ||
}, | ||
"categories": { | ||
"new": "new", | ||
"sale": "sale", | ||
"most-popular": "most popular", | ||
"price-low-to-hight": "price low to hight", | ||
"price-hight-to-low": "price hight to low" | ||
} | ||
}, | ||
"cta": { | ||
"action-button": "browse", | ||
"buy": "buy", | ||
"add-to-cart": "to cart", | ||
"slogan": ["sunglasses", "for all", "life", "seasons"] | ||
}, | ||
|
||
"cart": { | ||
"cart-is-empty": "Cart is currently empty", | ||
"delete-item": "Delete?" | ||
} | ||
} |
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,7 +1,27 @@ | ||
{ | ||
"filters": { | ||
"man": "Чоловічі", | ||
"woman": "Жіночі", | ||
"kids": "Дитячі" | ||
"genders": { | ||
"man": "Чоловікам", | ||
"woman": "Жінкам", | ||
"kids": "Діти" | ||
}, | ||
"categories": { | ||
"new": "новинки", | ||
"sale": "зі знижкою", | ||
"most-popular": "популярні", | ||
"price-low-to-hight": "починаючи з дешевих", | ||
"price-hight-to-low": "починаючи з дорогих" | ||
} | ||
}, | ||
"cta": { | ||
"action-button": "дивитися", | ||
"buy": "купити", | ||
"add-to-cart": "до кошика", | ||
"slogan": ["сонцезахисні окуляри", "на всі", "випадки", "життя"] | ||
}, | ||
|
||
"cart": { | ||
"cart-is-empty": "Кошик наразі порожній", | ||
"delete-item": "Видалити?" | ||
} | ||
} |
Oops, something went wrong.