-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate.ts
More file actions
33 lines (32 loc) · 1.21 KB
/
create.ts
File metadata and controls
33 lines (32 loc) · 1.21 KB
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
import * as isoly from "isoly"
import { getLanguage } from "./getLanguage"
import { Translate } from "./Translate"
import { Translation } from "./Translation"
import { Translations } from "./Translations"
export function create(translations: Translations, language: isoly.Language | HTMLElement): Translate
export function create(translation: Translation): Translate
export function create(translations: Translations | Translation, language?: isoly.Language | HTMLElement): Translate {
let result: Translate
if (!language) {
const translation = translations as Translation
result = (message: string, ...argument: any[]) => {
const r = translation[message]
return !r
? fallback(message, ...argument)
: typeof r == "function"
? r(...argument)
: argument.length > 0
? fallback(r, ...argument)
: r
}
} else {
if (!isoly.Language.is(language))
language = getLanguage(language)
const translation = language && (translations as Translations)[language]
result = translation ? create(translation) : fallback
}
return result
}
function fallback(message: string, ...argument: any[]): string {
return argument.length > 0 ? argument.reduce<string>((r, a, i) => r.replace("$" + i, a), message) : message
}