9
9
* rights grant found at http://polymer.github.io/PATENTS.txt
10
10
*/
11
11
12
- import { Message , ProgramMessage , Placeholder } from './messages' ;
13
- import { applyPatches , Patches } from './patches' ;
14
- import { Locale } from './locales' ;
15
- import { Config , RuntimeOutputConfig } from './config' ;
16
- import { KnownError } from './error' ;
12
+ import { Message , ProgramMessage , Placeholder } from '../messages' ;
13
+ import { applyPatches , Patches } from '../patches' ;
14
+ import { Locale } from '../locales' ;
15
+ import { Config } from '../config' ;
16
+ import { KnownError } from '../error' ;
17
+ import * as fs from 'fs' ;
18
+ import * as pathLib from 'path' ;
19
+
20
+ /**
21
+ * Configuration specific to the `runtime` output mode.
22
+ */
23
+ export interface RuntimeOutputConfig {
24
+ mode : 'runtime' ;
25
+
26
+ /**
27
+ * Output directory for generated TypeScript modules. After running
28
+ * lit-localize, this directory will contain:
29
+ *
30
+ * 1. localization.ts -- A TypeScript module that exports the `msg` function,
31
+ * along with other utilities.
32
+ *
33
+ * 2. <locale>.ts -- For each `targetLocale`, a TypeScript module that exports
34
+ * the translations in that locale keyed by message ID. These modules are
35
+ * used automatically by localization.ts and should not typically be
36
+ * imported directly by user code.
37
+ */
38
+ outputDir : string ;
39
+
40
+ /**
41
+ * The initial locale, if no other explicit locale selection has been made.
42
+ * Defaults to the value of `sourceLocale`.
43
+ *
44
+ * @TJS -type string
45
+ */
46
+ defaultLocale ?: Locale ;
47
+
48
+ /**
49
+ * If true, export a `setLocale(locale: Locale)` function in the generated
50
+ * `<outputDir>/localization.ts` module. Defaults to false.
51
+ *
52
+ * Note that calling this function will set the locale for subsequent calls to
53
+ * `msg`, but will not automatically re-render existing templates.
54
+ */
55
+ exportSetLocaleFunction ?: boolean ;
56
+
57
+ /**
58
+ * Automatically set the locale based on the URL at application startup.
59
+ */
60
+ setLocaleFromUrl ?: {
61
+ /**
62
+ * Set locale based on matching a regular expression against the URL.
63
+ *
64
+ * The regexp will be matched against `window.location.href`, and the first
65
+ * capturing group will be used as the locale. If no match is found, or if
66
+ * the capturing group does not contain a valid locale code, then
67
+ * `defaultLocale` is used.
68
+ *
69
+ * Optionally use the special string `:LOCALE:` to substitute a capturing
70
+ * group into the regexp that will only match the currently configured
71
+ * locale codes (`sourceLocale` and `targetLocales`). For example, if
72
+ * sourceLocale=en and targetLocales=es,zh_CN, then the regexp
73
+ * "^https?://:LOCALE:\\." becomes "^https?://(en|es|zh_CN)\\.".
74
+ *
75
+ * Tips: Remember to double-escape literal backslashes (once for JSON, once
76
+ * for the regexp), and note that you can use `(?:foo)` to create a
77
+ * non-capturing group.
78
+ *
79
+ * It is an error to set both `regexp` and `param`.
80
+ *
81
+ * Examples:
82
+ *
83
+ * 1. "^https?://[^/]+/:LOCALE:(?:$|[/?#])"
84
+ *
85
+ * Set locale from the first path component.
86
+ *
87
+ * E.g. https://www.example.com/es/foo
88
+ * ^^
89
+ *
90
+ * 2. "^https?://:LOCALE:\\."
91
+ *
92
+ * Set locale from the first subdomain.
93
+ *
94
+ * E.g. https://es.example.com/foo
95
+ * ^^
96
+ */
97
+ regexp ?: string ;
98
+
99
+ /**
100
+ * Set locale based on the value of a URL query parameter.
101
+ *
102
+ * Finds the first matching query parameter from `window.location.search`.
103
+ * If no such URL query parameter is set, or if it is not a valid locale
104
+ * code, then `defaultLocale` is used.
105
+ *
106
+ * It is an error to set both `regexp` and `param`.
107
+ *
108
+ * Examples:
109
+ *
110
+ * 1. "lang"
111
+ *
112
+ * https://example.com?foo&lang=es&bar
113
+ * ^^
114
+ */
115
+ param ?: string ;
116
+ } ;
117
+ }
118
+
119
+ /**
120
+ * Write output for the `runtime` output mode.
121
+ */
122
+ export function runtimeOutput (
123
+ messages : ProgramMessage [ ] ,
124
+ translationMap : Map < Locale , Message [ ] > ,
125
+ config : Config ,
126
+ runtimeConfig : RuntimeOutputConfig
127
+ ) {
128
+ // Write our "localization.ts" TypeScript module. This is the file that
129
+ // implements the "msg" function for our TypeScript program.
130
+ const ts = generateMsgModule ( messages , config , runtimeConfig ) ;
131
+ const tsFilename = pathLib . join (
132
+ config . resolve ( runtimeConfig . outputDir ) ,
133
+ 'localization.ts'
134
+ ) ;
135
+ try {
136
+ fs . writeFileSync ( tsFilename , ts ) ;
137
+ } catch ( e ) {
138
+ throw new KnownError (
139
+ `Error writing TypeScript file: ${ tsFilename } \n` +
140
+ `Does the parent directory exist, ` +
141
+ `and do you have write permission?\n` +
142
+ e . message
143
+ ) ;
144
+ }
145
+ // For each translated locale, generate a "<locale>.ts" TypeScript module that
146
+ // contains the mapping from message ID to each translated version. The
147
+ // "localization.ts" file we generated earlier knows how to import and switch
148
+ // between these maps.
149
+ for ( const locale of config . targetLocales ) {
150
+ const translations = translationMap . get ( locale ) || [ ] ;
151
+ const ts = generateLocaleModule (
152
+ locale ,
153
+ translations ,
154
+ messages ,
155
+ config . patches || { }
156
+ ) ;
157
+ fs . writeFileSync (
158
+ pathLib . join ( config . resolve ( runtimeConfig . outputDir ) , `${ locale } .ts` ) ,
159
+ ts
160
+ ) ;
161
+ }
162
+ }
17
163
18
164
/**
19
165
* Generate a TypeScript module which exports:
@@ -30,7 +176,7 @@ import {KnownError} from './error';
30
176
* generator can be referenced in `msg` calls, and only our supported locales
31
177
* can be switched to by `setLocale`.
32
178
*/
33
- export function generateMsgModule (
179
+ function generateMsgModule (
34
180
msgs : Message [ ] ,
35
181
config : Config ,
36
182
runtime : RuntimeOutputConfig
@@ -215,7 +361,7 @@ function genLocaleInitialization(
215
361
* Generate a "<locale>.ts" TypeScript module from the given bundle of
216
362
* translated messages.
217
363
*/
218
- export function generateLocaleModule (
364
+ function generateLocaleModule (
219
365
locale : Locale ,
220
366
translations : Message [ ] ,
221
367
canonMsgs : ProgramMessage [ ] ,
0 commit comments