BlueWallet/loc/index.js

123 lines
3.5 KiB
JavaScript
Raw Normal View History

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';
import { BitcoinUnit } from '../models/bitcoinUnits';
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;
// 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 06:08:17 +02:00
if (Localization.getCurrentLocaleAsync) {
let locale = await Localization.getCurrentLocaleAsync();
2018-05-28 21:18:11 +02:00
if (locale) {
locale = locale.split('-');
locale = locale[0];
console.log('current locale:', locale);
if (locale === 'en' || locale === 'ru' || locale === 'ua' || locale === 'es' || locale === 'pt-br' || locale === 'pt-pt' || locale === 'de-de') {
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'),
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'),
de_de: require('.de_DE.js')
2018-05-28 21:18:11 +02:00
});
strings.saveLanguage = lang => AsyncStorage.setItem(AppStorage.LANG, lang);
2018-06-25 00:22:46 +02:00
strings.transactionTimeToReadable = function(time) {
if (time === 0) {
2018-06-30 14:52:18 +02:00
return strings._.never;
2018-06-25 00:22:46 +02: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 14:52:18 +02:00
return ago + ' ' + strings._.months_ago;
2018-06-25 00:22:46 +02:00
} else if (ago / (3600 * 24) >= 1) {
ago = Math.round(ago / (3600 * 24));
2018-06-30 14:52:18 +02:00
return ago + ' ' + strings._.days_ago;
} else if (ago > 3600) {
2018-06-25 00:22:46 +02:00
ago = Math.round(ago / 3600);
2018-06-30 14:52:18 +02:00
return ago + ' ' + strings._.hours_ago;
} else {
ago = Math.round(ago / 60);
return ago + ' ' + strings._.minutes_ago;
2018-06-25 00:22:46 +02: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;
}
/**
*
* @param balance {Number} Float amount of bitcoins
2018-12-24 19:12:09 +01:00
* @param toUnit {String} Value from models/bitcoinUnits.js
* @returns {string}
*/
2018-12-23 06:21:43 +01:00
strings.formatBalance = (balance, toUnit) => {
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) {
2018-12-24 01:44:31 +01:00
return balance + ' ' + BitcoinUnit.BTC;
} else if (toUnit === BitcoinUnit.SATS) {
2018-12-25 00:22:53 +01:00
const value = new BigNumber(balance).multipliedBy(100000000);
return value.toString() + ' ' + BitcoinUnit.SATS;
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
2018-12-23 06:43:50 +01:00
return currency.BTCToLocalCurrency(balance);
}
};
2018-12-24 19:12:09 +01:00
/**
*
* @param balance {Integer} Satoshis
* @param toUnit {String} Value from models/bitcoinUnits.js
* @returns {string}
*/
2018-12-23 06:21:43 +01:00
strings.formatBalanceWithoutSuffix = (balance, toUnit) => {
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);
return removeTrailingZeros(value);
2018-12-22 04:23:52 +01:00
} else if (toUnit === BitcoinUnit.SATS) {
2018-12-24 19:12:27 +01:00
return 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
}
return balance;
2018-07-06 17:42:37 +02:00
};
2018-05-28 21:18:11 +02:00
module.exports = strings;