From e070c60285c0f9bd84b38d201fbcd2009fe9b933 Mon Sep 17 00:00:00 2001 From: jliddev Date: Fri, 20 Nov 2020 14:07:14 -0600 Subject: [PATCH] More number issues for translations --- .../install-from-url-dialog.component.html | 45 ++++--------------- .../install-from-url-dialog.component.ts | 15 +++++++ .../src/app/pages/home/home.module.ts | 2 +- .../src/app/pipes/download-count.pipe.ts | 15 ++----- wowup-electron/src/app/utils/number.utils.ts | 15 +++++++ wowup-electron/src/assets/i18n/ru.json | 2 +- 6 files changed, 44 insertions(+), 50 deletions(-) create mode 100644 wowup-electron/src/app/utils/number.utils.ts diff --git a/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.html b/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.html index 4d565d12..c6dca5a2 100644 --- a/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.html +++ b/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.html @@ -6,23 +6,10 @@

{{ "DIALOGS.INSTALL_FROM_URL.SUPPORTED_SOURCES" | translate }}

{{ "DIALOGS.INSTALL_FROM_URL.ADDON_URL_INPUT_LABEL" | translate }} - - @@ -35,15 +22,7 @@

{{ addon.name }}

{{ addon.author }}

- {{ - "DIALOGS.INSTALL_FROM_URL.DOWNLOAD_COUNT" - | translate - : { - count: addon.downloadCount, - textCount: (addon.downloadCount | downloadCount), - provider: addon.providerName - } - }} + {{ "DIALOGS.INSTALL_FROM_URL.DOWNLOAD_COUNT" | translate: getDownloadCountParams() }}

