Skip to content

Commit

Permalink
Refactor currencies title translation
Browse files Browse the repository at this point in the history
  • Loading branch information
RodBrowning committed Jul 27, 2022
1 parent 8122f9d commit 2373f83
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 192 deletions.
29 changes: 8 additions & 21 deletions src/Components/CurrencyDisplay/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import './style.scss';

import React, { useEffect, useRef, useState } from 'react';
// eslint-disable-next-line import/order
import { getTranslatedTitle, hasTranslation, userLocale } from './translatedCurrencies';

import PortugueseTitles from './portugueseTitles';
// eslint-disable-next-line sort-imports
import { ICurrenciesInfo } from '../../App';

type DolarAmount = string;
Expand Down Expand Up @@ -43,31 +43,18 @@ const CurrencyDisplay: React.FC<Props> = ({
setSymbolsAndTitles(symbolsAndTitles);
};

const userLocale = () => {
return navigator && navigator.languages && navigator.languages.length ? navigator.languages[0] : navigator.language;
};

const currencyFormatter = Intl.NumberFormat(userLocale() || 'en-US', {
style: 'currency',
currency: currencySymbol,
useGrouping: true,
});

const isPortuguese = () => {
return userLocale() === 'pt-BR';
};

const getPortugueseTitle = (title: string) => {
const joinedTitle = title.split(' ').join('-');
return PortugueseTitles[joinedTitle];
};

const setTitle = (title: string) => {
if (isPortuguese() && getPortugueseTitle(title)) {
const pTitle = getPortugueseTitle(title);
const setTitle = (currencySymbol: string) => {
if (hasTranslation()) {
const pTitle = getTranslatedTitle(currencySymbol);
setCurrencyTitle(pTitle);
} else {
setCurrencyTitle(title);
setCurrencyTitle(currenciesInfo.symbols.symbols![currencySymbol]);
}
};

Expand All @@ -76,7 +63,7 @@ const CurrencyDisplay: React.FC<Props> = ({
const newValue = currenciesInfo.latest.rates![currencySymbol];
setCurrencySymbol(currencySymbol);
setCurrencyVal(newValue);
setTitle(currenciesInfo.symbols.symbols![currencySymbol]);
setTitle(currencySymbol);
setBgCurrencyHandler(index, currencySymbol);
};

Expand Down Expand Up @@ -111,7 +98,7 @@ const CurrencyDisplay: React.FC<Props> = ({
setCurrencyVal(inicialCurrencyVal);
setCurrencySymbol(inicialCurrencySymbol);
setBgCurrencyHandler(index, inicialCurrencySymbol);
setTitle(currenciesInfo.symbols.symbols![inicialCurrencySymbol]);
setTitle(inicialCurrencySymbol);
getSymbolsAndTitles();
}, []);

Expand Down
171 changes: 0 additions & 171 deletions src/Components/CurrencyDisplay/portugueseTitles.ts

This file was deleted.

Loading

0 comments on commit 2373f83

Please sign in to comment.