You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
792 B
42 lines
792 B
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
|
|
|
interface LanguageResponse {
|
|
identifier: string;
|
|
}
|
|
|
|
function getLanguage() {
|
|
return createAjaxRequest({
|
|
global: false,
|
|
dataType: 'json',
|
|
url: '/localization/language',
|
|
}).request;
|
|
}
|
|
|
|
function getDisplayName(code: string) {
|
|
return Intl.DisplayNames
|
|
? new Intl.DisplayNames([code], { type: 'language' })
|
|
: null;
|
|
}
|
|
|
|
let languageNames = getDisplayName('en');
|
|
|
|
getLanguage().then((data: LanguageResponse) => {
|
|
const names = getDisplayName(data.identifier);
|
|
|
|
if (names) {
|
|
languageNames = names;
|
|
}
|
|
});
|
|
|
|
export default function getLanguageName(code: string) {
|
|
if (!languageNames) {
|
|
return code;
|
|
}
|
|
|
|
try {
|
|
return languageNames.of(code) ?? code;
|
|
} catch (error) {
|
|
return code;
|
|
}
|
|
}
|