2016-06-07 22:34:17 +00:00
|
|
|
const {ipcRenderer} = require('electron')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
2016-06-07 22:36:08 +00:00
|
|
|
const getExtensionMetadata = () => {
|
|
|
|
return ipcRenderer.sendSync('CHROME_I18N_MANIFEST', chrome.runtime.id)
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:34:17 +00:00
|
|
|
const getMessagesPath = (language) => {
|
2016-06-07 22:36:08 +00:00
|
|
|
const {srcDirectory, default_locale} = getExtensionMetadata()
|
|
|
|
const localesDirectory = path.join(srcDirectory, '_locales')
|
|
|
|
let messagesPath = path.join(localesDirectory, language, 'messages.json')
|
2016-06-07 22:34:17 +00:00
|
|
|
if (!fs.statSyncNoException(messagesPath)) {
|
2016-06-07 22:36:08 +00:00
|
|
|
messagesPath = path.join(localesDirectory, default_locale, 'messages.json')
|
2016-06-07 22:34:17 +00:00
|
|
|
}
|
|
|
|
return messagesPath
|
|
|
|
}
|
|
|
|
|
|
|
|
const getMessages = (language) => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(fs.readFileSync(getMessagesPath(language))) || {}
|
|
|
|
} catch (error) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLanguage = () => {
|
|
|
|
return navigator.language.replace(/-.*$/, '').toLowerCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getMessage (messageName, substitutions) {
|
|
|
|
const language = getLanguage()
|
|
|
|
const messages = getMessages(language)
|
|
|
|
if (messages.hasOwnProperty(messageName)) {
|
|
|
|
return messages[messageName].message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|