-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from favoloso/feat-translations
✨ Add translations support for headings and rules
- Loading branch information
Showing
23 changed files
with
199 additions
and
32 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
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,42 @@ | ||
function translator() { | ||
return require("../src/translation/translator")(); | ||
} | ||
|
||
function setConfig(other) { | ||
const config = require("../src/config/config-default"); | ||
jest.setMock("../src/config/config", { | ||
...config, | ||
...other | ||
}); | ||
} | ||
|
||
describe("translator", () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
it("should throw if language is not found", () => { | ||
setConfig({ language: "xyz-hello" }); | ||
expect(() => translator().translateRule("abc")).toThrowError( | ||
/^Language "xyz-hello" is not available\./ | ||
); | ||
}); | ||
|
||
describe("linter rules", () => { | ||
it("should translate correctly in english", () => { | ||
setConfig({ language: "en" }); | ||
expect( | ||
translator().translateRule("body-leading-blank") | ||
).toMatchInlineSnapshot(`"Body should begin with a leading blank line."`); | ||
}); | ||
|
||
it("should translate in another language", () => { | ||
setConfig({ language: "it" }); | ||
expect( | ||
translator().translateRule("body-leading-blank") | ||
).toMatchInlineSnapshot( | ||
`"Il <body> del commit deve iniziare con una nuova linea (\\\\n)"` | ||
); | ||
}); | ||
}); | ||
}); |
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
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
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
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,13 @@ | ||
const emoji = require("../emoji/emoji"); | ||
|
||
module.exports = function findGroupByHeading(strings, heading) { | ||
const translatedType = Object.keys(strings.headings).find( | ||
key => strings.headings[key] === heading | ||
); | ||
|
||
if (translatedType) { | ||
return emoji.list.find(e => e.type === translatedType); | ||
} | ||
|
||
return emoji.list.find(e => e.heading === heading); | ||
}; |
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,19 @@ | ||
const fs = require("fs"); | ||
const config = require("../config/config"); | ||
|
||
const stringFiles = fs.readdirSync(__dirname + "/strings"); | ||
const languages = stringFiles.map(f => f.replace(".js", "")); | ||
|
||
module.exports = function getLanguageStrings() { | ||
const language = config.language || "en"; | ||
|
||
if (languages.indexOf(language) === -1) { | ||
throw new Error( | ||
`Language "${language}" is not available. Please use on of: ${languages.join( | ||
", " | ||
)}` | ||
); | ||
} | ||
|
||
return require(`${__dirname}/strings/${language}.js`); | ||
}; |
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 @@ | ||
module.exports = { | ||
headings: { | ||
/** Already provided in emoji-groups */ | ||
}, | ||
rules: { | ||
"body-leading-blank": "Body should begin with a leading blank line.", | ||
"emoji-from-type": `Type Alias "$0" is not allowed. It should be one of: $1.`, | ||
"emoji-known": `Emoji "$0" is not allowed. It should be one of: $1.`, | ||
"emoji-require": `Emoji is required, but it's not present in "$0".`, | ||
"header-max-length": `Header max length is $0 characters, $1 provided.`, | ||
"subject-require": `Subject is required, but it's not present in "$0".` | ||
} | ||
}; |
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,22 @@ | ||
module.exports = { | ||
headings: { | ||
fix: `🐛 Bug Fix`, | ||
improvement: `🛠 Migliorie`, | ||
feat: `✨ Nuove Funzionalità`, | ||
chore: `🏗 Modifiche Interne`, | ||
perf: `⚡️ Performance`, | ||
refactor: `♻️ Refactor`, | ||
docs: `📚 Documentazione`, | ||
breaking: `🚨 Breaking Change`, | ||
security: `🔓 Sicurezza` | ||
}, | ||
rules: { | ||
"body-leading-blank": | ||
"Il <body> del commit deve iniziare con una nuova linea (\\n)", | ||
"emoji-from-type": `Il tipo "$0" non è permesso. Deve essere uno fra: $1.`, | ||
"emoji-known": `L'Emoji "$0" non è permessa. Deve essere una fra: $1.`, | ||
"emoji-require": `L'Emoji è richiesta, ma non è presente nè deducibile dal tipo in "$0".`, | ||
"header-max-length": `La lunghezza massima dell'intestazione è di $0 caratteri, $1 forniti.`, | ||
"subject-require": `Il Soggetto è richiesto, ma non è presente in "$0".` | ||
} | ||
}; |
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,8 @@ | ||
/** | ||
* Provides heading translation based on config locale | ||
*/ | ||
module.exports = function translateHeading(strings, group) { | ||
return strings.headings[group.type] | ||
? strings.headings[group.type] | ||
: group.heading; | ||
}; |
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,10 @@ | ||
/** | ||
* Given a string, translate rule message passing arguments and replacing | ||
* theme inside the string given their index, i.e. `$0` for the first arg, etc. | ||
*/ | ||
module.exports = function translateRule(strings, rule, ...args) { | ||
return strings.rules[rule].replace( | ||
/\$([0-9]+)/, | ||
(match, index) => args[parseInt(index, 10)] | ||
); | ||
}; |
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,14 @@ | ||
const getLanguageStrings = require("./get-language-strings"); | ||
const translateHeading = require("./translate-heading"); | ||
const translateRule = require("./translate-rule"); | ||
const findGroupByHeading = require("./find-group-by-heading"); | ||
|
||
module.exports = function() { | ||
const strings = getLanguageStrings(); | ||
|
||
return { | ||
translateHeading: group => translateHeading(strings, group), | ||
translateRule: (rule, args) => translateRule(strings, rule, args), | ||
findGroupByHeading: heading => findGroupByHeading(strings, heading) | ||
}; | ||
}; |