-
Notifications
You must be signed in to change notification settings - Fork 324
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
3 changed files
with
101 additions
and
7 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,59 @@ | ||
'use strict' | ||
/* eslint-env browser, webextensions */ | ||
|
||
const browser = require('webextension-polyfill') | ||
|
||
/** | ||
* Renders a translated string with html anchors. | ||
* @param {String} message The message to translate. | ||
* @param {Array} links The array of hrefs. | ||
* @param {Object} attributes HTML attributes to put in the anchor. | ||
* @return {html} An HTML node with the translated string with anchors. | ||
*/ | ||
const renderTranslatedLink = (message, links, attributes) => { | ||
const regex = /<a>(.+?)<\/a>/mg | ||
const str = browser.i18n.getMessage(message) | ||
let match = regex.exec(str) | ||
|
||
let output = str | ||
let i = 0 | ||
while (match !== null) { | ||
output = output.replace(match[0], `<a href="${links[i]}" ${attributes}>${match[1]}</a>`) | ||
match = regex.exec(str) | ||
i++ | ||
} | ||
|
||
const template = document.createElement('template') | ||
template.innerHTML = output | ||
|
||
return template.content | ||
} | ||
|
||
/** | ||
* Renders a translated string with html spans. | ||
* @param {String} message - The message to translate. | ||
* @param {String} dynamicData - The dynamic data in the translation. | ||
* @param {Object} attributes HTML attributes to put in the anchor. | ||
* @return {html} An HTML node with the translated string with spans. | ||
*/ | ||
const renderTranslatedDynamicSpan = (message, dynamicData, attributes) => { | ||
const regex = /<span><\/span>/mg | ||
const str = browser.i18n.getMessage(message) | ||
let match = regex.exec(str) | ||
|
||
let output = str | ||
while (match !== null) { | ||
output = output.replace(match[0], `<span ${attributes}>${dynamicData}</span>`) | ||
match = regex.exec(str) | ||
} | ||
|
||
const template = document.createElement('template') | ||
template.innerHTML = output | ||
|
||
return template.content | ||
} | ||
|
||
module.exports = { | ||
renderTranslatedLink, | ||
renderTranslatedDynamicSpan | ||
} |