-
Notifications
You must be signed in to change notification settings - Fork 870
Internationalize units and currency #780
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
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3e4257c
Internationalize data transfer amounts
28105b4
Internationalize units and currency
01fc89a
Remove stray log
79fb1ea
Respond to review comments
ee59df0
Remove byte-size
a0dc712
Move to `web_app`
c18d6f2
Merge branch 'master' into cohenjon-intl-units
8c1a131
Respond to review comments
017bdf7
Use property binding to the app language
49ce60d
Respond to review comments
9d11943
Respond to review comments
435c5a5
s/get/format
6f4a323
Error out on Node
c970ee5
Set the persisted app language before starting the app to prevent iss…
2af1024
Merge branch 'master' into cohenjon-intl-units
5bcc243
Respond to review comments and broaden covered languages
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
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,67 @@ | ||
| /* | ||
| Copyright 2020 The Outline Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| import * as i18n from './data_formatting'; | ||
|
|
||
| describe('formatBytesParts', () => { | ||
| if (process?.versions?.node) { | ||
| it('doesn\'t run on Node', () => { | ||
| expect(() => i18n.formatBytesParts(0, 'en')).toThrow(); | ||
| }); | ||
| } else { | ||
| it('extracts the unit string and value separately', () => { | ||
| const english = i18n.formatBytesParts(0, 'en'); | ||
| expect(english.unit).toEqual('B'); | ||
| expect(english.value).toEqual('0'); | ||
|
|
||
| const korean = i18n.formatBytesParts(2, 'kr'); | ||
| expect(korean.unit).toEqual('B'); | ||
| expect(korean.value).toEqual('2'); | ||
|
|
||
| const russian = i18n.formatBytesParts(3000, 'ru'); | ||
| expect(russian.unit).toEqual('кБ'); | ||
| expect(russian.value).toEqual('3'); | ||
|
|
||
| const simplifiedChinese = i18n.formatBytesParts(1.5 * 10 ** 9, 'zh-CN'); | ||
| expect(simplifiedChinese.unit).toEqual('吉字节'); | ||
| expect(simplifiedChinese.value).toEqual('1.5'); | ||
|
|
||
| const farsi = i18n.formatBytesParts(133.5 * 10 ** 6, 'fa'); | ||
| expect(farsi.unit).toEqual('مگابایت'); | ||
| expect(farsi.value).toEqual('۱۳۳٫۵'); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe('formatBytes', () => { | ||
| if (process?.versions?.node) { | ||
| it('doesn\'t run on Node', () => { | ||
| expect(() => i18n.formatBytes(0, 'en')).toThrow(); | ||
| }); | ||
| } else { | ||
| it('Formats data amounts', () => { | ||
| expect(i18n.formatBytes(2.1, 'zh-TW')).toEqual('2 byte'); | ||
| expect(i18n.formatBytes(7.8 * 10 ** 3, 'ar')).toEqual('8 كيلوبايت'); | ||
| expect(i18n.formatBytes(1.5 * 10 ** 6, 'tr')).toEqual('1,5 MB'); | ||
| expect(i18n.formatBytes(10 * 10 ** 9, 'jp')).toEqual('10 GB'); | ||
| expect(i18n.formatBytes(2.35 * 10 ** 12, 'pr')).toEqual('2.35 TB'); | ||
| }); | ||
|
|
||
| it('Omits trailing zero decimal digits', () => { | ||
| expect(i18n.formatBytes(10 ** 12, 'en')).toEqual('1 TB'); | ||
| }); | ||
| } | ||
| }); |
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,107 @@ | ||
| /* | ||
| Copyright 2020 The Outline Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| // import '@formatjs/intl-numberformat/polyfill' | ||
|
|
||
| // Utility functions for internationalizing numbers and units | ||
|
|
||
| // WARNING! This assumes an ES2020 target as this will always run on browser code in | ||
| // electron's built-in Chromium. This code shouldn't be used by anything running in Node. | ||
|
|
||
| const TERABYTE = 10 ** 12; | ||
| const GIGABYTE = 10 ** 9; | ||
| const MEGABYTE = 10 ** 6; | ||
| const KILOBYTE = 10 ** 3; | ||
|
|
||
| const inWebApp = typeof window !== 'undefined' && typeof window.document !== 'undefined'; | ||
| interface FormatParams { | ||
| value: number; | ||
| unit: 'terabyte'|'gigabyte'|'megabyte'|'kilobyte'|'byte'; | ||
| decimalPlaces: number; | ||
| } | ||
|
|
||
| function getDataFormattingParams(numBytes: number): FormatParams { | ||
| if (numBytes >= TERABYTE) { | ||
| return {value: numBytes / TERABYTE, unit: 'terabyte', decimalPlaces: 2}; | ||
| } else if (numBytes >= GIGABYTE) { | ||
| return {value: numBytes / GIGABYTE, unit: 'gigabyte', decimalPlaces: 2}; | ||
| } else if (numBytes >= MEGABYTE) { | ||
| return {value: numBytes / MEGABYTE, unit: 'megabyte', decimalPlaces: 1}; | ||
| } else if (numBytes >= KILOBYTE) { | ||
| return {value: numBytes / KILOBYTE, unit: 'kilobyte', decimalPlaces: 0}; | ||
| } | ||
| return {value: numBytes, unit: 'byte', decimalPlaces: 0}; | ||
| } | ||
|
|
||
| function makeDataAmountFormatter(language: string, params: FormatParams) { | ||
| // We need to cast through `unknown` since `tsc` mistakenly omits the 'unit' field in | ||
| // `NumberFormatOptions`. | ||
| const options = { | ||
| style: 'unit', | ||
| unit: params.unit, | ||
| unitDisplay: 'short', | ||
| maximumFractionDigits: params.decimalPlaces | ||
| } as unknown as Intl.NumberFormatOptions; | ||
| return new Intl.NumberFormat(language, options); | ||
| } | ||
|
|
||
| interface DataAmountParts { | ||
| value: string; | ||
| unit: string; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a localized amount of bytes as a separate value and unit. This is useful for styling | ||
| * the unit and the value differently, or if you need them in separate nodes in the layout. | ||
| * | ||
| * @param {number} numBytes An amount of data to format. | ||
| * @param {string} language The ISO language code for the lanugage to translate to, eg 'en'. | ||
| */ | ||
| export function formatBytesParts(numBytes: number, language: string): DataAmountParts { | ||
| if (!inWebApp) { | ||
| throw new Error('formatBytesParts only works in web app code. Node usage isn\'t supported.'); | ||
| } | ||
| const params = getDataFormattingParams(numBytes); | ||
| const parts = makeDataAmountFormatter(language, params).formatToParts(params.value); | ||
| // Cast away the type since `tsc` mistakenly omits the possibility for a 'unit' part | ||
| const isUnit = (part: Intl.NumberFormatPart) => (part as {type: string}).type === 'unit'; | ||
| const unitText = parts.find(isUnit).value; | ||
| return { | ||
| value: parts.filter((part: Intl.NumberFormatPart) => !isUnit(part)) | ||
| .map((part: Intl.NumberFormatPart) => part.value) | ||
| .join('') | ||
| .trim(), | ||
| // Special case for "byte", since we'd rather be consistent with "KB", etc. "byte" is | ||
| // presumably used due to the example in the Unicode standard, | ||
| // http://unicode.org/reports/tr35/tr35-general.html#Example_Units | ||
| unit: unitText === 'byte' ? 'B' : unitText | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a string representation of a number of bytes, translated into the given language | ||
| * | ||
| * @param {Number} numBytes An amount of data to format. | ||
| * @param {string} language The ISO language code for the lanugage to translate to, eg 'en'. | ||
| * @returns {string} The formatted data amount. | ||
| */ | ||
| export function formatBytes(numBytes: number, language: string): string { | ||
| if (!inWebApp) { | ||
| throw new Error('formatBytes only works in web app code. Node usage isn\'t supported.'); | ||
| } | ||
| const params = getDataFormattingParams(numBytes); | ||
| return makeDataAmountFormatter(language, params).format(params.value); | ||
| } |
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
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.