-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathlang.ts
256 lines (217 loc) · 8.53 KB
/
lang.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import i18next, { StringMap, TOptions } from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { Stage, Status, FinalType, GroupType, StageType } from 'brackets-model';
import { isMajorRound } from './helpers';
import { OriginHint, RoundNameInfo } from './types';
import en from './i18n/en/translation.json';
import fr from './i18n/fr/translation.json';
export const locales = {
en,
fr,
};
export type Locale = typeof locales['en'];
void i18next.use(LanguageDetector).init({
fallbackLng: 'en',
debug: false,
resources: {
en: {
translation: locales.en,
},
fr: {
translation: locales.fr,
},
},
});
/**
* Adds a locale to the available i18n bundles.
*
* @param name Name of the locale.
* @param locale Contents of the locale.
*/
export async function addLocale(name: string, locale: Locale): Promise<void> {
i18next.addResourceBundle(name, 'translation', locale, true, true);
await i18next.changeLanguage();
}
/**
* Returns an internationalized version of a locale key.
*
* @param key A locale key.
* @param options Data to pass to the i18n process.
*/
export function t<Scope extends keyof Locale, SubKey extends string & keyof Locale[Scope], T extends TOptions>(
key: `${Scope}.${SubKey}`, options?: T,
): T['returnObjects'] extends true ? StringMap : string {
return i18next.t(key, options);
}
export type Translator = typeof t;
export type ToI18nKey<S extends string> = S extends `${infer A}_${infer B}`
? `${A}-${B}`
: never;
/**
* Converts a type to a valid i18n key.
*
* @param key The key to convert.
*/
export function toI18nKey<S extends `${string}_${string}`>(key: S): ToI18nKey<S> {
return key.replace('_', '-') as ToI18nKey<S>;
}
/**
* Returns an origin hint function based on rounds information.
*
* @param roundNumber Number of the round.
* @param roundCount Count of rounds.
* @param skipFirstRound Whether to skip the first round.
* @param matchLocation Location of the match.
*/
export function getOriginHint(roundNumber: number, roundCount: number, skipFirstRound: boolean, matchLocation: GroupType): OriginHint | undefined {
if (roundNumber === 1) {
if (matchLocation === 'single_bracket')
return (position: number): string => t('origin-hint.seed', { position });
if (matchLocation === 'winner_bracket')
return (position: number): string => t('origin-hint.seed', { position });
if (matchLocation === 'loser_bracket' && skipFirstRound)
return (position: number): string => t('origin-hint.seed', { position });
}
if (isMajorRound(roundNumber) && matchLocation === 'loser_bracket') {
if (roundNumber === roundCount - 2)
return (position: number): string => t('origin-hint.winner-bracket-semi-final', { position });
if (roundNumber === roundCount)
return (): string => t('origin-hint.winner-bracket-final');
const roundNumberWB = Math.ceil((roundNumber + 1) / 2);
if (skipFirstRound)
return (position: number): string => t('origin-hint.winner-bracket', { round: roundNumberWB - 1, position });
return (position: number): string => t('origin-hint.winner-bracket', { round: roundNumberWB, position });
}
return undefined;
}
/**
* Returns an origin hint function for a match in final.
*
* @param stageType Type of the stage.
* @param finalType Type of the final.
* @param roundNumber Number of the round.
*/
export function getFinalOriginHint(stageType: StageType, finalType: FinalType, roundNumber: number): OriginHint | undefined {
if (stageType === 'single_elimination')
return (position: number): string => t('origin-hint.consolation-final', { position });
// Double elimination.
if (finalType === 'grand_final') {
return roundNumber === 1
? (): string => t('origin-hint.grand-final') // Grand Final round 1
: undefined; // Grand Final round 2 (no hint because it's obvious both participants come from the previous round)
}
// Consolation final in double elimination.
return (position: number): string => position === 1
? t('origin-hint.double-elimination-consolation-final-opponent-1')
: t('origin-hint.double-elimination-consolation-final-opponent-2');
}
/**
* Returns the label of a match.
*
* @param matchNumber Number of the match.
* @param roundNumber Number of the round.
* @param roundCount Count of rounds.
* @param matchLocation Location of the match.
*/
export function getMatchLabel(matchNumber: number, roundNumber?: number, roundCount?: number, matchLocation?: GroupType): string {
if (roundNumber === undefined || roundCount === undefined || matchLocation === undefined)
return t('match-label.default', { matchNumber });
const matchPrefix = matchLocation === 'winner_bracket' ? t('match-label.winner-bracket') :
matchLocation === 'loser_bracket' ? t('match-label.loser-bracket') : t('match-label.standard-bracket');
const inSemiFinalRound = roundNumber === roundCount - 1;
const inFinalRound = roundNumber === roundCount;
if (matchLocation === 'single_bracket') {
if (inSemiFinalRound)
return t('match-label.standard-bracket-semi-final', { matchNumber });
if (inFinalRound)
return t('match-label.standard-bracket-final');
}
if (inSemiFinalRound)
return t('match-label.double-elimination-semi-final', { matchPrefix, matchNumber });
if (inFinalRound)
return t('match-label.double-elimination-final', { matchPrefix });
return t('match-label.double-elimination', { matchPrefix, roundNumber, matchNumber });
}
/**
* Returns the label of a match in final.
*
* @param finalType Type of the final.
* @param roundNumber Number of the round.
* @param roundCount Count of rounds.
*/
export function getFinalMatchLabel(finalType: FinalType, roundNumber: number, roundCount: number): string {
// Single elimination.
if (finalType === 'consolation_final')
return t('match-label.consolation-final');
// Double elimination.
if (roundCount === 1)
return t('match-label.grand-final-single');
return t('match-label.grand-final', { roundNumber });
}
/**
* Returns the status of a match.
*
* @param status The match status.
*/
export function getMatchStatus(status: Status): string {
switch (status) {
case Status.Locked:
return t('match-status.locked');
case Status.Waiting:
return t('match-status.waiting');
case Status.Ready:
return t('match-status.ready');
case Status.Running:
return t('match-status.running');
case Status.Completed:
return t('match-status.completed');
case Status.Archived:
return t('match-status.archived');
default:
return 'Unknown status';
}
}
/**
* Returns the name of a group.
*
* @param groupNumber Number of the group.
*/
export function getGroupName(groupNumber: number): string {
return t('common.group-name', { groupNumber });
}
/**
* Returns the name of the bracket.
*
* @param stage The current stage.
* @param type Type of the bracket.
*/
export function getBracketName(stage: Stage, type: GroupType): string | undefined {
switch (type) {
case 'winner_bracket':
case 'loser_bracket':
return t(`common.group-name-${toI18nKey(type)}`, { stage });
default:
return undefined;
}
}
// eslint-disable-next-line jsdoc/require-param
/**
* Returns the name of a round.
*/
export function getRoundName({ roundNumber, roundCount }: RoundNameInfo, t: Translator): string {
return roundNumber === roundCount ? t('common.round-name-final') : t('common.round-name', { roundNumber });
}
// eslint-disable-next-line jsdoc/require-param
/**
* Returns the name of a round in the winner bracket of a double elimination stage.
*/
export function getWinnerBracketRoundName({ roundNumber, roundCount }: RoundNameInfo, t: Translator): string {
return roundNumber === roundCount ? t('common.round-name-winner-bracket-final') : t('common.round-name-winner-bracket', { roundNumber });
}
// eslint-disable-next-line jsdoc/require-param
/**
* Returns the name of a round in the loser bracket of a double elimination stage.
*/
export function getLoserBracketRoundName({ roundNumber, roundCount }: RoundNameInfo, t: Translator): string {
return roundNumber === roundCount ? t('common.round-name-loser-bracket-final') : t('common.round-name-loser-bracket', { roundNumber });
}