-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[Fix] Localize currency symbol #8299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mountiny
merged 32 commits into
Expensify:main
from
mdneyazahmad:fix/7915-localize-currency-symbol
Jun 6, 2022
Merged
Changes from 10 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
7890252
fix: localize currency symbol
mdneyazahmad 3fa9bc3
refactor: move common function to hoc component
mdneyazahmad fad813c
refactor: rename variable names
mdneyazahmad 0d56d96
refactor: ltr symbol component
mdneyazahmad 1f50122
feat: add currency symbol utils
mdneyazahmad df003d6
refactor: currency utils functions
mdneyazahmad 3cd30fa
test: add CurrencySymbolUtilsTest
mdneyazahmad 155dc02
test: replace require to import
mdneyazahmad 3e57e26
test: fix lint error
mdneyazahmad 6a0e400
test: fix lint error
mdneyazahmad 84bcdeb
refactor: CurrencySymbolUtils
mdneyazahmad 696b7d5
test: refactor names
mdneyazahmad 3ef60e8
docs: update comments
mdneyazahmad 5992dfc
docs: update comment
mdneyazahmad b6d518c
merge main branch
mdneyazahmad 21d3bc0
merge main branch
mdneyazahmad 671f2d2
add comment for currencyList.json
mdneyazahmad 7c4aa7e
refactor: IOUAmountPage
mdneyazahmad 291782f
update comment in tests/unit/CurrencySymbolUtilsTest.js
mdneyazahmad 1841022
refactor: TextInputWithCurrencySymbol
mdneyazahmad 6147523
refactor: move inline function to class method
mdneyazahmad 8e9764f
refactor: IOUAmountPage
mdneyazahmad 46148a3
docs: add jsdocs
mdneyazahmad f85bbde
style: format code
mdneyazahmad 04882ce
fix merge conflict
mdneyazahmad 355e65d
refactor: remove unused code
mdneyazahmad fc91394
Merge branch 'fix/7915-localize-currency-symbol' of github.com:mdneya…
mdneyazahmad c25bf64
style: fix eslint error
mdneyazahmad 133a62e
chore: update comment
mdneyazahmad f018a24
test: fix typo
mdneyazahmad c93b4cb
test: update comment
mdneyazahmad d87f569
fix: merge conflict
mdneyazahmad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import _ from 'underscore'; | ||
import * as NumberFormatUtils from './NumberFormatUtils'; | ||
|
||
/** | ||
* Get localized currency symbol for SO4217 Code | ||
* @param {String} preferredLocale | ||
* @param {String} currencyCode | ||
* @return {String} | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
function getLocalizedCurrencySymbol(preferredLocale, currencyCode) { | ||
const parts = NumberFormatUtils.formatToParts(preferredLocale, 0, { | ||
style: 'currency', | ||
currency: currencyCode, | ||
}); | ||
const currencySymbol = _.find(parts, part => part.type === 'currency').value; | ||
return currencySymbol; | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Is currency symbol to left | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param {String} preferredLocale | ||
* @param {String} currencyCode | ||
* @return {Boolean} | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
function isCurrencySymbolLTR(preferredLocale, currencyCode) { | ||
const parts = NumberFormatUtils.formatToParts(preferredLocale, 0, { | ||
style: 'currency', | ||
currency: currencyCode, | ||
}); | ||
|
||
// The first element of parts will be type: currency for all currency | ||
// Where it starts with symbol and the other will have it at last | ||
// If it is not the first, it must be at last | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const isLeft = parts[0].type === 'currency'; | ||
return isLeft; | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export { | ||
getLocalizedCurrencySymbol, | ||
isCurrencySymbolLTR, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import _ from 'underscore'; | ||
import * as CurrencySymbolUtils from '../../src/libs/CurrencySymbolUtils'; | ||
import currencyList from './currencyList.json'; | ||
|
||
const currencyCodeList = _.keys(currencyList); | ||
|
||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const AVAILABLE_LOCALES = ['en', 'es']; | ||
|
||
// Contains item [isLeft, locale, currencyCode] | ||
const symbolPositions = [ | ||
[true, 'en', 'USD'], | ||
[false, 'es', 'USD'], | ||
]; | ||
|
||
describe('CurrencySymbolUtils', () => { | ||
describe('getLocalizedCurrencySymbol', () => { | ||
test.each(AVAILABLE_LOCALES)('returns non empty string for all currencyCode with preferredLocale %s', (prefrredLocale) => { | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_.forEach(currencyCodeList, (currencyCode) => { | ||
const localizedSymbol = CurrencySymbolUtils.getLocalizedCurrencySymbol(prefrredLocale, currencyCode); | ||
|
||
expect(typeof localizedSymbol).toBe('string'); | ||
expect(localizedSymbol.length).toBeGreaterThan(0); | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}); | ||
}); | ||
|
||
describe('isCurrencySymbolLTR', () => { | ||
test.each(symbolPositions)('returns %s for prefrredLocale %s and currencyCode %s', (isLeft, locale, currencyCode) => { | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const isSymbolLeft = CurrencySymbolUtils.isCurrencySymbolLTR(locale, currencyCode); | ||
expect(isSymbolLeft).toBe(isLeft); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.