This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Message.ts
64 lines (52 loc) · 1.52 KB
/
Message.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Serves as a wrapper for [[https://github.com/globalizejs/globalize |
* Globalize.js]].
*/
import "./loadJSON";
import * as Globalize from "globalize";
import Options, { Gender } from "./LanguageOptions";
/** The wrapped Globalize object. */
export let globalize: Globalize;
export const setLanguage = function (lang: string): void {
globalize = new Globalize(lang);
};
/**
* Gets the translation of a message from loadMessages.js.
*
* @returns The translated message.
*/
export const get = function (message: string, options: Options = {}): string {
options.uppercase ||= false;
options.gender ||= Gender.male;
options.count ||= 1;
return uppercase(globalize.messageFormatter(message)(options), options);
};
export const error = function (
code: string,
options: Options = {},
dev = false
): never {
let message: string = get(code, options);
if (dev) message = get("error/unexpected", { arg0: message });
throw new Error(message);
};
export const uppercase = function (str: string, options: Options = {}): string {
if (options.uppercase) return firstToUpper(str);
return str;
};
/**
* Makes the first letter of a string uppercase.
*
* @returns The transformed string.
*/
export const firstToUpper = function (str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
};
/**
* Makes the first letter of a string lowercase.
*
* @returns The transformed string.
*/
export const firstToLower = function (str: string): string {
return str.charAt(0).toLowerCase() + str.slice(1);
};