2018-09-14 06:08:17 +02:00
|
|
|
import Localization from 'react-localization';
|
2019-05-02 22:33:03 +02:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
2018-05-28 21:18:11 +02:00
|
|
|
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
|
2019-05-02 22:33:03 +02:00
|
|
|
// For some reason using the AppStorage.LANG constant is not working. Hard coding string for now.
|
|
|
|
let lang = await AsyncStorage.getItem('lang');
|
2018-05-28 21:18:11 +02:00
|
|
|
if (lang) {
|
|
|
|
strings.setLanguage(lang);
|
2019-01-11 17:23:09 +01:00
|
|
|
let localeForDayJSAvailable = true;
|
|
|
|
switch (lang) {
|
2019-03-09 01:23:53 +01:00
|
|
|
case 'el':
|
|
|
|
require('dayjs/locale/el');
|
|
|
|
break;
|
2019-03-04 15:53:52 +01:00
|
|
|
case 'it':
|
|
|
|
require('dayjs/locale/it');
|
|
|
|
break;
|
2019-03-05 19:47:52 +01:00
|
|
|
case 'zh_cn':
|
2019-04-23 20:49:00 +02:00
|
|
|
lang = 'zh-cn';
|
2019-02-19 22:07:47 +01:00
|
|
|
require('dayjs/locale/zh-cn');
|
|
|
|
break;
|
2019-09-20 13:20:28 +02:00
|
|
|
case 'zh_tw':
|
|
|
|
lang = 'zh-tw';
|
|
|
|
require('dayjs/locale/zh-tw');
|
|
|
|
break;
|
2019-01-11 17:23:09 +01:00
|
|
|
case 'ru':
|
|
|
|
require('dayjs/locale/ru');
|
|
|
|
break;
|
|
|
|
case 'es':
|
|
|
|
require('dayjs/locale/es');
|
|
|
|
break;
|
2019-03-15 00:05:44 +01:00
|
|
|
case 'fi_fi':
|
|
|
|
require('dayjs/locale/fi');
|
|
|
|
break;
|
2019-01-11 17:23:09 +01:00
|
|
|
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;
|
2019-06-19 22:45:33 +02:00
|
|
|
case 'hu_hu':
|
|
|
|
require('dayjs/locale/hu');
|
|
|
|
break;
|
2019-02-12 23:14:07 +01:00
|
|
|
case 'id_id':
|
|
|
|
require('dayjs/locale/id');
|
|
|
|
break;
|
2019-03-19 09:44:42 +01:00
|
|
|
case 'sv_se':
|
|
|
|
require('dayjs/locale/sv');
|
|
|
|
break;
|
2019-03-09 20:08:47 +01:00
|
|
|
case 'nb_no':
|
|
|
|
require('dayjs/locale/nb');
|
|
|
|
break;
|
2019-03-15 20:58:04 +01:00
|
|
|
case 'tr_tr':
|
|
|
|
require('dayjs/locale/tr');
|
2019-03-16 13:53:52 +01:00
|
|
|
break;
|
2019-09-20 17:11:11 +02:00
|
|
|
case 'vi_vn':
|
|
|
|
lang = 'vi-vn';
|
|
|
|
require('dayjs/locale/vn');
|
|
|
|
break;
|
2019-01-11 17:23:09 +01:00
|
|
|
default:
|
|
|
|
localeForDayJSAvailable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (localeForDayJSAvailable) {
|
|
|
|
dayjs.locale(lang.split('_')[0]);
|
|
|
|
}
|
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'),
|
2019-03-04 15:53:52 +01:00
|
|
|
it: require('./it.js'),
|
2019-03-09 01:23:53 +01:00
|
|
|
el: require('./el.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-03-15 00:05:44 +01:00
|
|
|
fi_fi: require('./fi_FI.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'),
|
2019-06-19 22:45:33 +02:00
|
|
|
hu_hu: require('./hu_HU.js'),
|
2019-02-12 23:14:07 +01:00
|
|
|
id_id: require('./id_ID.js'),
|
2019-02-19 22:07:47 +01:00
|
|
|
zh_cn: require('./zh_cn.js'),
|
2019-09-20 13:20:28 +02:00
|
|
|
zh_tw: require('./zh_tw.js'),
|
2019-03-19 09:44:42 +01:00
|
|
|
sv_se: require('./sv_SE.js'),
|
2019-03-09 20:08:47 +01:00
|
|
|
nb_no: require('./nb_NO.js'),
|
2019-03-15 20:58:04 +01:00
|
|
|
tr_tr: require('./tr_TR.js'),
|
2019-09-20 17:11:11 +02:00
|
|
|
vi_vn: require('./vi_VN.js'),
|
2019-09-20 13:53:06 +02:00
|
|
|
zar_xho: require('./ZAR_Xho.js'),
|
|
|
|
zar_afr: require('./ZAR_Afr.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-04-23 20:49:00 +02:00
|
|
|
let ret;
|
|
|
|
try {
|
|
|
|
ret = dayjs(time).fromNow();
|
|
|
|
} catch (_) {
|
|
|
|
console.warn('incorrect locale set for dayjs');
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
return ret;
|
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) {
|
2019-05-02 22:33:03 +02:00
|
|
|
const value = new BigNumber(balance).dividedBy(100000000).toFixed(8);
|
|
|
|
return removeTrailingZeros(value) + ' ' + BitcoinUnit.BTC;
|
2018-12-22 06:12:41 +01:00
|
|
|
} else if (toUnit === BitcoinUnit.SATS) {
|
2019-01-12 05:01:15 +01:00
|
|
|
return (
|
|
|
|
(balance < 0 ? '-' : '') +
|
2019-05-02 22:33:03 +02:00
|
|
|
(withFormatting ? new Intl.NumberFormat().format(balance.toString()).replace(/[^0-9]/g, ' ') : balance) +
|
2019-01-12 05:01:15 +01:00
|
|
|
' ' +
|
|
|
|
BitcoinUnit.SATS
|
|
|
|
);
|
2018-12-22 06:12:41 +01:00
|
|
|
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
|
2019-05-02 22:33:03 +02:00
|
|
|
return currency.satoshiToLocalCurrency(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;
|