2018-09-14 06:08:17 +02:00
|
|
|
import Localization from 'react-localization';
|
2018-05-28 21:18:11 +02:00
|
|
|
import { AsyncStorage } from 'react-native';
|
|
|
|
import { AppStorage } from '../class';
|
2018-10-20 23:10:21 +02:00
|
|
|
import { BitcoinUnit } from '../models/bitcoinUnits';
|
2019-01-11 04:28:03 +01:00
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
|
|
|
const dayjs = require('dayjs');
|
2018-12-22 04:23:52 +01:00
|
|
|
const currency = require('../currency');
|
2018-12-23 06:21:43 +01:00
|
|
|
const BigNumber = require('bignumber.js');
|
2018-05-28 21:18:11 +02:00
|
|
|
let strings;
|
2019-01-11 04:28:03 +01:00
|
|
|
dayjs.extend(relativeTime);
|
2018-05-28 21:18:11 +02:00
|
|
|
|
|
|
|
// first-time loading sequence
|
|
|
|
(async () => {
|
|
|
|
// finding out whether lang preference was saved
|
|
|
|
let lang = await AsyncStorage.getItem(AppStorage.LANG);
|
|
|
|
if (lang) {
|
|
|
|
strings.setLanguage(lang);
|
2019-01-11 17:23:09 +01:00
|
|
|
let localeForDayJSAvailable = true;
|
|
|
|
switch (lang) {
|
|
|
|
case 'ru':
|
|
|
|
require('dayjs/locale/ru');
|
|
|
|
break;
|
|
|
|
case 'es':
|
|
|
|
require('dayjs/locale/es');
|
|
|
|
break;
|
|
|
|
case 'fr_fr':
|
|
|
|
require('dayjs/locale/fr');
|
|
|
|
break;
|
|
|
|
case 'pt_br':
|
|
|
|
lang = 'pt-br';
|
|
|
|
require('dayjs/locale/pt-br');
|
|
|
|
break;
|
|
|
|
case 'pt_pt':
|
|
|
|
lang = 'pt';
|
|
|
|
require('dayjs/locale/pt');
|
|
|
|
break;
|
|
|
|
case 'jp_jp':
|
|
|
|
lang = 'ja';
|
|
|
|
require('dayjs/locale/ja');
|
|
|
|
break;
|
|
|
|
case 'de_de':
|
|
|
|
require('dayjs/locale/de');
|
|
|
|
break;
|
|
|
|
case 'th_th':
|
|
|
|
require('dayjs/locale/th');
|
|
|
|
break;
|
|
|
|
case 'da_dk':
|
|
|
|
require('dayjs/locale/da');
|
|
|
|
break;
|
|
|
|
case 'nl_nl':
|
|
|
|
require('dayjs/locale/nl');
|
|
|
|
break;
|
|
|
|
case 'hr_hr':
|
|
|
|
require('dayjs/locale/hr');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
localeForDayJSAvailable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (localeForDayJSAvailable) {
|
|
|
|
dayjs.locale(lang.split('_')[0]);
|
|
|
|
}
|
2018-05-28 21:18:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-14 06:08:17 +02:00
|
|
|
if (Localization.getCurrentLocaleAsync) {
|
|
|
|
let locale = await Localization.getCurrentLocaleAsync();
|
2018-05-28 21:18:11 +02:00
|
|
|
if (locale) {
|
|
|
|
console.log('current locale:', locale);
|
2018-12-31 20:36:45 +01:00
|
|
|
if (
|
|
|
|
locale === 'en' ||
|
|
|
|
locale === 'ru' ||
|
|
|
|
locale === 'ua' ||
|
|
|
|
locale === 'es' ||
|
2019-01-06 17:10:25 +01:00
|
|
|
locale === 'fr-fr' ||
|
2018-12-31 20:36:45 +01:00
|
|
|
locale === 'pt-br' ||
|
|
|
|
locale === 'pt-pt' ||
|
2019-01-10 06:11:39 +01:00
|
|
|
locale === 'jp-JP' ||
|
2019-01-02 13:07:27 +01:00
|
|
|
locale === 'de-de' ||
|
2019-01-05 17:38:28 +01:00
|
|
|
locale === 'cs-cz' ||
|
2019-01-05 21:24:07 +01:00
|
|
|
locale === 'th-th' ||
|
2019-01-06 17:46:31 +01:00
|
|
|
locale === 'da-dk' ||
|
2019-01-08 03:05:40 +01:00
|
|
|
locale === 'nl-nl' ||
|
|
|
|
locale === 'hr-hr'
|
2018-12-31 20:36:45 +01:00
|
|
|
) {
|
2019-01-11 17:23:09 +01:00
|
|
|
switch (locale) {
|
|
|
|
case 'ru':
|
|
|
|
require('dayjs/locale/ru');
|
|
|
|
break;
|
|
|
|
case 'es':
|
|
|
|
require('dayjs/locale/es');
|
|
|
|
break;
|
|
|
|
case 'fr-fr':
|
|
|
|
require('dayjs/locale/fr');
|
|
|
|
break;
|
|
|
|
case 'pt-br':
|
|
|
|
require('dayjs/locale/pt-br');
|
|
|
|
break;
|
|
|
|
case 'pt-pt':
|
|
|
|
require('dayjs/locale/pt');
|
|
|
|
break;
|
|
|
|
case 'jp-JP':
|
|
|
|
require('dayjs/locale/ja');
|
|
|
|
break;
|
|
|
|
case 'de-de':
|
|
|
|
require('dayjs/locale/de');
|
|
|
|
break;
|
|
|
|
case 'th-th':
|
|
|
|
require('dayjs/locale/th');
|
|
|
|
break;
|
|
|
|
case 'da-dk':
|
|
|
|
require('dayjs/locale/da');
|
|
|
|
break;
|
|
|
|
case 'nl-nl':
|
|
|
|
require('dayjs/locale/nl');
|
|
|
|
break;
|
|
|
|
case 'hr-hr':
|
|
|
|
require('dayjs/locale/hr');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dayjs.locale(locale.split('-')[0]);
|
2018-10-08 00:35:12 +02:00
|
|
|
locale = locale.replace('-', '_');
|
2018-05-31 20:38:20 +02:00
|
|
|
strings.setLanguage(locale);
|
2018-05-31 20:43:56 +02:00
|
|
|
} else {
|
|
|
|
strings.setLanguage('en');
|
2018-05-28 21:18:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2018-09-14 06:08:17 +02:00
|
|
|
strings = new Localization({
|
2018-05-28 21:18:11 +02:00
|
|
|
en: require('./en.js'),
|
|
|
|
ru: require('./ru.js'),
|
2018-10-08 00:35:12 +02:00
|
|
|
pt_br: require('./pt_BR.js'),
|
|
|
|
pt_pt: require('./pt_PT.js'),
|
2018-05-31 20:38:20 +02:00
|
|
|
es: require('./es.js'),
|
2018-06-02 14:18:50 +02:00
|
|
|
ua: require('./ua.js'),
|
2019-01-10 06:11:39 +01:00
|
|
|
jp_jp: require('./jp_JP.js'),
|
2018-12-31 20:36:45 +01:00
|
|
|
de_de: require('./de_DE.js'),
|
2019-01-06 17:46:31 +01:00
|
|
|
da_dk: require('./da_DK.js'),
|
2019-01-02 13:07:27 +01:00
|
|
|
cs_cz: require('./cs_CZ.js'),
|
2019-01-05 21:24:07 +01:00
|
|
|
th_th: require('./th_TH.js'),
|
2019-01-05 13:23:59 +01:00
|
|
|
nl_nl: require('./nl_NL.js'),
|
2019-01-06 17:10:25 +01:00
|
|
|
fr_fr: require('./fr_FR.js'),
|
2019-01-08 03:05:40 +01:00
|
|
|
hr_hr: require('./hr_HR.js'),
|
2018-05-28 21:18:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
strings.saveLanguage = lang => AsyncStorage.setItem(AppStorage.LANG, lang);
|
|
|
|
|
2019-01-11 04:28:03 +01:00
|
|
|
strings.transactionTimeToReadable = time => {
|
2018-06-25 00:22:46 +02:00
|
|
|
if (time === 0) {
|
2018-06-30 14:52:18 +02:00
|
|
|
return strings._.never;
|
2018-06-25 00:22:46 +02:00
|
|
|
}
|
2019-01-11 04:28:03 +01:00
|
|
|
return dayjs(time).fromNow();
|
2018-06-25 00:22:46 +02:00
|
|
|
};
|
|
|
|
|
2018-12-23 14:05:33 +01:00
|
|
|
function removeTrailingZeros(value) {
|
|
|
|
value = value.toString();
|
|
|
|
|
|
|
|
if (value.indexOf('.') === -1) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
while ((value.slice(-1) === '0' || value.slice(-1) === '.') && value.indexOf('.') !== -1) {
|
|
|
|
value = value.substr(0, value.length - 1);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-12-15 15:38:05 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param balance {Number} Float amount of bitcoins
|
2018-12-24 19:12:09 +01:00
|
|
|
* @param toUnit {String} Value from models/bitcoinUnits.js
|
2018-12-15 15:38:05 +01:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2019-01-06 20:34:01 +01:00
|
|
|
strings.formatBalance = (balance, toUnit, withFormatting = false) => {
|
2018-12-22 04:43:49 +01:00
|
|
|
if (toUnit === undefined) {
|
2018-10-20 23:10:21 +02:00
|
|
|
return balance + ' ' + BitcoinUnit.BTC;
|
2018-12-22 04:23:52 +01:00
|
|
|
}
|
2018-12-22 06:12:41 +01:00
|
|
|
if (toUnit === BitcoinUnit.BTC) {
|
2018-12-24 01:44:31 +01:00
|
|
|
return balance + ' ' + BitcoinUnit.BTC;
|
2018-12-22 06:12:41 +01:00
|
|
|
} else if (toUnit === BitcoinUnit.SATS) {
|
2018-12-25 00:22:53 +01:00
|
|
|
const value = new BigNumber(balance).multipliedBy(100000000);
|
2019-01-12 04:54:53 +01:00
|
|
|
return (balance < 0 ? '-' : '') + (withFormatting ? new Intl.NumberFormat().format(value.toString()).replace(/[^0-9]/g, ' ') : value) + ' ' + BitcoinUnit.SATS;
|
2018-12-22 06:12:41 +01:00
|
|
|
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
|
2018-12-23 06:43:50 +01:00
|
|
|
return currency.BTCToLocalCurrency(balance);
|
2018-10-20 23:10:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-24 19:12:09 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param balance {Integer} Satoshis
|
|
|
|
* @param toUnit {String} Value from models/bitcoinUnits.js
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2019-01-06 20:34:01 +01:00
|
|
|
strings.formatBalanceWithoutSuffix = (balance, toUnit, withFormatting = false) => {
|
2018-12-22 04:23:52 +01:00
|
|
|
if (toUnit === undefined) {
|
|
|
|
return balance;
|
|
|
|
}
|
2018-10-20 23:10:21 +02:00
|
|
|
if (balance !== 0) {
|
2018-12-24 19:12:27 +01:00
|
|
|
if (toUnit === BitcoinUnit.BTC) {
|
2018-12-23 14:05:33 +01:00
|
|
|
const value = new BigNumber(balance).dividedBy(100000000).toFixed(8);
|
|
|
|
return removeTrailingZeros(value);
|
2018-12-22 04:23:52 +01:00
|
|
|
} else if (toUnit === BitcoinUnit.SATS) {
|
2019-01-11 15:29:18 +01:00
|
|
|
return (balance < 0 ? '-' : '') + (withFormatting ? new Intl.NumberFormat().format(balance).replace(/[^0-9]/g, ' ') : balance);
|
2018-12-22 04:23:52 +01:00
|
|
|
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
|
2018-12-23 06:21:43 +01:00
|
|
|
return currency.satoshiToLocalCurrency(balance);
|
2018-10-20 23:10:21 +02:00
|
|
|
}
|
2018-07-06 17:42:37 +02:00
|
|
|
}
|
2019-01-04 05:28:22 +01:00
|
|
|
return balance.toString();
|
2018-07-06 17:42:37 +02:00
|
|
|
};
|
|
|
|
|
2018-05-28 21:18:11 +02:00
|
|
|
module.exports = strings;
|