|
| 1 | +package localization |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "golang.org/x/text/language" |
| 7 | + "golang.org/x/text/message" |
| 8 | + "golang.org/x/text/number" |
| 9 | + |
| 10 | + currency_lib "github.com/code-payments/code-server/pkg/currency" |
| 11 | + "github.com/pkg/errors" |
| 12 | + |
| 13 | + chatpb "github.com/code-payments/code-protobuf-api/generated/go/chat/v1" |
| 14 | +) |
| 15 | + |
| 16 | +var symbolByCode = map[currency_lib.Code]string{ |
| 17 | + currency_lib.AED: "د.إ", |
| 18 | + currency_lib.AFN: "؋", |
| 19 | + currency_lib.ALL: "Lek", |
| 20 | + currency_lib.ANG: "ƒ", |
| 21 | + currency_lib.AOA: "Kz", |
| 22 | + currency_lib.ARS: "$", |
| 23 | + currency_lib.AUD: "$", |
| 24 | + currency_lib.AWG: "ƒ", |
| 25 | + currency_lib.AZN: "₼", |
| 26 | + currency_lib.BAM: "KM", |
| 27 | + currency_lib.BDT: "৳", |
| 28 | + currency_lib.BBD: "$", |
| 29 | + currency_lib.BGN: "лв", |
| 30 | + currency_lib.BMD: "$", |
| 31 | + currency_lib.BND: "$", |
| 32 | + currency_lib.BOB: "$b", |
| 33 | + currency_lib.BRL: "R$", |
| 34 | + currency_lib.BSD: "$", |
| 35 | + currency_lib.BWP: "P", |
| 36 | + currency_lib.BYN: "Br", |
| 37 | + currency_lib.BZD: "BZ$", |
| 38 | + currency_lib.CAD: "$", |
| 39 | + currency_lib.CHF: "CHF", |
| 40 | + currency_lib.CLP: "$", |
| 41 | + currency_lib.CNY: "¥", |
| 42 | + currency_lib.COP: "$", |
| 43 | + currency_lib.CRC: "₡", |
| 44 | + currency_lib.CUC: "$", |
| 45 | + currency_lib.CUP: "₱", |
| 46 | + currency_lib.CZK: "Kč", |
| 47 | + currency_lib.DKK: "kr", |
| 48 | + currency_lib.DOP: "RD$", |
| 49 | + currency_lib.EGP: "£", |
| 50 | + currency_lib.ERN: "£", |
| 51 | + currency_lib.EUR: "€", |
| 52 | + currency_lib.FJD: "$", |
| 53 | + currency_lib.FKP: "£", |
| 54 | + currency_lib.GBP: "£", |
| 55 | + currency_lib.GEL: "₾", |
| 56 | + currency_lib.GGP: "£", |
| 57 | + currency_lib.GHS: "¢", |
| 58 | + currency_lib.GIP: "£", |
| 59 | + currency_lib.GNF: "FG", |
| 60 | + currency_lib.GTQ: "Q", |
| 61 | + currency_lib.GYD: "$", |
| 62 | + currency_lib.HKD: "$", |
| 63 | + currency_lib.HNL: "L", |
| 64 | + currency_lib.HRK: "kn", |
| 65 | + currency_lib.HUF: "Ft", |
| 66 | + currency_lib.IDR: "Rp", |
| 67 | + currency_lib.ILS: "₪", |
| 68 | + currency_lib.IMP: "£", |
| 69 | + currency_lib.INR: "₹", |
| 70 | + currency_lib.IRR: "﷼", |
| 71 | + currency_lib.ISK: "kr", |
| 72 | + currency_lib.JEP: "£", |
| 73 | + currency_lib.JMD: "J$", |
| 74 | + currency_lib.JPY: "¥", |
| 75 | + currency_lib.KGS: "лв", |
| 76 | + currency_lib.KHR: "៛", |
| 77 | + currency_lib.KMF: "CF", |
| 78 | + currency_lib.KPW: "₩", |
| 79 | + currency_lib.KRW: "₩", |
| 80 | + currency_lib.KYD: "$", |
| 81 | + currency_lib.KZT: "лв", |
| 82 | + currency_lib.LAK: "₭", |
| 83 | + currency_lib.LBP: "£", |
| 84 | + currency_lib.LKR: "₨", |
| 85 | + currency_lib.LRD: "$", |
| 86 | + currency_lib.LTL: "Lt", |
| 87 | + currency_lib.LVL: "Ls", |
| 88 | + currency_lib.MGA: "Ar", |
| 89 | + currency_lib.MKD: "ден", |
| 90 | + currency_lib.MMK: "K", |
| 91 | + currency_lib.MNT: "₮", |
| 92 | + currency_lib.MUR: "₨", |
| 93 | + currency_lib.MXN: "$", |
| 94 | + currency_lib.MYR: "RM", |
| 95 | + currency_lib.MZN: "MT", |
| 96 | + currency_lib.NAD: "$", |
| 97 | + currency_lib.NGN: "₦", |
| 98 | + currency_lib.NIO: "C$", |
| 99 | + currency_lib.NOK: "kr", |
| 100 | + currency_lib.NPR: "₨", |
| 101 | + currency_lib.NZD: "$", |
| 102 | + currency_lib.OMR: "﷼", |
| 103 | + currency_lib.PAB: "B/.", |
| 104 | + currency_lib.PEN: "S/.", |
| 105 | + currency_lib.PHP: "₱", |
| 106 | + currency_lib.PKR: "₨", |
| 107 | + currency_lib.PLN: "zł", |
| 108 | + currency_lib.PYG: "Gs", |
| 109 | + currency_lib.QAR: "﷼", |
| 110 | + currency_lib.RON: "lei", |
| 111 | + currency_lib.RSD: "Дин.", |
| 112 | + currency_lib.RUB: "₽", |
| 113 | + currency_lib.RWF: "RF", |
| 114 | + currency_lib.SAR: "﷼", |
| 115 | + currency_lib.SBD: "$", |
| 116 | + currency_lib.SCR: "₨", |
| 117 | + currency_lib.SEK: "kr", |
| 118 | + currency_lib.SGD: "$", |
| 119 | + currency_lib.SHP: "£", |
| 120 | + currency_lib.SOS: "S", |
| 121 | + currency_lib.SRD: "$", |
| 122 | + currency_lib.SSP: "£", |
| 123 | + currency_lib.STD: "Db", |
| 124 | + currency_lib.SVC: "$", |
| 125 | + currency_lib.SYP: "£", |
| 126 | + currency_lib.THB: "฿", |
| 127 | + currency_lib.TOP: "T$", |
| 128 | + currency_lib.TRY: "₺", |
| 129 | + currency_lib.TTD: "TT$", |
| 130 | + currency_lib.TWD: "NT$", |
| 131 | + currency_lib.UAH: "₴", |
| 132 | + currency_lib.USD: "$", |
| 133 | + currency_lib.UYU: "$U", |
| 134 | + currency_lib.UZS: "лв", |
| 135 | + currency_lib.VND: "₫", |
| 136 | + currency_lib.XCD: "$", |
| 137 | + currency_lib.YER: "﷼", |
| 138 | + currency_lib.ZAR: "R", |
| 139 | + currency_lib.ZMW: "ZK", |
| 140 | +} |
| 141 | + |
| 142 | +// FormatFiat formats a currency amount into a string in the provided locale |
| 143 | +func FormatFiat(locale language.Tag, code currency_lib.Code, amount float64, ofKin bool) (string, error) { |
| 144 | + isRtlScript := isRtlScript(locale) |
| 145 | + |
| 146 | + decimals := 2 |
| 147 | + if code == currency_lib.KIN { |
| 148 | + decimals = 0 |
| 149 | + amount = float64(uint64(amount)) |
| 150 | + } |
| 151 | + |
| 152 | + printer := message.NewPrinter(locale) |
| 153 | + amountAsDecimal := number.Decimal(amount, number.Scale(decimals)) |
| 154 | + formattedAmount := printer.Sprint(amountAsDecimal) |
| 155 | + |
| 156 | + symbol := symbolByCode[code] |
| 157 | + |
| 158 | + suffixKey := CoreOfKin |
| 159 | + if code == currency_lib.KIN { |
| 160 | + suffixKey = CoreKin |
| 161 | + } |
| 162 | + |
| 163 | + localizedSuffix, localizedIn, err := localizeKey(locale, suffixKey) |
| 164 | + if err != nil { |
| 165 | + return "", err |
| 166 | + } |
| 167 | + if !ofKin { |
| 168 | + localizedSuffix = "" |
| 169 | + } |
| 170 | + localizedSuffix = strings.TrimSpace(localizedSuffix) |
| 171 | + |
| 172 | + if isRtlScript && !isDefaultLocale(*localizedIn) { |
| 173 | + if len(localizedSuffix) > 0 { |
| 174 | + localizedSuffix = localizedSuffix + " " |
| 175 | + } |
| 176 | + return localizedSuffix + formattedAmount + symbol, nil |
| 177 | + } |
| 178 | + |
| 179 | + if len(localizedSuffix) > 0 { |
| 180 | + localizedSuffix = " " + localizedSuffix |
| 181 | + } |
| 182 | + return symbol + formattedAmount + localizedSuffix, nil |
| 183 | +} |
| 184 | + |
| 185 | +// LocalizeFiatWithVerb is like FormatFiat, but includes a verb for the interaction |
| 186 | +// with the currency amount |
| 187 | +func LocalizeFiatWithVerb(locale language.Tag, verb chatpb.ExchangeDataContent_Verb, code currency_lib.Code, amount float64, ofKin bool) (string, error) { |
| 188 | + localizedAmount, err := FormatFiat(locale, code, amount, ofKin) |
| 189 | + if err != nil { |
| 190 | + return "", err |
| 191 | + } |
| 192 | + |
| 193 | + var key string |
| 194 | + var isAmountBeforeVerb bool |
| 195 | + switch verb { |
| 196 | + case chatpb.ExchangeDataContent_GAVE: |
| 197 | + key = VerbGave |
| 198 | + case chatpb.ExchangeDataContent_RECEIVED: |
| 199 | + key = VerbReceived |
| 200 | + case chatpb.ExchangeDataContent_WITHDREW: |
| 201 | + key = VerbWithdrew |
| 202 | + case chatpb.ExchangeDataContent_DEPOSITED: |
| 203 | + key = VerbDeposited |
| 204 | + case chatpb.ExchangeDataContent_SENT: |
| 205 | + key = VerbSpent |
| 206 | + case chatpb.ExchangeDataContent_RETURNED: |
| 207 | + key = VerbReturned |
| 208 | + isAmountBeforeVerb = true |
| 209 | + case chatpb.ExchangeDataContent_SPENT: |
| 210 | + key = VerbSpent |
| 211 | + case chatpb.ExchangeDataContent_PAID: |
| 212 | + key = VerbPaid |
| 213 | + case chatpb.ExchangeDataContent_PURCHASED: |
| 214 | + key = VerbPurchased |
| 215 | + default: |
| 216 | + return "", errors.Errorf("verb %s is not supported", verb) |
| 217 | + } |
| 218 | + |
| 219 | + localizedVerbText, localizedIn, err := localizeKey(locale, key) |
| 220 | + if err != nil { |
| 221 | + return "", err |
| 222 | + } |
| 223 | + |
| 224 | + if isRtlScript(locale) && !isDefaultLocale(*localizedIn) { |
| 225 | + isAmountBeforeVerb = !isAmountBeforeVerb |
| 226 | + } |
| 227 | + |
| 228 | + if isAmountBeforeVerb { |
| 229 | + return localizedAmount + " " + localizedVerbText, nil |
| 230 | + } |
| 231 | + return localizedVerbText + " " + localizedAmount, nil |
| 232 | +} |
0 commit comments