|
|
@ -1,6 +1,9 @@ |
|
|
|
import { app, remote } from 'electron' |
|
|
|
import Store from 'electron-store' |
|
|
|
import { addLocaleData } from 'react-intl' |
|
|
|
import Store from 'electron-store' |
|
|
|
import get from 'lodash.get' |
|
|
|
import { lookup } from 'country-data-lookup' |
|
|
|
import createDebug from 'debug' |
|
|
|
|
|
|
|
// Load locale data.
|
|
|
|
import bg from 'react-intl/locale-data/bg' |
|
|
@ -41,6 +44,8 @@ import ukTranslationMessages from '../../translations/uk-UA.json' |
|
|
|
import zhCNTranslationMessages from '../../translations/zh-CN.json' |
|
|
|
import zhTWTranslationMessages from '../../translations/zh-TW.json' |
|
|
|
|
|
|
|
const debug = createDebug('zap:i18n') |
|
|
|
|
|
|
|
// Add locale data.
|
|
|
|
addLocaleData([ |
|
|
|
...bg, |
|
|
@ -83,24 +88,31 @@ export const locales = [ |
|
|
|
'zh' |
|
|
|
] |
|
|
|
|
|
|
|
function getDefaltLocale() { |
|
|
|
const store = new Store({ name: 'settings' }) |
|
|
|
|
|
|
|
// Detect user language.
|
|
|
|
let language = store.get('locale') || (app || remote.app).getLocale() |
|
|
|
|
|
|
|
// If the detected language is not available, strip out any regional component and check again.
|
|
|
|
if (!locales.includes(language)) { |
|
|
|
language = language.toLowerCase().split(/[_-]+/)[0] |
|
|
|
} |
|
|
|
// If we still can't find the users language, default to english.
|
|
|
|
if (!locales.includes(language)) { |
|
|
|
language = 'en' |
|
|
|
} |
|
|
|
return language |
|
|
|
} |
|
|
|
|
|
|
|
export const DEFAULT_LOCALE = getDefaltLocale() |
|
|
|
// Defaine list of currencies that we will support.
|
|
|
|
export const currencies = [ |
|
|
|
'USD', |
|
|
|
'EUR', |
|
|
|
'JPY', |
|
|
|
'GBP', |
|
|
|
'CAD', |
|
|
|
'KRW', |
|
|
|
'AUD', |
|
|
|
'BRL', |
|
|
|
'CHF', |
|
|
|
'CLP', |
|
|
|
'CNY', |
|
|
|
'DKK', |
|
|
|
'HKD', |
|
|
|
'INR', |
|
|
|
'ISK', |
|
|
|
'NZD', |
|
|
|
'PLN', |
|
|
|
'RUB', |
|
|
|
'SEK', |
|
|
|
'SGD', |
|
|
|
'THB', |
|
|
|
'TWB' |
|
|
|
] |
|
|
|
|
|
|
|
// Collate all translations.
|
|
|
|
export const translationMessages = { |
|
|
@ -123,3 +135,62 @@ export const translationMessages = { |
|
|
|
tr: trTranslationMessages, |
|
|
|
uk: ukTranslationMessages |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the most appropriate language code. |
|
|
|
* @return {string} Language code. |
|
|
|
*/ |
|
|
|
export const getLocale = () => { |
|
|
|
const store = new Store({ name: 'settings' }) |
|
|
|
const userLocale = store.get('locale') |
|
|
|
if (userLocale) { |
|
|
|
debug('Determined locale as %s from settings', userLocale) |
|
|
|
return userLocale |
|
|
|
} |
|
|
|
const defaultLocale = (app || remote.app).getLocale() || 'en-US' |
|
|
|
const language = defaultLocale.toLowerCase().split(/[_-]+/)[0] |
|
|
|
let locale = 'en' |
|
|
|
if (locales.includes(language)) { |
|
|
|
locale = language |
|
|
|
} |
|
|
|
if (locales.includes(defaultLocale)) { |
|
|
|
locale = userLocale |
|
|
|
} |
|
|
|
debug('Determined locale as %s', locale) |
|
|
|
return locale |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the most appropriate language code. |
|
|
|
* @return {string} Language code. |
|
|
|
*/ |
|
|
|
export const getLanguageName = lang => { |
|
|
|
const language = lang.toLowerCase().split(/[_-]+/)[0] |
|
|
|
const data = lookup.languages({ alpha2: language }) |
|
|
|
const name = get(data, '[0]name', language) |
|
|
|
debug('Determined language as %s', name) |
|
|
|
return name |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the most appropriate currency code. |
|
|
|
* @return {string} Currency code. |
|
|
|
*/ |
|
|
|
export const getCurrency = () => { |
|
|
|
const store = new Store({ name: 'settings' }) |
|
|
|
const userCurrency = store.get('fiatTicker') |
|
|
|
if (userCurrency) { |
|
|
|
debug('Determined currency as %s from settings', userCurrency) |
|
|
|
return userCurrency |
|
|
|
} |
|
|
|
const defaultLocale = (app || remote.app).getLocale() || 'en-US' |
|
|
|
const country = defaultLocale.split(/[_-]+/)[1] |
|
|
|
const data = lookup.countries({ alpha2: country }) |
|
|
|
const detectedCurrency = get(data, '[0]currencies[0]', 'USD') |
|
|
|
let currency = 'USD' |
|
|
|
if (currencies.includes(detectedCurrency)) { |
|
|
|
currency = detectedCurrency |
|
|
|
} |
|
|
|
debug('Determined currency as %s', currency) |
|
|
|
return currency |
|
|
|
} |
|
|
|