mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-18 01:25:09 +00:00
6e47d76ba6
Client file sharing, rebuilt from the ground up: a private area per client, resumable uploads, folders, groups and categories, sharing with expiry dates and download limits, comments, file versions, an activity log, a REST API, and sixteen languages. This repository begins here. ProjectSend 2 was developed privately, and that development history is not published — the previous generation remains available, with its own history, at projectsend/legacy. Free software under the GNU General Public License v2, or (at your option) any later version.
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { intlLocale } from '@/lib/intl-locale';
|
|
|
|
/**
|
|
* Naming a language for a human, from this application's locale keys.
|
|
*
|
|
* The tag has to be converted first — a catalogue key like `zh_CN` is not a
|
|
* valid BCP 47 tag, and `Intl.DisplayNames` throws on it rather than
|
|
* degrading, which without `intlLocale()` left Chinese listed as "zh_CN".
|
|
* Unknown codes fall back to the code itself: locales are discovered from
|
|
* disk, so a pack for a language `Intl` has never heard of is a legitimate
|
|
* thing to find.
|
|
*/
|
|
function displayName(code: string, displayIn: string): string {
|
|
const tag = intlLocale(code);
|
|
|
|
if (tag === undefined) {
|
|
return code;
|
|
}
|
|
|
|
try {
|
|
const name = new Intl.DisplayNames([displayIn], { type: 'language' }).of(tag) ?? code;
|
|
|
|
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
} catch {
|
|
return code;
|
|
}
|
|
}
|
|
|
|
/** The language's own name for itself (autonym): "Español", not "Spanish". */
|
|
export function languageName(code: string): string {
|
|
return displayName(code, intlLocale(code) ?? 'en');
|
|
}
|
|
|
|
/**
|
|
* The English name — a secondary label for the Languages settings screen,
|
|
* where an administrator may well be picking languages they cannot read.
|
|
*/
|
|
export function englishLanguageName(code: string): string {
|
|
return displayName(code, 'en');
|
|
}
|