You've already forked tf2wikipricing
fix, feat: add currency conversion stub
uses proper JS api for currency formatting and preparations for showing other currencies based on page locale
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { ItemPriceData } from "./priceService";
|
import { ItemPriceData } from "./priceService";
|
||||||
import { convertTF2PriceToUSD } from "./utils/currency";
|
import { convertTF2PriceToUSD, defaultCurrencyForPageLocale, convertUSD } from "./utils/currency";
|
||||||
import { formatPrice } from "./utils/formatting";
|
import { formatPrice } from "./utils/formatting";
|
||||||
import { $T } from "./utils/localization";
|
import { $T } from "./utils/localization";
|
||||||
|
import { logError } from "./utils/log";
|
||||||
|
|
||||||
export function createPriceRow(qualityName: string, data: ItemPriceData, keyPrice: ItemPriceData, locale: string, wikiPage: string = null): HTMLTableRowElement {
|
export function createPriceRow(qualityName: string, data: ItemPriceData, keyPrice: ItemPriceData, locale: string, wikiPage: string = null): HTMLTableRowElement {
|
||||||
const priceRow = document.createElement("tr");
|
const priceRow = document.createElement("tr");
|
||||||
@@ -23,14 +24,25 @@ export function createPriceRow(qualityName: string, data: ItemPriceData, keyPric
|
|||||||
var priceString: string = ''
|
var priceString: string = ''
|
||||||
|
|
||||||
if(data) {
|
if(data) {
|
||||||
const currencyFormatter = new Intl.NumberFormat(locale, {
|
const gamePrice = formatPrice(data.keys, data.metal, keyPrice.metal, locale).trim()
|
||||||
minimumFractionDigits: 2,
|
const realPriceUSD = convertTF2PriceToUSD(data.keys, data.metal, keyPrice.metal)
|
||||||
maximumFractionDigits: 2,
|
|
||||||
});
|
const USDFormatter = new Intl.NumberFormat(locale, { style: "currency", currency: 'USD' })
|
||||||
|
var realPriceString = USDFormatter.format(realPriceUSD)
|
||||||
|
const currency = defaultCurrencyForPageLocale(locale) ?? 'USD'
|
||||||
|
if(currency !== 'USD') {
|
||||||
|
try {
|
||||||
|
const realPrice = convertUSD(realPriceUSD, currency)
|
||||||
|
const currencyFormatter = new Intl.NumberFormat(locale, { style: "currency", currency: currency })
|
||||||
|
realPriceString = currencyFormatter.format(realPrice).replace(/\s+/g, '')
|
||||||
|
} catch (e) {
|
||||||
|
logError(`Failed to convert USD ${realPriceUSD} to ${currency}`, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
priceString += [
|
priceString += [
|
||||||
formatPrice(data.keys, data.metal, keyPrice.metal, locale).trim(),
|
gamePrice,
|
||||||
'(US$' + currencyFormatter.format(convertTF2PriceToUSD(data.keys, data.metal, keyPrice.metal)) + ')'
|
'(' + realPriceString + ')'
|
||||||
].join(' ').trim()
|
].join(' ').trim()
|
||||||
} else {
|
} else {
|
||||||
priceString += $T('Data unavailable')
|
priceString += $T('Data unavailable')
|
||||||
|
|||||||
@@ -1,8 +1,47 @@
|
|||||||
import { conversion_ref_usd } from '../config';
|
import { conversion_ref_usd } from '../config';
|
||||||
|
|
||||||
|
export function defaultCurrencyForPageLocale(locale: string): string | null {
|
||||||
|
switch (locale) {
|
||||||
|
case 'de':
|
||||||
|
case 'fr':
|
||||||
|
case 'it':
|
||||||
|
case 'nl':
|
||||||
|
case 'hu':
|
||||||
|
return 'EUR';
|
||||||
|
case 'ja':
|
||||||
|
return 'JPY';
|
||||||
|
case 'tr':
|
||||||
|
return 'TRY';
|
||||||
|
case 'da':
|
||||||
|
return 'DKK';
|
||||||
|
case 'pt-br':
|
||||||
|
return 'BRL';
|
||||||
|
case 'zh-hans':
|
||||||
|
case 'zh-hant':
|
||||||
|
return 'CNY';
|
||||||
|
case 'ko':
|
||||||
|
return 'KRW';
|
||||||
|
case 'cs':
|
||||||
|
return 'CZK';
|
||||||
|
case 'ru':
|
||||||
|
return 'RUB';
|
||||||
|
case 'pl':
|
||||||
|
return 'PLN';
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function convertTF2PriceToUSD(keys: number, metal: number, keyPrice: number): number {
|
export function convertTF2PriceToUSD(keys: number, metal: number, keyPrice: number): number {
|
||||||
const pureMetal = (keys * keyPrice) + metal;
|
const pureMetal = (keys * keyPrice) + metal;
|
||||||
|
|
||||||
// Round price up to nearest cent
|
// Round price up to nearest cent
|
||||||
return Math.ceil(pureMetal * conversion_ref_usd * 100) / 100
|
return Math.ceil(pureMetal * conversion_ref_usd * 100) / 100
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function convertUSD(usd: number, currency: string): number {
|
||||||
|
const cur = currency.toUpperCase()
|
||||||
|
if(cur === 'USD') return usd
|
||||||
|
|
||||||
|
throw new Error(`Not implemented`)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user