BlueWallet/loc/index.ts

386 lines
11 KiB
TypeScript
Raw Normal View History

2020-12-16 04:15:57 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2024-05-20 11:54:13 +02:00
import BigNumber from 'bignumber.js';
2020-07-20 15:38:46 +02:00
import dayjs from 'dayjs';
import localizedFormat from 'dayjs/plugin/localizedFormat';
2024-05-20 11:54:13 +02:00
import relativeTime from 'dayjs/plugin/relativeTime';
2024-06-02 20:31:28 +02:00
import Localization, { LocalizedStrings } from 'react-localization';
2024-05-20 11:54:13 +02:00
import { I18nManager } from 'react-native';
import * as RNLocalize from 'react-native-localize';
2020-07-20 15:38:46 +02:00
2024-05-20 11:54:13 +02:00
import { satoshiToLocalCurrency } from '../blue_modules/currency';
2020-07-20 15:38:46 +02:00
import { BitcoinUnit } from '../models/bitcoinUnits';
import { AvailableLanguages } from './languages';
2024-06-02 20:31:28 +02:00
import enJson from './en.json';
2020-07-20 15:38:46 +02:00
2022-06-19 15:05:30 +02:00
export const STORAGE_KEY = 'lang';
2021-05-18 22:38:18 +02:00
2019-01-11 04:28:03 +01:00
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
2018-05-28 21:18:11 +02:00
2024-06-02 20:31:28 +02:00
interface ILocalization1 extends LocalizedStrings<typeof enJson> {}
// overriding formatString to only return string
interface ILocalization extends Omit<ILocalization1, 'formatString'> {
formatString: (...args: Parameters<ILocalization1['formatString']>) => string;
}
2020-12-13 11:23:54 +01:00
const setDateTimeLocale = async () => {
2022-06-19 15:05:30 +02:00
let lang = (await AsyncStorage.getItem(STORAGE_KEY)) ?? '';
2020-12-13 11:23:54 +01:00
let localeForDayJSAvailable = true;
switch (lang) {
case 'ar':
require('dayjs/locale/ar');
break;
2023-06-23 15:12:05 +02:00
case 'be':
require('dayjs/locale/be');
break;
2020-12-13 11:23:54 +01:00
case 'bg_bg':
lang = 'bg';
require('dayjs/locale/bg');
break;
2024-01-22 20:29:23 +01:00
case 'bqi':
lang = 'fa';
require('dayjs/locale/fa');
break;
2020-12-13 11:23:54 +01:00
case 'ca':
require('dayjs/locale/ca');
break;
case 'cy':
require('dayjs/locale/cy');
break;
case 'da_dk':
require('dayjs/locale/da');
break;
case 'de_de':
require('dayjs/locale/de');
break;
case 'el':
require('dayjs/locale/el');
break;
case 'es':
require('dayjs/locale/es');
break;
case 'es_419':
// es-do it is the closes one to es_419
lang = 'es-do';
require('dayjs/locale/es-do');
break;
2023-06-23 15:12:05 +02:00
case 'et':
require('dayjs/locale/et');
break;
2020-12-13 11:23:54 +01:00
case 'fi_fi':
require('dayjs/locale/fi');
break;
case 'fa':
require('dayjs/locale/fa');
break;
case 'fr_fr':
require('dayjs/locale/fr');
break;
case 'he':
require('dayjs/locale/he');
break;
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;
2021-07-23 21:53:10 +02:00
case 'ko_kr':
lang = 'ko';
require('dayjs/locale/ko');
break;
case 'lrc':
lang = 'fa';
require('dayjs/locale/fa');
break;
2023-06-23 15:12:05 +02:00
case 'kn':
require('dayjs/locale/kn');
break;
case 'ms':
require('dayjs/locale/ms');
break;
2022-03-11 08:52:04 +01:00
case 'ne':
require('dayjs/locale/ne');
break;
2020-12-13 11:23:54 +01:00
case 'nb_no':
require('dayjs/locale/nb');
break;
case 'nl_nl':
require('dayjs/locale/nl');
break;
case 'pt_br':
lang = 'pt-br';
require('dayjs/locale/pt-br');
break;
case 'pt_pt':
lang = 'pt';
require('dayjs/locale/pt');
break;
case 'pl':
require('dayjs/locale/pl');
break;
case 'ro':
require('dayjs/locale/ro');
break;
2020-12-13 11:23:54 +01:00
case 'ru':
require('dayjs/locale/ru');
break;
case 'si_lk':
require('dayjs/locale/si.js');
break;
2020-12-13 11:23:54 +01:00
case 'sk_sk':
require('dayjs/locale/sk');
break;
case 'sl_si':
require('dayjs/locale/sl');
break;
2023-06-23 15:12:05 +02:00
case 'sr_rs':
lang = 'sr-cyrl';
require('dayjs/locale/sr-cyrl');
break;
2020-12-13 11:23:54 +01:00
case 'sv_se':
require('dayjs/locale/sv');
break;
case 'th_th':
require('dayjs/locale/th');
break;
case 'tr_tr':
require('dayjs/locale/tr');
break;
case 'vi_vn':
require('dayjs/locale/vi');
break;
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 {
dayjs.locale('en');
2020-12-13 11:23:54 +01:00
}
};
2022-06-19 15:05:30 +02:00
const init = async () => {
2018-05-28 21:18:11 +02:00
// finding out whether lang preference was saved
2022-06-19 15:05:30 +02:00
const lang = await AsyncStorage.getItem(STORAGE_KEY);
2018-05-28 21:18:11 +02:00
if (lang) {
2022-06-19 15:05:30 +02:00
await saveLanguage(lang);
await loc.setLanguage(lang);
if (process.env.JEST_WORKER_ID === undefined) {
2022-06-19 15:05:30 +02:00
const foundLang = AvailableLanguages.find(language => language.value === lang);
I18nManager.allowRTL(foundLang?.isRTL ?? false);
I18nManager.forceRTL(foundLang?.isRTL ?? false);
}
2020-12-13 11:23:54 +01:00
await setDateTimeLocale();
} else {
const locales = RNLocalize.getLocales();
if (Object.values(AvailableLanguages).some(language => language.value === locales[0].languageCode)) {
2022-06-19 15:05:30 +02:00
await saveLanguage(locales[0].languageCode);
await loc.setLanguage(locales[0].languageCode);
if (process.env.JEST_WORKER_ID === undefined) {
2021-08-06 15:41:12 +02:00
I18nManager.allowRTL(locales[0].isRTL ?? false);
I18nManager.forceRTL(locales[0].isRTL ?? false);
}
} else {
2022-06-19 15:05:30 +02:00
await saveLanguage('en');
await loc.setLanguage('en');
2021-03-19 14:17:50 +01:00
if (process.env.JEST_WORKER_ID === undefined) {
2021-04-02 06:34:01 +02:00
I18nManager.allowRTL(false);
2021-03-19 04:09:46 +01:00
I18nManager.forceRTL(false);
}
}
await setDateTimeLocale();
2018-05-28 21:18:11 +02:00
}
2020-12-13 10:52:54 +01:00
};
2022-06-19 15:05:30 +02:00
init();
2018-05-28 21:18:11 +02:00
2024-06-02 20:31:28 +02:00
const loc: ILocalization = new Localization({
en: enJson,
2020-09-09 15:33:59 +02:00
ar: require('./ar.json'),
2023-06-23 15:12:05 +02:00
be: require('./be@tarask.json'),
bg_bg: require('./bg_bg.json'),
2024-01-22 20:29:23 +01:00
bqi: require('./bqi.json'),
2020-07-20 15:38:46 +02:00
ca: require('./ca.json'),
cy: require('./cy.json'),
2020-07-20 15:38:46 +02:00
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'),
2023-06-23 15:12:05 +02:00
et: require('./et_EE.json'),
fa: require('./fa.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'),
2021-07-23 21:53:10 +02:00
ko_kr: require('./ko_KR.json'),
lrc: require('./lrc.json'),
ms: require('./ms.json'),
2023-06-23 15:12:05 +02:00
kn: require('./kn.json'),
2022-03-11 08:52:04 +01:00
ne: require('./ne.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'),
pl: require('./pl.json'),
ro: require('./ro.json'),
2020-08-28 10:05:41 +02:00
ru: require('./ru.json'),
si_lk: require('./si_LK.json'),
2020-08-28 10:05:41 +02:00
sk_sk: require('./sk_sk.json'),
2020-09-07 11:07:18 +02:00
sl_si: require('./sl_SI.json'),
2023-06-23 15:12:05 +02:00
sr_rs: require('./sr_RS.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
});
2022-06-19 15:05:30 +02:00
export const saveLanguage = async (lang: string) => {
await AsyncStorage.setItem(STORAGE_KEY, lang);
loc.setLanguage(lang);
// even tho it makes no effect changing it in this run, it will on the next run, so we are doign it here:
if (process.env.JEST_WORKER_ID === undefined) {
const foundLang = AvailableLanguages.find(language => language.value === lang);
I18nManager.allowRTL(foundLang?.isRTL ?? false);
I18nManager.forceRTL(foundLang?.isRTL ?? false);
}
2020-12-13 18:05:22 +01:00
await setDateTimeLocale();
};
2018-05-28 21:18:11 +02:00
2024-07-20 15:10:03 +02:00
export const transactionTimeToReadable = (time: number | string) => {
2021-09-09 13:00:11 +02:00
if (time === -1) {
return 'unknown';
}
2018-06-25 00:22:46 +02:00
if (time === 0) {
2022-06-19 15:05:30 +02:00
return loc._.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');
2024-06-02 20:31:28 +02:00
return String(time);
2019-04-23 20:49:00 +02:00
}
return ret;
2018-06-25 00:22:46 +02:00
};
2024-06-02 20:31:28 +02:00
export const removeTrailingZeros = (value: number | string): string => {
2022-06-19 15:05:30 +02:00
let ret = value.toString();
2022-06-19 15:05:30 +02:00
if (ret.indexOf('.') === -1) {
return ret;
}
2022-06-19 15:05:30 +02:00
while ((ret.slice(-1) === '0' || ret.slice(-1) === '.') && ret.indexOf('.') !== -1) {
ret = ret.substr(0, ret.length - 1);
}
2022-06-19 15:05:30 +02:00
return ret;
2020-06-09 16:08:18 +02:00
};
/**
*
2020-06-09 16:08:18 +02:00
* @param balance {number} Satoshis
2022-06-19 15:05:30 +02: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}
*/
2024-06-02 20:31:28 +02:00
export function formatBalance(balance: number, toUnit: string, withFormatting = false): string {
2018-12-22 04:43:49 +01:00
if (toUnit === undefined) {
2022-06-19 15:05:30 +02:00
return balance + ' ' + loc.units[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);
2022-06-19 15:05:30 +02:00
return removeTrailingZeros(+value) + ' ' + loc.units[BitcoinUnit.BTC];
} else if (toUnit === BitcoinUnit.SATS) {
2022-06-19 15:05:30 +02:00
return (withFormatting ? new Intl.NumberFormat().format(balance).toString() : String(balance)) + ' ' + loc.units[BitcoinUnit.SATS];
2024-06-02 20:31:28 +02:00
} else {
2024-01-28 16:11:08 +01:00
return satoshiToLocalCurrency(balance);
}
2020-06-09 16:08:18 +02:00
}
2018-12-24 19:12:09 +01:00
/**
*
2022-06-19 15:05:30 +02:00
* @param balance {number} Satoshis
* @param toUnit {string} Value from models/bitcoinUnits.js, for example `BitcoinUnit.SATS`
2020-06-09 16:08:18 +02:00
* @param withFormatting {boolean} Works only with `BitcoinUnit.SATS`, makes spaces wetween groups of 000
2018-12-24 19:12:09 +01:00
* @returns {string}
*/
2024-06-02 20:31:28 +02:00
export function formatBalanceWithoutSuffix(balance = 0, toUnit: string, withFormatting = false): string | number {
2018-12-22 04:23:52 +01:00
if (toUnit === undefined) {
return balance;
}
2024-08-22 17:50:12 +02:00
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).toString() : String(balance);
} else {
return satoshiToLocalCurrency(balance);
2018-07-06 17:42:37 +02:00
}
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
*
2022-06-19 15:05:30 +02:00
* @param balance {number} Satoshis
* @param toUnit {string} Value from models/bitcoinUnits.js, for example `BitcoinUnit.SATS`
2020-06-09 16:08:18 +02:00
* @param withFormatting {boolean} Works only with `BitcoinUnit.SATS`, makes spaces wetween groups of 000
* @returns {string}
*/
2022-06-19 15:05:30 +02:00
export function formatBalancePlain(balance = 0, toUnit: string, withFormatting = false) {
2020-06-09 16:08:18 +02:00
const newInputValue = formatBalanceWithoutSuffix(balance, toUnit, withFormatting);
2022-06-19 15:05:30 +02:00
// eslint-disable-next-line @typescript-eslint/no-use-before-define
2024-01-28 16:11:08 +01:00
return _leaveNumbersAndDots(newInputValue.toString());
2020-06-09 16:08:18 +02:00
}
2022-06-19 15:05:30 +02:00
export function _leaveNumbersAndDots(newInputValue: string) {
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
2022-06-19 15:05:30 +02:00
/**
* @see https://github.com/BlueWallet/BlueWallet/issues/3466
*/
export function formatStringAddTwoWhiteSpaces(text: string): string {
2021-08-01 21:05:21 +02:00
return `${text} `;
}
2022-06-19 15:05:30 +02:00
export default loc;