You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.1 KiB

import Localization from 'react-localization';
import { AsyncStorage } from 'react-native';
import { AppStorage } from '../class';
import { BitcoinUnit } from '../models/bitcoinUnits';
const currency = require('../currency');
const BigNumber = require('bignumber.js');
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;
}
if (Localization.getCurrentLocaleAsync) {
let locale = await Localization.getCurrentLocaleAsync();
if (locale) {
locale = locale.split('-');
locale = locale[0];
console.log('current locale:', locale);
6 years ago
if (
locale === 'en' ||
locale === 'ru' ||
locale === 'ua' ||
locale === 'es' ||
locale === 'fr-fr' ||
6 years ago
locale === 'pt-br' ||
locale === 'pt-pt' ||
locale === 'de-de' ||
locale === 'cs-cz' ||
6 years ago
locale === 'th-th' ||
locale === 'da-dk' ||
locale === 'nl-nl' ||
locale === 'hr-hr'
6 years ago
) {
locale = locale.replace('-', '_');
strings.setLanguage(locale);
6 years ago
} else {
strings.setLanguage('en');
}
}
}
})();
strings = new Localization({
en: require('./en.js'),
ru: require('./ru.js'),
pt_br: require('./pt_BR.js'),
pt_pt: require('./pt_PT.js'),
es: require('./es.js'),
6 years ago
ua: require('./ua.js'),
6 years ago
de_de: require('./de_DE.js'),
da_dk: require('./da_DK.js'),
cs_cz: require('./cs_CZ.js'),
6 years ago
th_th: require('./th_TH.js'),
nl_nl: require('./nl_NL.js'),
fr_fr: require('./fr_FR.js'),
hr_hr: require('./hr_HR.js'),
});
strings.saveLanguage = lang => AsyncStorage.setItem(AppStorage.LANG, lang);
6 years ago
strings.transactionTimeToReadable = function(time) {
if (time === 0) {
return strings._.never;
6 years ago
}
let ago = (Date.now() - Date.parse(time)) / 1000; // seconds
if (ago / (3600 * 24) >= 30) {
ago = Math.round(ago / (3600 * 24 * 30));
return ago + ' ' + strings._.months_ago;
6 years ago
} else if (ago / (3600 * 24) >= 1) {
ago = Math.round(ago / (3600 * 24));
return ago + ' ' + strings._.days_ago;
} else if (ago > 3600) {
6 years ago
ago = Math.round(ago / 3600);
return ago + ' ' + strings._.hours_ago;
} else {
ago = Math.round(ago / 60);
return ago + ' ' + strings._.minutes_ago;
6 years ago
}
};
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
6 years ago
* @param toUnit {String} Value from models/bitcoinUnits.js
* @returns {string}
*/
strings.formatBalance = (balance, toUnit, withFormatting = false) => {
if (toUnit === undefined) {
return balance + ' ' + BitcoinUnit.BTC;
}
if (toUnit === BitcoinUnit.BTC) {
return balance + ' ' + BitcoinUnit.BTC;
} else if (toUnit === BitcoinUnit.SATS) {
const value = new BigNumber(balance).multipliedBy(100000000);
return (withFormatting ? new Intl.NumberFormat().format(value.toString()).replace(/[^0-9]/g, ' ') : value) + ' ' + BitcoinUnit.SATS;
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
return currency.BTCToLocalCurrency(balance);
}
};
6 years ago
/**
*
* @param balance {Integer} Satoshis
* @param toUnit {String} Value from models/bitcoinUnits.js
* @returns {string}
*/
strings.formatBalanceWithoutSuffix = (balance, toUnit, withFormatting = false) => {
if (toUnit === undefined) {
return balance;
}
if (balance !== 0) {
if (toUnit === BitcoinUnit.BTC) {
const value = new BigNumber(balance).dividedBy(100000000).toFixed(8);
return removeTrailingZeros(value);
} else if (toUnit === BitcoinUnit.SATS) {
return withFormatting ? new Intl.NumberFormat().format(balance).replace(/[^0-9]/g, ' ') : balance;
} else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) {
return currency.satoshiToLocalCurrency(balance);
}
}
6 years ago
return balance.toString();
};
module.exports = strings;