tethys.backend/resources/js/utils/tethyscloud-l10n/locale.ts
Arno Kaimbacher b06ccae603
Some checks failed
CI Pipeline / japa-tests (push) Failing after 1m2s
- added @adonisjs/mail
- mail_settings_controller for setting smtp settings
- added view ror rjecting dataset for editor
- added new model AppConfig for stroing appwide config values
- better validate_chesum.ts command with process chunking
- added vue3 apps 'BasicSettings' like email, profile settings
- started with 2 multilingual capabilities
- npm updates
2024-09-16 17:59:46 +02:00

84 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Returns the user's locale
*/
export function getLocale(): string {
return document.documentElement.dataset.locale || 'en'
}
/**
* Returns user's locale in canonical form
* E.g. `en-US` instead of `en_US`
*/
export function getCanonicalLocale(): string {
return getLocale().replace(/_/g, '-')
}
/**
* Returns the user's language
*/
export function getLanguage(): string {
return document.documentElement.lang || 'en'
}
/**
* Check whether the current, or a given, language is read right-to-left
*
* @param language Language code to check, defaults to current language
*/
export function isRTL(language?: string): boolean {
const languageCode = language || getLanguage()
// Source: https://meta.wikimedia.org/wiki/Template:List_of_language_names_ordered_by_code
const rtlLanguages = [
/* eslint-disable no-multi-spaces */
'ae', // Avestan
'ar', // 'العربية', Arabic
'arc', // Aramaic
'arz', // 'مصرى', Egyptian
'bcc', // 'بلوچی مکرانی', Southern Balochi
'bqi', // 'بختياري', Bakthiari
'ckb', // 'Soranî / کوردی', Sorani
'dv', // Dhivehi
'fa', // 'فارسی', Persian
'glk', // 'گیلکی', Gilaki
'ha', // 'هَوُسَ', Hausa
'he', // 'עברית', Hebrew
'khw', // 'کھوار', Khowar
'ks', // 'कॉशुर / کٲشُر', Kashmiri
'ku', // 'Kurdî / كوردی', Kurdish
'mzn', // 'مازِرونی', Mazanderani
'nqo', // 'ߒߞߏ', NKo
'pnb', // 'پنجابی', Western Punjabi
'ps', // 'پښتو', Pashto,
'sd', // 'سنڌي', Sindhi
'ug', // 'Uyghurche / ئۇيغۇرچە', Uyghur
'ur', // 'اردو', Urdu
'uzs', // 'اوزبیکی', Uzbek Afghan
'yi', // 'ייִדיש', Yiddish
/* eslint-enable no-multi-spaces */
]
// special case for Uzbek Afghan
if ((language || getCanonicalLocale()).startsWith('uz-AF')) {
return true
}
return rtlLanguages.includes(languageCode)
}
export function getBrowserLocale(options?: { languageCodeOnly: boolean }): string {
const navigatorLocale = navigator.languages !== undefined
? navigator.languages[0]
: navigator.language;
if (!navigatorLocale) {
return 'en'; // Fallback to 'en' if no locale is detected
}
const locale = options?.languageCodeOnly
? navigatorLocale.trim().split(/-|_/)[0]
: navigatorLocale.trim();
return locale;
}