-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathlingui.js
170 lines (139 loc) · 4.26 KB
/
lingui.js
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
// @flow
import fs from "fs"
import path from "path"
import mkdirp from "mkdirp"
import glob from "glob"
import R from "ramda"
import type { LinguiConfig, CatalogFormat } from "./types"
import * as locales from "./utils/locales"
const sourceFilename = path.join("{locale}", "messages.json")
const compiledFilename = path.join("{locale}", "messages.js")
export default (config: LinguiConfig): CatalogFormat => ({
formatFilename(pattern, locale) {
return pattern.replace("{locale}", locale)
},
write(locale, messages) {
const filename = path.join(
config.localeDir,
this.formatFilename(sourceFilename, locale)
)
const created = !fs.existsSync(filename)
fs.writeFileSync(filename, JSON.stringify(messages, null, 2))
return [created, filename]
},
read(locale) {
const filename = path.join(
config.localeDir,
this.formatFilename(sourceFilename, locale)
)
if (!fs.existsSync(filename)) return null
const raw = fs.readFileSync(filename).toString()
try {
return JSON.parse(raw)
} catch (e) {
return null
}
},
merge(nextCatalog) {
const nextKeys = R.keys(nextCatalog)
return R.mergeAll(
this.getLocales().map(locale => {
const prevCatalog = this.read(locale)
if (!prevCatalog) return nextCatalog
const prevKeys = R.keys(prevCatalog)
const newKeys = R.difference(nextKeys, prevKeys)
const mergeKeys = R.intersection(nextKeys, prevKeys)
const obsoleteKeys = R.difference(prevKeys, nextKeys)
// Initialize new catalog with new keys
const newMessages = R.mapObjIndexed(
(message, key) => ({
translation: config.sourceLocale === locale ? key : "",
...message
}),
R.pick(newKeys, nextCatalog)
)
// Merge translations from previous catalog
const mergedMessages = mergeKeys.map(key => ({
[key]: {
translation: prevCatalog[key].translation,
...R.omit(["obsolete, translation"], nextCatalog[key])
}
}))
// Mark all remaining translations as obsolete
const obsoleteMessages = obsoleteKeys.map(key => ({
[key]: {
...prevCatalog[key],
obsolete: true
}
}))
const newCatalog = R.mergeAll([
newMessages,
...mergedMessages,
...obsoleteMessages
])
return { [locale]: newCatalog }
})
)
},
getTranslation(catalogs, locale, key, { fallbackLocale, sourceLocale }) {
const getTranslation = locale => catalogs[locale][key].translation
return (
// Get translation in target locale
getTranslation(locale) ||
// Get translation in fallbackLocale (if any)
(fallbackLocale && getTranslation(fallbackLocale)) ||
// Get message default
catalogs[locale][key].defaults ||
// If sourceLocale is either target locale of fallback one, use key
(sourceLocale && sourceLocale === locale && key) ||
(sourceLocale &&
fallbackLocale &&
sourceLocale === fallbackLocale &&
key) ||
// Otherwise no translation is available
undefined
)
},
writeCompiled(locale, content) {
const filename = path.join(
config.localeDir,
this.formatFilename(compiledFilename, locale)
)
fs.writeFileSync(filename, content)
return filename
},
getLocale(filename) {
const filenameRe = new RegExp(
this.formatFilename(sourceFilename, locales.localeRe.source)
)
const match = filenameRe.exec(filename)
if (!match) return null
return match[1]
},
getLocales() {
const pattern = path.join(
config.localeDir,
this.formatFilename(sourceFilename, "*")
)
return glob
.sync(pattern)
.map(filename => this.getLocale(filename))
.filter(Boolean)
},
addLocale(locale) {
if (!locales.isValid(locale)) {
return [false, null]
}
const filename = path.join(
config.localeDir,
this.formatFilename(sourceFilename, locale)
)
if (!fs.existsSync(filename)) {
const dirname = path.dirname(filename)
mkdirp.sync(dirname)
this.write(locale, {})
return [true, filename]
}
return [false, filename]
}
})