forked from datacommonsorg/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.tsx
346 lines (331 loc) · 9.54 KB
/
i18n.tsx
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Helpers for formatJS i18n library. More info at formatjs.io
* NOTE: Messages in this file will not be extracted for translation.
*/
import React from "react";
import { createIntl, createIntlCache, IntlCache, IntlShape } from "react-intl";
// A single cache instance can be shared for all locales.
// TODO(beets): might not be necessary since we create one intl object.
const intlCache: IntlCache = createIntlCache();
// This IntlShape object will be used for both React Intl's
// React Component API (arg for RawIntlProvider) and
// Imperative API (format<X> method).
let intl: IntlShape = createIntl({ locale: "en", messages: {} }, intlCache);
/**
* Load compiled messages into the global intl object.
*
* @param locale: Locale determined server-side for consistency.
* @param modules: An array of Promises from calling import on the compiled
* message module for the current locale. Note that this needs to be done from
* the app so that we won't have to bundle all compiled messages across apps.
* See https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import
*/
function loadLocaleData(
locale: string,
modules: Promise<Record<any, any>>[]
): Promise<void> {
const allMessages = {};
return Promise.all(modules)
.then((messages) => {
for (const msg of messages) {
Object.assign(allMessages, msg.default);
}
intl = createIntl({ locale, messages: allMessages }, intlCache);
})
.catch((err) => {
console.error(err);
intl = createIntl({ locale, messages: {} }, intlCache);
});
}
/**
* Returns translation for message with :id. If unavailable, :id is returned as
* the translation.
*
* Note: Only use this for variables. Raw strings in JS should call
* intl.formatMessage or <FormattedMessage> directly in order for the extractor
* to pick up the id.
*
* @param id: message bundle id
* @return translation for :id, or :id if translation is unavailable.
*/
function translateVariableString(id: string): string {
if (!id) {
return "";
}
return intl.formatMessage({
// Matching ID as above
id,
// Default Message in English.
// Can consider suppressing log error when translation not found.
defaultMessage: id,
description: id,
});
}
/**
* Adds / updates the hl parameter for the searchParams to maintain the current
* page's locale.
* TODO(beets): Add tests for this function.
*
* @param searchParams: to update
* @return potentially updated searchParams
*/
function localizeSearchParams(searchParams: URLSearchParams): URLSearchParams {
if (intl.locale == "en") {
return searchParams;
}
searchParams.set("hl", intl.locale);
return searchParams;
}
/**
* Adds / updates the hl parameter for the link to maintain the current page's locale.
* TODO(beets): Add tests for this function.
*
* @param href: to update
* @return potentially updated href
*/
function localizeLink(href: string): string {
try {
const url = new URL(href, document.location.origin);
url.search = localizeSearchParams(
new URLSearchParams(url.searchParams)
).toString();
return url.toString();
} catch (e) {
return href;
}
}
/**
* Properties for LocalizedLink. Property names are analogous to those for <a> tags.
*/
interface LocalizedLinkProps {
className?: string;
href: string;
text: string | JSX.Element;
// Callback function when a link is clicked.
handleClick?: () => void;
onMouseEnter?: () => void;
onMouseLeave?: () => void;
}
/**
* Adds / updates the hl parameter for the link to maintain the current page's locale.
* TODO(beets): Add tests for this component.
*
* @param props: <a> tag properties to include
* @return An <a> tag JSX element.
*/
function LocalizedLink(props: LocalizedLinkProps): JSX.Element {
const href = props.href ? localizeLink(props.href) : null;
return (
<a
href={href}
className={props.className ? props.className : null}
onClick={props.handleClick}
onMouseEnter={props.onMouseEnter}
onMouseLeave={props.onMouseLeave}
>
{props.text}
</a>
);
}
/**
* Formats numbers to the currently set locale. Only shows a certain number of
* significant digits. To call this, i18n/compiled-strings/{locale}/units.json
* must be loaded. Only a subset of units are available. To add a unit, add the
* appropriate "short-other-nominative" unit from CLDR, as well as the display
* name for the unit to each locale's unit message bundle. e.g.
* https://unicode-org.github.io/cldr-staging/charts/38/summary/ru.html or
* https://github.com/unicode-org/cldr/blob/release-38-1/common/main/de.xml
*
* @param value: the number to format
* @param unit: (optional) short unit
*
* @return localized display string for the number
*/
function formatNumber(
value: number,
unit?: string,
useDefaultFormat?: boolean,
numFractionDigits?: number
): string {
if (isNaN(value)) {
return "-";
}
if (useDefaultFormat) {
return Intl.NumberFormat(intl.locale).format(value);
}
const formatOptions: any = {
/* any is used since not all available options are defined in NumberFormatOptions */
compactDisplay: "short",
maximumSignificantDigits: 3,
notation: "compact",
style: "decimal",
};
if (numFractionDigits !== undefined) {
formatOptions.maximumFractionDigits = numFractionDigits;
formatOptions.minimumFractionDigits = numFractionDigits;
delete formatOptions.maximumSignificantDigits;
}
let unitKey: string;
switch (unit) {
case "₹":
formatOptions.style = "currency";
formatOptions.currency = "INR";
formatOptions.currencyDisplay = "code";
break;
case "$":
case "USDollar":
formatOptions.style = "currency";
formatOptions.currency = "USD";
formatOptions.currencyDisplay = "code";
break;
case "%":
case "Percent":
case "Percentage":
formatOptions.style = "percent";
value = value / 100; // Values are scaled by formatter for percent display
break;
case "MetricTon":
case "t":
unitKey = "metric-ton";
break;
case "Millions of tonnes":
unitKey = "mega-ton";
break;
case "kWh":
unitKey = "kilowatt-hour";
break;
case "GWh":
unitKey = "gigawatt-hour";
break;
case "g":
unitKey = "gram";
break;
case "kg":
unitKey = "kilogram";
break;
case "L":
unitKey = "liter";
break;
case "celsius":
case "Celsius":
unitKey = "celsius";
break;
case "μg/m³":
unitKey = "micro-gram-per-cubic-meter";
break;
case "MetricTonCO2e":
case "MTCO2e":
unitKey = "metric-tons-of-co2";
break;
case "per-million":
unitKey = "per-million";
break;
case "¢/kWh":
unitKey = "cent-per-kilowatt";
break;
case "ppb":
unitKey = "ppb";
break;
case "mgd":
unitKey = "million-gallon-per-day";
break;
default:
unitKey = unit;
}
let returnText = Intl.NumberFormat(intl.locale, formatOptions).format(value);
if (unitKey) {
returnText = intl.formatMessage(
{
id: unitKey,
defaultMessage: `{0} {unit}`,
},
{ 0: returnText, unit }
);
}
return returnText;
}
/**
* Returns a sentence-cased, translated unit in the current locale for display
* purposes. To call this, i18n/compiled-strings/{locale}/units.json must be
* loaded. Only a subset of units are available. To add a unit, add the
* appropriate display name for the unit from CLDR, e.g.
* https://unicode-org.github.io/cldr-staging/charts/38/summary/ru.html or
* https://github.com/unicode-org/cldr/blob/release-38-1/common/main/de.xml
*
* @param unit: short unit
*
* @return localized display string for the number
*/
function translateUnit(unit: string): string {
let messageId: string;
switch (unit) {
case "$":
return "USD";
case "Percent":
case "Percentage":
case "%":
return "%";
case "t":
messageId = "metric-ton-display";
break;
case "Millions of tonnes":
messageId = "mega-ton-display";
break;
case "kWh":
messageId = "kilowatt-hour-display";
break;
case "g":
messageId = "gram-display";
break;
case "kg":
messageId = "kilogram-display";
break;
case "L":
messageId = "liter-display";
break;
case "celsius":
case "Celsius":
messageId = "celsius-display";
break;
case "Knot":
case "Millibar":
case "SquareKilometer":
messageId = `${unit}-display`;
default:
return unit;
}
let displayUnit = intl.formatMessage({
id: messageId,
defaultMessage: unit,
});
// A hack to use since there is no standardized equivalent:
// https://github.com/tc39/ecma402/issues/294
displayUnit =
displayUnit.charAt(0).toLocaleUpperCase() + displayUnit.slice(1);
return displayUnit;
}
export {
formatNumber,
intl,
loadLocaleData,
LocalizedLink,
localizeLink,
localizeSearchParams,
translateUnit,
translateVariableString,
};