2018-09-14 00:08:17 -04:00
|
|
|
import Localization from 'react-localization';
|
2018-05-28 20:18:11 +01:00
|
|
|
import { AsyncStorage } from 'react-native';
|
|
|
|
import { AppStorage } from '../class';
|
2018-10-20 22:10:21 +01:00
|
|
|
import { BitcoinUnit } from '../models/bitcoinUnits';
|
2018-12-15 14:38:05 +00:00
|
|
|
let currency = require('../currency');
|
2018-07-06 17:54:00 +01:00
|
|
|
let BigNumber = require('bignumber.js');
|
2018-05-28 20:18:11 +01:00
|
|
|
let strings;
|
|
|
|
|
|
|
|
// first-time loading sequence
|
|
|
|
(async () => {
|
|
|
|
// finding out whether lang preference was saved
|
|
|
|
let lang = await AsyncStorage.getItem(AppStorage.LANG);
|
|
|
|
if (lang) {
|
|
|
|
strings.setLanguage(lang);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-14 00:08:17 -04:00
|
|
|
if (Localization.getCurrentLocaleAsync) {
|
|
|
|
let locale = await Localization.getCurrentLocaleAsync();
|
2018-05-28 20:18:11 +01:00
|
|
|
if (locale) {
|
|
|
|
locale = locale.split('-');
|
|
|
|
locale = locale[0];
|
|
|
|
console.log('current locale:', locale);
|
2018-10-08 00:35:12 +02:00
|
|
|
if (locale === 'en' || locale === 'ru' || locale === 'ua' || locale === 'es' || locale === 'pt-br' || locale === 'pt-pt') {
|
|
|
|
locale = locale.replace('-', '_');
|
2018-05-31 19:38:20 +01:00
|
|
|
strings.setLanguage(locale);
|
2018-05-31 19:43:56 +01:00
|
|
|
} else {
|
|
|
|
strings.setLanguage('en');
|
2018-05-28 20:18:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2018-09-14 00:08:17 -04:00
|
|
|
strings = new Localization({
|
2018-05-28 20:18:11 +01: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 19:38:20 +01:00
|
|
|
es: require('./es.js'),
|
2018-06-02 13:18:50 +01:00
|
|
|
ua: require('./ua.js'),
|
2018-05-28 20:18:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
strings.saveLanguage = lang => AsyncStorage.setItem(AppStorage.LANG, lang);
|
|
|
|
|
2018-06-24 23:22:46 +01:00
|
|
|
strings.transactionTimeToReadable = function(time) {
|
|
|
|
if (time === 0) {
|
2018-06-30 13:52:18 +01:00
|
|
|
return strings._.never;
|
2018-06-24 23:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let ago = (Date.now() - Date.parse(time)) / 1000; // seconds
|
|
|
|
if (ago / (3600 * 24) >= 30) {
|
|
|
|
ago = Math.round(ago / (3600 * 24 * 30));
|
2018-06-30 13:52:18 +01:00
|
|
|
return ago + ' ' + strings._.months_ago;
|
2018-06-24 23:22:46 +01:00
|
|
|
} else if (ago / (3600 * 24) >= 1) {
|
|
|
|
ago = Math.round(ago / (3600 * 24));
|
2018-06-30 13:52:18 +01:00
|
|
|
return ago + ' ' + strings._.days_ago;
|
2018-09-27 20:19:14 +01:00
|
|
|
} else if (ago > 3600) {
|
2018-06-24 23:22:46 +01:00
|
|
|
ago = Math.round(ago / 3600);
|
2018-06-30 13:52:18 +01:00
|
|
|
return ago + ' ' + strings._.hours_ago;
|
2018-09-27 20:19:14 +01:00
|
|
|
} else {
|
|
|
|
ago = Math.round(ago / 60);
|
|
|
|
return ago + ' ' + strings._.minutes_ago;
|
2018-06-24 23:22:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-15 14:38:05 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param balance {Number} Float amount of bitcoins
|
|
|
|
* @param unit {String} Value from models/bitcoinUnits.js
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-10-20 22:10:21 +01:00
|
|
|
strings.formatBalance = (balance, unit) => {
|
|
|
|
const conversion = 100000000;
|
|
|
|
if (unit === undefined) {
|
|
|
|
return balance + ' ' + BitcoinUnit.BTC;
|
|
|
|
} else {
|
|
|
|
if (balance !== 0) {
|
|
|
|
let b = new BigNumber(balance);
|
|
|
|
if (unit === BitcoinUnit.MBTC) {
|
|
|
|
return b.multipliedBy(1000).toString() + ' ' + BitcoinUnit.MBTC;
|
|
|
|
} else if (unit === BitcoinUnit.BITS) {
|
|
|
|
return b.multipliedBy(1000000).toString() + ' ' + BitcoinUnit.BITS;
|
|
|
|
} else if (unit === BitcoinUnit.SATOSHIS) {
|
|
|
|
return (b.times(conversion).toString() + ' ' + BitcoinUnit.SATOSHIS).replace(/\./g, '');
|
2018-10-27 11:13:09 -04:00
|
|
|
} else if (unit === BitcoinUnit.SATS) {
|
|
|
|
return (b.times(conversion).toString() + ' ' + BitcoinUnit.SATS).replace(/\./g, '');
|
2018-12-15 14:38:05 +00:00
|
|
|
} else if (unit === BitcoinUnit.LOCAL_CURRENCY) {
|
|
|
|
return currency.satoshiToLocalCurrency(b.times(conversion).toNumber());
|
2018-10-20 22:10:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return balance + ' ' + BitcoinUnit.BTC;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
strings.formatBalanceWithoutSuffix = (balance, unit) => {
|
|
|
|
const conversion = 100000000;
|
|
|
|
if (balance !== 0) {
|
2018-07-06 17:54:00 +01:00
|
|
|
let b = new BigNumber(balance);
|
2018-10-20 22:10:21 +01:00
|
|
|
if (unit === BitcoinUnit.BTC) {
|
|
|
|
return Number(b.div(conversion));
|
|
|
|
} else if (unit === BitcoinUnit.MBTC) {
|
|
|
|
return b.multipliedBy(1000).toString();
|
|
|
|
} else if (unit === BitcoinUnit.BITS) {
|
|
|
|
return b.multipliedBy(1000000).toString();
|
|
|
|
} else if (unit === BitcoinUnit.SATOSHIS) {
|
|
|
|
return b
|
|
|
|
.times(conversion)
|
|
|
|
.toString()
|
|
|
|
.replace(/\./g, '');
|
|
|
|
}
|
2018-07-06 16:42:37 +01:00
|
|
|
}
|
2018-10-20 22:10:21 +01:00
|
|
|
return balance;
|
2018-07-06 16:42:37 +01:00
|
|
|
};
|
|
|
|
|
2018-05-28 20:18:11 +01:00
|
|
|
module.exports = strings;
|