-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
4,141 additions
and
1,559 deletions.
There are no files selected for viewing
This file was deleted.
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
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 |
---|---|---|
@@ -1,48 +1,3 @@ | ||
const glob = jest.genMockFromModule('glob'); | ||
|
||
const files = [ | ||
'src/pages/Search/index.js', | ||
'src/pages/About/index.js', | ||
'src/index.js', | ||
'src/test.js' | ||
]; | ||
|
||
glob.sync.mockImplementation((pattern) => { | ||
if (pattern === 'test no files') { | ||
return []; | ||
} else if (pattern == 'test removed file') { | ||
return files.slice(1); | ||
} else if (pattern === 'test added file') { | ||
return files.concat(['src/pages/Added/index.js']); | ||
} else if (pattern === 'test files') { | ||
return files; | ||
} else if (pattern === 'test removed directory target') { | ||
return [ | ||
'src/pages', | ||
'src/application', | ||
'src/pages/About', | ||
]; | ||
} else if (pattern === 'test added directory target') { | ||
return [ | ||
'src/pages', | ||
'src/application', | ||
'src/pages/About', | ||
'src/pages/Added', | ||
]; | ||
} else if (pattern === 'test directory targets') { | ||
return [ | ||
'src/pages', | ||
'src/application', | ||
'src/pages/Search', | ||
'src/pages/About', | ||
]; | ||
} else if (pattern === 'test empty keys') { | ||
return [ | ||
'src/pages/EmptyKeys/index.js', | ||
]; | ||
} | ||
|
||
return []; | ||
}); | ||
|
||
module.exports = glob; |
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,5 @@ | ||
module.exports = { | ||
NEW: 'new', | ||
UNUSED: 'unused', | ||
EXISTING: 'existing', | ||
}; |
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,76 @@ | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const toKey = require('./toKey'); | ||
|
||
/** | ||
* Compresses the translation for use by code, removing any extra context information | ||
* when not needed. (If there is only a default context, the context object is removed | ||
* and the translation is linked directly to the key for example.) | ||
* @private | ||
*/ | ||
function collapseLocalization(localization) { | ||
return Object.entries(localization.reduce((accumulator, l) => { | ||
const key = toKey(l.key, l.plural); | ||
const context = l.context || 'default'; | ||
const value = Object.assign({}, accumulator[key] || {}, {[context]: l.value}); | ||
|
||
return Object.assign({}, accumulator, {[key]: value}); | ||
}, {})).reduce((accumulator, [key, value]) => { | ||
const keys = new Set(Object.keys(value)); | ||
|
||
if (keys.size === 1 && keys.has('default')) { | ||
return Object.assign({}, accumulator, {[key]: value.default}); | ||
} | ||
|
||
return Object.assign({}, accumulator, {[key]: value}); | ||
}, {}); | ||
} | ||
|
||
function collapseLocalizations(localizations) { | ||
return Object.entries(localizations).reduce((accumulator, [locale, localization]) => { | ||
return Object.assign({}, accumulator, { | ||
[locale]: collapseLocalization(localization) | ||
}); | ||
}, {}); | ||
} | ||
|
||
class TargetDirectory { | ||
constructor(directoryPath) { | ||
this.cache = {} | ||
this.directoryPath = directoryPath; | ||
} | ||
|
||
buildFilename(locale) { | ||
const basename = [locale, 'json'].filter(p => p).join('.'); | ||
|
||
return path.resolve(this.directoryPath, `${basename}`); | ||
} | ||
|
||
ensureDirectory() { | ||
fs.ensureDirSync(this.directoryPath); | ||
} | ||
|
||
writeIndex(localizations) { | ||
const filename = this.buildFilename('index'); | ||
const collapsed = collapseLocalizations(localizations); | ||
const serialized = JSON.stringify(collapsed, null, 4); | ||
|
||
if (this.cache[filename] !== serialized) { | ||
fs.writeFileSync(filename, serialized); | ||
this.cache[filename] = serialized; | ||
} | ||
} | ||
|
||
writeLocalization(locale, localization) { | ||
const filename = this.buildFilename(locale); | ||
const collapsed = collapseLocalization(localization); | ||
const serialized = JSON.stringify(collapsed, null, 4); | ||
|
||
if (this.cache[filename] !== serialized) { | ||
fs.writeFileSync(filename, serialized); | ||
this.cache[filename] = serialized; | ||
} | ||
} | ||
} | ||
|
||
module.exports = TargetDirectory; |
Oops, something went wrong.