2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-06-07 23:03:43 +00:00
|
|
|
// Implementation of chrome.i18n.getMessage
|
|
|
|
// https://developer.chrome.com/extensions/i18n#method-getMessage
|
|
|
|
//
|
|
|
|
// Does not implement predefined messages:
|
|
|
|
// https://developer.chrome.com/extensions/i18n#overview-predefined
|
|
|
|
|
2018-10-06 11:48:00 +00:00
|
|
|
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
2016-06-07 22:34:17 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
2016-06-07 23:10:27 +00:00
|
|
|
let metadata
|
|
|
|
|
2016-06-08 00:00:53 +00:00
|
|
|
const getExtensionMetadata = (extensionId) => {
|
2016-06-07 23:10:27 +00:00
|
|
|
if (!metadata) {
|
2016-06-08 00:00:53 +00:00
|
|
|
metadata = ipcRenderer.sendSync('CHROME_I18N_MANIFEST', extensionId)
|
2016-06-07 23:10:27 +00:00
|
|
|
}
|
|
|
|
return metadata
|
2016-06-07 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 00:00:53 +00:00
|
|
|
const getMessagesPath = (extensionId, language) => {
|
2016-06-09 21:17:54 +00:00
|
|
|
const metadata = getExtensionMetadata(extensionId)
|
|
|
|
const localesDirectory = path.join(metadata.srcDirectory, '_locales')
|
2018-09-06 18:06:32 +00:00
|
|
|
try {
|
|
|
|
const filename = path.join(localesDirectory, language, 'messages.json')
|
|
|
|
fs.accessSync(filename, fs.constants.R_OK)
|
|
|
|
return filename
|
|
|
|
} catch (err) {
|
|
|
|
const defaultLocale = metadata.default_locale || 'en'
|
|
|
|
return path.join(localesDirectory, defaultLocale, 'messages.json')
|
2016-06-07 22:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 00:00:53 +00:00
|
|
|
const getMessages = (extensionId, language) => {
|
2016-06-07 22:34:17 +00:00
|
|
|
try {
|
2016-06-08 00:00:53 +00:00
|
|
|
const messagesPath = getMessagesPath(extensionId, language)
|
|
|
|
return JSON.parse(fs.readFileSync(messagesPath)) || {}
|
2016-06-07 22:34:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLanguage = () => {
|
|
|
|
return navigator.language.replace(/-.*$/, '').toLowerCase()
|
|
|
|
}
|
|
|
|
|
2016-06-07 23:03:43 +00:00
|
|
|
const replaceNumberedSubstitutions = (message, substitutions) => {
|
|
|
|
return message.replace(/\$(\d+)/, (_, number) => {
|
|
|
|
const index = parseInt(number, 10) - 1
|
|
|
|
return substitutions[index] || ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const replacePlaceholders = (message, placeholders, substitutions) => {
|
|
|
|
if (typeof substitutions === 'string') {
|
|
|
|
substitutions = [substitutions]
|
|
|
|
}
|
|
|
|
if (!Array.isArray(substitutions)) {
|
|
|
|
substitutions = []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placeholders) {
|
|
|
|
Object.keys(placeholders).forEach((name) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
let { content } = placeholders[name]
|
2016-06-07 23:03:43 +00:00
|
|
|
content = replaceNumberedSubstitutions(content, substitutions)
|
|
|
|
message = message.replace(new RegExp(`\\$${name}\\$`, 'gi'), content)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return replaceNumberedSubstitutions(message, substitutions)
|
|
|
|
}
|
|
|
|
|
2016-06-08 00:00:53 +00:00
|
|
|
const getMessage = (extensionId, messageName, substitutions) => {
|
|
|
|
const messages = getMessages(extensionId, getLanguage())
|
|
|
|
if (messages.hasOwnProperty(messageName)) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { message, placeholders } = messages[messageName]
|
2016-06-08 00:00:53 +00:00
|
|
|
return replacePlaceholders(message, placeholders, substitutions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.setup = (extensionId) => {
|
|
|
|
return {
|
|
|
|
getMessage (messageName, substitutions) {
|
|
|
|
return getMessage(extensionId, messageName, substitutions)
|
2016-06-07 22:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|