File tree Expand file tree Collapse file tree 3 files changed +44
-1
lines changed
Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 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" ,
4444 "." : {
4545 "node" : " ./dist/index.js" ,
4646 "default" : null
47+ },
48+ "./i18n" : {
49+ "node" : " ./dist/i18n.js" ,
50+ "default" : null
4751 }
4852 },
4953 "files" : [
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" ,
You can’t perform that action at this time.
0 commit comments