Skip to content

Commit 1e4546c

Browse files
committed
i18n
1 parent 10cd469 commit 1e4546c

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "firebase-frameworks",
3-
"version": "0.9.0",
3+
"version": "0.10.0",
44
"description": "Experimental addon to the Firebase CLI to add web framework support",
55
"main": "dist/index.js",
66
"repository": "FirebaseExtended/firebase-framework-tools",
@@ -44,6 +44,10 @@
4444
".": {
4545
"node": "./dist/index.js",
4646
"default": null
47+
},
48+
"./i18n": {
49+
"node": "./dist/i18n.js",
50+
"default": null
4751
}
4852
},
4953
"files": [

src/i18n.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Request } from "firebase-functions/v2/https";
2+
3+
export function getPreferredLocale(req: Request, locales: string[], defaultLocale: string): string {
4+
const country = req.headers["x-country-code"] || ""
5+
const languages = languagesByPreference(req.headers["accept-language"]);
6+
const localesByHostingOOO: string[] = [];
7+
if (country) {
8+
for (const language of languages) {
9+
localesByHostingOOO.push(`${language}_${country}`);
10+
}
11+
localesByHostingOOO.push(`ALL_${country}`);
12+
}
13+
for (const language of languages) {
14+
localesByHostingOOO.push(`${language}_ALL`);
15+
localesByHostingOOO.push(`${language}`);
16+
}
17+
return localesByHostingOOO.find(it => locales.includes(it)) || defaultLocale;
18+
}
19+
20+
function languagesByPreference(acceptLanguage: string|undefined): string[] {
21+
if (!acceptLanguage) {
22+
return [];
23+
}
24+
25+
const languagesSeen = new Set<string>();
26+
const languagesOrdered: string[] = [];
27+
for (const v of acceptLanguage.split(",")) {
28+
const l = v.split("-")[0];
29+
if (!l) {
30+
continue;
31+
}
32+
if (!languagesSeen.has(l)) {
33+
languagesOrdered.push(l);
34+
}
35+
languagesSeen.add(l);
36+
}
37+
return languagesOrdered;
38+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"include": [
1414
"src/index.ts",
15+
"src/i18n.ts",
1516
"src/firebase-aware.ts",
1617
"src/*/index.ts",
1718
"src/*/firebase-aware.ts",

0 commit comments

Comments
 (0)