-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieve localized strings from SplatNet
- Loading branch information
1 parent
d556047
commit 47d0abb
Showing
8 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
import mkdirp from 'mkdirp'; | ||
import jsonpath from 'jsonpath'; | ||
import get from 'lodash/get.js'; | ||
import set from 'lodash/set.js'; | ||
|
||
function makeArray(value) { | ||
return Array.isArray(value) ? value : [value]; | ||
} | ||
|
||
export class LocalizationProcessor { | ||
outputDirectory = 'dist/data/locale'; | ||
|
||
constructor(locale, rulesets) { | ||
this.locale = locale; | ||
this.rulesets = rulesets; | ||
} | ||
|
||
get filename() { | ||
return `${this.outputDirectory}/${this.locale.code}.json`; | ||
} | ||
|
||
*rulesetIterations() { | ||
for (let ruleset of this.rulesets) { | ||
for (let node of makeArray(ruleset.nodes)) { | ||
for (let value of makeArray(ruleset.values)) { | ||
yield { | ||
key: ruleset.key, | ||
node, | ||
id: ruleset.id, | ||
value, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
*dataIterations(data) { | ||
for (let ruleset of this.rulesetIterations()) { | ||
for (let node of jsonpath.query(data, ruleset.node)) { | ||
let id = get(node, ruleset.id); | ||
|
||
yield { | ||
ruleset, | ||
node, | ||
id, | ||
value: get(node, ruleset.value), | ||
path: `${ruleset.key}.${id}.${ruleset.value}`, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
async updateLocalizations(data) { | ||
let localizations = await this.readData(); | ||
|
||
for (let { path, value } of this.dataIterations(data)) { | ||
set(localizations, path, value); | ||
} | ||
|
||
await this.writeData(localizations); | ||
} | ||
|
||
async hasMissingLocalizations(data) { | ||
let localizations = await this.readData(); | ||
|
||
for (let { path } of this.dataIterations(data)) { | ||
if (get(localizations, path) === undefined) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
async writeData(data) { | ||
// If we're running in debug mode, format the JSON output so it's easier to read | ||
let debug = !!process.env.DEBUG; | ||
let space = debug ? 2 : undefined; | ||
|
||
data = JSON.stringify(data, undefined, space); | ||
|
||
await mkdirp(path.dirname(this.filename)) | ||
await fs.writeFile(this.filename, data); | ||
} | ||
|
||
async readData() { | ||
try { | ||
let result = await fs.readFile(this.filename); | ||
|
||
return JSON.parse(result) || {}; | ||
} catch (e) { | ||
// | ||
} | ||
|
||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters