-
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
0 parents
commit 6ab5b93
Showing
7 changed files
with
392 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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 @@ | ||
node_modules/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Infermedica | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,144 @@ | ||
# Infermedica Object Override Loader | ||
|
||
This Webpack loader aims to override parts of YAML or JSON files with values from corresponding files in a specified | ||
directory. | ||
|
||
## Installation | ||
|
||
```bash | ||
$ npm install --save-dev @infermedica/object-override-loader | ||
``` | ||
|
||
## Usage | ||
|
||
To use this loader, include it in your Webpack configuration file and provide a `objectsDir` path, e.g.: | ||
|
||
```js | ||
{ | ||
test: /\.ya?ml$/, | ||
use: [ | ||
'js-yaml-loader', | ||
{ | ||
loader: '@infermedica/object-override-loader', | ||
options: { | ||
objectsDir: 'path/to/directory' | ||
} | ||
} | ||
] | ||
}, | ||
``` | ||
|
||
## Package purpose | ||
|
||
This loader has been created specifically to allow overriding of translations in vue-i18n `<i18>` blocks. If you have | ||
a core library that is later included in other applications, you can use this loader to override only selected | ||
translations from the core library in other applications. | ||
|
||
Let's say you have following Vue file with `<i18n>` translation block in your core library: | ||
```yaml | ||
<i18n> | ||
en: | ||
SegmentFeedback: | ||
question: "Is the information on this site helpful?" | ||
commentLabel: "Comment" | ||
commentLabelOptionl: "(optional)" | ||
sendButtonText: "Send feedback" | ||
</i18n> | ||
``` | ||
|
||
Other translations can also be loaded globally: | ||
```javascript | ||
import en from './en.yaml'; | ||
import pl from './pl.yaml'; | ||
|
||
Vue.use(VueI18n); | ||
|
||
const i18n = new VueI18n({ | ||
messages: { | ||
en, | ||
pl | ||
} | ||
}); | ||
|
||
new Vue({ | ||
i18n, | ||
render: (h) => h(App) | ||
}).$mount('#app'); | ||
``` | ||
|
||
```yaml | ||
# pl.yaml | ||
SegmentFeedback: | ||
question: "Czy infomacje na tej stornie są pomocne?" | ||
commentLabel: "Komentarz" | ||
commentLabelOptionl: "(opcjonalnie)" | ||
sendButtonText: "Wyślij opinię" | ||
``` | ||
To override some of the above translation in other project, include this loader in your Webpack configuration file: | ||
```js | ||
{ | ||
resourceQuery: /blockType=i18n/, | ||
type: 'javascript/auto', | ||
use: [ | ||
'@kazupon/vue-i18n-loader', | ||
'js-yaml-loader', | ||
{ | ||
loader: '@infermedica/object-override-loader', | ||
options: { | ||
objectsDir: 'path/to/directory' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
test: /\.ya?ml$/, | ||
use: [ | ||
'js-yaml-loader', | ||
{ | ||
loader: '@infermedica/object-override-loader', | ||
options: { | ||
objectsDir: 'path/to/directory' | ||
} | ||
} | ||
] | ||
}, | ||
``` | ||
|
||
The loader looks for overrides in `path/to/directory` directory: | ||
|
||
```yaml | ||
$ ls path/to/directory | ||
en.yaml pl.yaml | ||
``` | ||
|
||
```yaml | ||
# path/to/directory/en.yaml | ||
SegmentFeedback: | ||
question: "How would you rate us?" | ||
sendButtonText: "Send rating" | ||
``` | ||
```yaml | ||
# path/to/directory/pl.yaml | ||
SegmentFeedback: | ||
question: "Jak byś nas ocenił?" | ||
sendButtonText: "Wyślij ocenę" | ||
``` | ||
After running Webpack, the loader will replace "Is the information on this site helpful?" with | ||
"How would you rate us?" and "Send feedback" with "Send rating" (similarly for Polish translations), | ||
leaving other translations untouched. The final build will contain the merge of core and overridden translations 🎉. | ||
#### Note | ||
Although the loader was created to override vue-i18n translations it can be used to update any YAML or JSON file. | ||
Also, even though examples above use YAML files to store translations, it should work the same with JSON objects. | ||
## Contribution | ||
Feel free to raise an issue if you have any questions or a similar use case. We're happy to accept pull requests too. | ||
## License | ||
MIT Copyright (c) Infermedica |
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,91 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const yaml = require('js-yaml'); | ||
const {getOptions} = require('loader-utils'); | ||
|
||
|
||
const isYAMLFile = (fileExt) => /^\.ya?ml$/.test(fileExt); | ||
const isJSONFile = (fileExt) => /^\.json/.test(fileExt); | ||
|
||
|
||
function updateSourceObject(srcObject, overrideObject) { | ||
for (const srcKey of Object.keys(srcObject)) { | ||
if (overrideObject.hasOwnProperty(srcKey)) { | ||
if (typeof overrideObject[srcKey] === 'object') { | ||
updateSourceObject(srcObject[srcKey], overrideObject[srcKey]); | ||
} else { | ||
srcObject[srcKey] = overrideObject[srcKey]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
function parseContent(mode, source) { | ||
if (mode === 'yaml') { | ||
return typeof source === 'string' ? yaml.safeLoad(source) : source; | ||
} | ||
if (mode === 'json') { | ||
return typeof source === 'string' ? JSON.parse(source) : source; | ||
} | ||
return source; | ||
} | ||
|
||
|
||
function serializeContent(mode, source) { | ||
if (mode === 'yaml') { | ||
return yaml.safeDump(source); | ||
} | ||
if (mode === 'json') { | ||
return JSON.stringify(source); | ||
} | ||
return source; | ||
} | ||
|
||
|
||
module.exports = function (source) { | ||
try { | ||
const resourceObject = path.parse(this.resourcePath); | ||
const options = Object.assign({ | ||
mode: 'yaml', | ||
insertFileNameAsPrefix: isYAMLFile(resourceObject.ext) || isJSONFile(resourceObject.ext), | ||
objectsDir: null | ||
}, getOptions(this)); | ||
|
||
if (!options.objectsDir || !fs.existsSync(options.objectsDir)) { | ||
return source; | ||
} | ||
|
||
const overrides = {}; | ||
fs.readdirSync(options.objectsDir).forEach((fileName) => { | ||
const filePath = path.join(options.objectsDir, fileName); | ||
const fileObject = path.parse(filePath); | ||
|
||
if (isYAMLFile(fileObject.ext) || isJSONFile(fileObject.ext)) { | ||
overrides[fileObject.name] = parseContent( | ||
isYAMLFile(fileObject.ext) ? 'yaml' : 'json', | ||
fs.readFileSync(filePath, 'utf8') | ||
); | ||
} | ||
}); | ||
|
||
if (Object.keys(overrides).length === 0) { | ||
return source; | ||
} | ||
|
||
let content = {}; | ||
if (options.insertFileNameAsPrefix) { | ||
content = {[resourceObject.name]: parseContent(options.mode, source)}; | ||
updateSourceObject(content, overrides); | ||
content = content[resourceObject.name]; | ||
} else { | ||
content = parseContent(options.mode, source); | ||
updateSourceObject(content, overrides); | ||
} | ||
|
||
return serializeContent(options.mode, content); | ||
} catch (err) { | ||
this.emitError(err); | ||
return source; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@infermedica/object-override-loader", | ||
"description": "Object YAML/JSON override loader", | ||
"version": "0.1.0", | ||
"author": "Infermedica", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/infermedica/object-override-loader" | ||
}, | ||
"files": [ | ||
"*.js", | ||
"README.md" | ||
], | ||
"keywords": [ | ||
"i18n", | ||
"loader", | ||
"vue", | ||
"webpack" | ||
], | ||
"dependencies": { | ||
"js-yaml": "^3.12.2", | ||
"loader-utils": "^1.2.3", | ||
"path": "^0.12.7" | ||
} | ||
} |