BlueWallet/loc/index.js

275 lines
8.2 KiB
JavaScript
Raw Normal View History

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';
2020-07-20 15:38:46 +02:00
import dayjs from 'dayjs';
2019-01-11 04:28:03 +01:00
import relativeTime from 'dayjs/plugin/relativeTime';
import * as RNLocalize from 'react-native-localize';
2020-07-20 15:38:46 +02:00
import BigNumber from 'bignumber.js';
import { AppStorage } from '../class';
import { BitcoinUnit } from '../models/bitcoinUnits';
import { AvailableLanguages } from './languages';
2020-06-09 16:08:18 +02:00
const currency = require('../blue_modules/currency');
2020-07-20 15:38:46 +02:00
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);
let localeForDayJSAvailable = true;
switch (lang) {
2020-09-03 13:54:50 +02:00
case 'ar':
require('dayjs/locale/ar');
break;
2020-08-28 10:05:41 +02:00
case 'ca':
require('dayjs/locale/ca');
2019-03-04 15:53:52 +01:00
break;
2020-08-28 10:05:41 +02:00
case 'da_dk':
require('dayjs/locale/da');
2019-02-19 22:07:47 +01:00
break;
2020-08-28 10:05:41 +02:00
case 'de_de':
require('dayjs/locale/de');
break;
2020-08-28 10:05:41 +02:00
case 'el':
require('dayjs/locale/el');
break;
case 'es':
require('dayjs/locale/es');
break;
case 'es_419':
// es-do it is the closesd one to es_419
lang = 'es-do';
require('dayjs/locale/es-do');
break;
2019-03-15 00:05:44 +01:00
case 'fi_fi':
require('dayjs/locale/fi');
break;
case 'fr_fr':
require('dayjs/locale/fr');
break;
2020-08-28 10:05:41 +02:00
case 'he':
require('dayjs/locale/he');
break;
2020-08-28 10:05:41 +02:00
case 'hr_hr':
require('dayjs/locale/hr');
break;
case 'hu_hu':
require('dayjs/locale/hu');
break;
case 'id_id':
require('dayjs/locale/id');
break;
case 'it':
require('dayjs/locale/it');
break;
case 'jp_jp':
lang = 'ja';
require('dayjs/locale/ja');
break;
2020-08-28 10:05:41 +02:00
case 'nb_no':
require('dayjs/locale/nb');
break;
case 'nl_nl':
require('dayjs/locale/nl');
break;
2020-08-28 10:05:41 +02:00
case 'pt_br':
lang = 'pt-br';
require('dayjs/locale/pt-br');
break;
2020-08-28 10:05:41 +02:00
case 'pt_pt':
lang = 'pt';
require('dayjs/locale/pt');
2019-06-19 22:45:33 +02:00
break;
2020-08-28 10:05:41 +02:00
case 'ru':
require('dayjs/locale/ru');
2019-02-12 23:14:07 +01:00
break;
2020-09-07 11:07:18 +02:00
case 'sk_sk':
require('dayjs/locale/sk');
break;
case 'sl_si':
require('dayjs/locale/sl');
break;
2019-03-19 09:44:42 +01:00
case 'sv_se':
require('dayjs/locale/sv');
break;
2020-08-28 10:05:41 +02:00
case 'th_th':
require('dayjs/locale/th');
2019-03-09 20:08:47 +01:00
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':
2019-09-22 21:34:20 +02:00
require('dayjs/locale/vi');
2019-09-20 17:11:11 +02:00
break;
2020-08-28 10:05:41 +02:00
case 'zh_cn':
lang = 'zh-cn';
require('dayjs/locale/zh-cn');
break;
case 'zh_tw':
lang = 'zh-tw';
require('dayjs/locale/zh-tw');
break;
default:
localeForDayJSAvailable = false;
break;
}
if (localeForDayJSAvailable) {
dayjs.locale(lang.split('_')[0]);
}
} else {
const locales = RNLocalize.getLocales();
if (Object.keys(AvailableLanguages).some(language => language === locales[0])) {
strings.saveLanguage(locales[0].languageCode);
strings.setLanguage(locales[0].languageCode);
} else {
strings.saveLanguage('en');
strings.setLanguage('en');
}
2018-05-28 21:18:11 +02:00
}
})();
const strings = new Localization({
2020-09-03 13:54:50 +02:00
en: require('./en.json'),
2020-09-09 15:33:59 +02:00
ar: require('./ar.json'),
2020-07-20 15:38:46 +02:00
ca: require('./ca.json'),
cs_cz: require('./cs_cz.json'),
2020-08-28 10:05:41 +02:00
da_dk: require('./da_dk.json'),
de_de: require('./de_de.json'),
el: require('./el.json'),
es: require('./es.json'),
es_419: require('./es_419.json'),
2020-07-20 15:38:46 +02:00
fi_fi: require('./fi_fi.json'),
fr_fr: require('./fr_fr.json'),
2020-08-28 10:05:41 +02:00
he: require('./he.json'),
2020-07-20 15:38:46 +02:00
hr_hr: require('./hr_hr.json'),
hu_hu: require('./hu_hu.json'),
id_id: require('./id_id.json'),
2020-08-28 10:05:41 +02:00
it: require('./it.json'),
jp_jp: require('./jp_jp.json'),
2020-07-20 15:38:46 +02:00
nb_no: require('./nb_no.json'),
2020-08-28 10:05:41 +02:00
nl_nl: require('./nl_nl.json'),
pt_br: require('./pt_br.json'),
pt_pt: require('./pt_pt.json'),
ru: require('./ru.json'),
sk_sk: require('./sk_sk.json'),
2020-09-07 11:07:18 +02:00
sl_si: require('./sl_SI.json'),
2020-08-28 10:05:41 +02:00
sv_se: require('./sv_se.json'),
th_th: require('./th_th.json'),
2020-07-20 15:38:46 +02:00
tr_tr: require('./tr_tr.json'),
2020-08-28 10:05:41 +02:00
ua: require('./ua.json'),
2020-07-20 15:38:46 +02:00
vi_vn: require('./vi_vn.json'),
zar_afr: require('./zar_afr.json'),
2020-08-28 10:05:41 +02:00
zar_xho: require('./zar_xho.json'),
zh_cn: require('./zh_cn.json'),
zh_tw: require('./zh_tw.json'),
2018-05-28 21:18:11 +02:00
});
strings.saveLanguage = lang => AsyncStorage.setItem(AppStorage.LANG, lang);
2020-07-20 15:38:46 +02:00
export const 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
};
2020-07-20 15:38:46 +02:00
export const 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;
2020-06-09 16:08:18 +02:00
};
/**
*
2020-06-09 16:08:18 +02:00
* @param balance {number} Satoshis
2018-12-24 19:12:09 +01:00
* @param toUnit {String} Value from models/bitcoinUnits.js
2020-06-09 16:08:18 +02:00
* @param withFormatting {boolean} Works only with `BitcoinUnit.SATS`, makes spaces wetween groups of 000
* @returns {string}
*/
2020-07-20 15:38:46 +02:00
export function formatBalance(balance, toUnit, withFormatting = false) {
2018-12-22 04:43:49 +01:00
if (toUnit === undefined) {
return balance + ' ' + BitcoinUnit.BTC;
2018-12-22 04:23:52 +01:00
}
if (toUnit === BitcoinUnit.BTC) {
2019-05-02 22:33:03 +02:00
const value = new BigNumber(balance).dividedBy(100000000).toFixed(8);
2020-07-20 15:38:46 +02:00
return removeTrailingZeros(value) + ' ' + BitcoinUnit.BTC;
} else if (toUnit === BitcoinUnit.SATS) {
return (
(balance < 0 ? '-' : '') +
2019-05-02 22:33:03 +02:00
(withFormatting ? new Intl.NumberFormat().format(balance.toString()).replace(/[^0-9]/g, ' ') : balance) +
' ' +
BitcoinUnit.SATS
);
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
2019-05-02 22:33:03 +02:00
return currency.satoshiToLocalCurrency(balance);
}
2020-06-09 16:08:18 +02:00
}
2018-12-24 19:12:09 +01:00
/**
*
* @param balance {Integer} Satoshis
2020-06-09 16:08:18 +02:00
* @param toUnit {String} Value from models/bitcoinUnits.js, for example `BitcoinUnit.SATS`
* @param withFormatting {boolean} Works only with `BitcoinUnit.SATS`, makes spaces wetween groups of 000
2018-12-24 19:12:09 +01:00
* @returns {string}
*/
2020-07-20 15:38:46 +02:00
export function formatBalanceWithoutSuffix(balance = 0, toUnit, withFormatting = false) {
2018-12-22 04:23:52 +01:00
if (toUnit === undefined) {
return balance;
}
if (balance !== 0) {
2018-12-24 19:12:27 +01:00
if (toUnit === BitcoinUnit.BTC) {
const value = new BigNumber(balance).dividedBy(100000000).toFixed(8);
2020-07-20 15:38:46 +02:00
return removeTrailingZeros(value);
2018-12-22 04:23:52 +01:00
} else if (toUnit === BitcoinUnit.SATS) {
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-07-06 17:42:37 +02:00
}
2019-01-04 05:28:22 +01:00
return balance.toString();
2020-06-09 16:08:18 +02:00
}
/**
* Should be used when we need a simple string to be put in text input, for example
*
* @param balance {integer} Satoshis
* @param toUnit {String} Value from models/bitcoinUnits.js, for example `BitcoinUnit.SATS`
* @param withFormatting {boolean} Works only with `BitcoinUnit.SATS`, makes spaces wetween groups of 000
* @returns {string}
*/
2020-07-20 15:38:46 +02:00
export function formatBalancePlain(balance = 0, toUnit, withFormatting = false) {
2020-06-09 16:08:18 +02:00
const newInputValue = formatBalanceWithoutSuffix(balance, toUnit, withFormatting);
return _leaveNumbersAndDots(newInputValue);
}
2020-07-20 15:38:46 +02:00
export function _leaveNumbersAndDots(newInputValue) {
2020-06-09 16:08:18 +02:00
newInputValue = newInputValue.replace(/[^\d.,-]/g, ''); // filtering, leaving only numbers, dots & commas
if (newInputValue.endsWith('.00') || newInputValue.endsWith(',00')) newInputValue = newInputValue.substring(0, newInputValue.length - 3);
if (newInputValue[newInputValue.length - 3] === ',') {
// this is a fractional value, lets replace comma to dot so it represents actual fractional value for normal people
newInputValue = newInputValue.substring(0, newInputValue.length - 3) + '.' + newInputValue.substring(newInputValue.length - 2);
}
newInputValue = newInputValue.replace(/,/gi, '');
return newInputValue;
}
2018-07-06 17:42:37 +02:00
2020-07-20 15:38:46 +02:00
export default strings;