@@ -66,16 +45,8 @@ - -
+ \ No newline at end of file diff --git a/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.ts b/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.ts index a9ca9a99..f95e8622 100644 --- a/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.ts +++ b/wowup-electron/src/app/components/install-from-url-dialog/install-from-url-dialog.component.ts @@ -7,6 +7,8 @@ import { AddonService } from "../../services/addons/addon.service"; import { SessionService } from "../../services/session/session.service"; import { AlertDialogComponent } from "../alert-dialog/alert-dialog.component"; import { TranslateService } from "@ngx-translate/core"; +import { roundDownloadCount, shortenDownloadCount } from "../../utils/number.utils"; +import { DownloadCountPipe } from "app/pipes/download-count.pipe"; @Component({ selector: "app-install-from-url-dialog", @@ -28,6 +30,7 @@ export class InstallFromUrlDialogComponent implements OnInit, OnDestroy { private _dialog: MatDialog, private _sessionService: SessionService, private _translateService: TranslateService, + private _downloadCountPipe: DownloadCountPipe, public dialogRef: MatDialogRef ) {} @@ -66,6 +69,18 @@ export class InstallFromUrlDialogComponent implements OnInit, OnDestroy { }); } + public getDownloadCountParams() { + const count = 1000; + return { + count, + shortCount: roundDownloadCount(count), + simpleCount: shortenDownloadCount(count, 1), + myriadCount: shortenDownloadCount(count, 4), + textCount: this._downloadCountPipe.transform(count), + provider: this.addon.providerName, + }; + } + async onImportUrl() { this.addon = undefined; this.showInstallSuccess = false; diff --git a/wowup-electron/src/app/pages/home/home.module.ts b/wowup-electron/src/app/pages/home/home.module.ts index a39340ae..aa2753d5 100644 --- a/wowup-electron/src/app/pages/home/home.module.ts +++ b/wowup-electron/src/app/pages/home/home.module.ts @@ -63,6 +63,6 @@ import { HomeComponent } from "./home.component"; OptionsDebugSectionComponent, ], imports: [CommonModule, SharedModule, HomeRoutingModule, MatModule, DirectiveModule], - providers: [DatePipe, GetAddonListItemFilePropPipe], + providers: [DatePipe, GetAddonListItemFilePropPipe, DownloadCountPipe], }) export class HomeModule {} diff --git a/wowup-electron/src/app/pipes/download-count.pipe.ts b/wowup-electron/src/app/pipes/download-count.pipe.ts index 03372cfe..e94d3543 100644 --- a/wowup-electron/src/app/pipes/download-count.pipe.ts +++ b/wowup-electron/src/app/pipes/download-count.pipe.ts @@ -1,6 +1,6 @@ -import { DecimalPipe, formatNumber } from "@angular/common"; import { Pipe, PipeTransform } from "@angular/core"; import { TranslateService } from "@ngx-translate/core"; +import { shortenDownloadCount } from "../utils/number.utils"; @Pipe({ name: "downloadCount", @@ -8,22 +8,15 @@ import { TranslateService } from "@ngx-translate/core"; export class DownloadCountPipe implements PipeTransform { constructor(private translateService: TranslateService) {} - shortenDownloadCount(value: number, nDigit: number): string { - const exponent = Math.log10(value); - const nGroups = Math.floor(exponent / nDigit); - const shortValue = value / Math.pow(10, nGroups * nDigit); - return shortValue.toFixed(0); - } - transform(value: number, ...args: unknown[]): string { const numMatches = /(e\+\d+)/.exec(value.toExponential()); const suffix = numMatches[1]; return suffix ? this.translateService.instant("COMMON.DOWNLOAD_COUNT." + suffix, { - count: this.shortenDownloadCount(value, 3), - simpleCount: this.shortenDownloadCount(value, 1), - myriadCount: this.shortenDownloadCount(value, 4), + count: shortenDownloadCount(value, 3), + simpleCount: shortenDownloadCount(value, 1), + myriadCount: shortenDownloadCount(value, 4), }) : value.toString(); } diff --git a/wowup-electron/src/app/utils/number.utils.ts b/wowup-electron/src/app/utils/number.utils.ts new file mode 100644 index 00000000..0ba6ba62 --- /dev/null +++ b/wowup-electron/src/app/utils/number.utils.ts @@ -0,0 +1,15 @@ +export function shortenDownloadCount(value: number, nDigit: number): string { + const exponent = Math.log10(value); + const nGroups = Math.floor(exponent / nDigit); + const shortValue = value / Math.pow(10, nGroups * nDigit); + return shortValue.toFixed(0); +} + +// This is a horrifying way to round to the nearest tens place +export function roundDownloadCount(value: number): number { + const exp = value.toExponential(); + const numberMatch = /(\d*\.?\d*)e\+(\d+)/.exec(exp); + const number = Math.ceil(parseFloat(numberMatch[1]) * 10); + const exponent = new Array(parseInt(numberMatch[2]) - 1).fill(0); + return parseInt(`${number}${exponent.join("")}`, 10); +} diff --git a/wowup-electron/src/assets/i18n/ru.json b/wowup-electron/src/assets/i18n/ru.json index 5fa77c8f..6e334ebf 100644 --- a/wowup-electron/src/assets/i18n/ru.json +++ b/wowup-electron/src/assets/i18n/ru.json @@ -113,7 +113,7 @@ "ADDON_URL_INPUT_PLACEHOLDER": "Например ссылки GitHub или WowInterface", "CLOSE_BUTTON": "Закрыть", "DESCRIPTION": "Если вы хотите установить модификацию непосредственно по ссылке, вставьте её ниже, чтобы начать.", - "DOWNLOAD_COUNT": "{textCount} {count, plural, one{загрузка} few{загрузки} other{загрузок}} на {provider}", + "DOWNLOAD_COUNT": "{textCount} {shortCount, plural, one{загрузка} few{загрузки} other{загрузок}} на {provider}", "ERROR": { "FAILED_TO_CONNECT": "Не удается подключиться к API, подождите немного и повторите попытку.", "INSTALL_FAILED": "Что-то пошло не так при попытке установить модификацию, попытайтесь снова.\n\nЕсли это сообщение продолжает появляться, вы можете получить помощь в канале #wowup-support нашего Discord сообщества.",