Skip to content

Commit 27653f9

Browse files
authored
fix: formatNumber is now automatically using the locale now (#497)
1 parent 96fd9c8 commit 27653f9

8 files changed

Lines changed: 18 additions & 26 deletions

File tree

CONTRIBUTING.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,17 @@ We recommend the [i18n-ally](https://marketplace.visualstudio.com/items?itemName
369369

370370
The extension is included in our workspace recommendations, so VSCode should prompt you to install it.
371371

372-
### Formatting with locale
372+
### Formatting numbers and dates
373373

374-
When formatting numbers or dates that should respect the user's locale, pass the locale:
374+
Use vue-i18n's built-in formatters for locale-aware formatting:
375375

376-
```typescript
377-
const { locale } = useI18n()
378-
const formatted = formatNumber(12345, locale.value) // "12,345" in en-US
376+
```vue
377+
<template>
378+
<p>{{ $n(12345) }}</p>
379+
<!-- "12,345" in en-US, "12 345" in fr-FR -->
380+
<p>{{ $d(new Date()) }}</p>
381+
<!-- locale-aware date -->
382+
</template>
379383
```
380384

381385
## Testing

app/components/PackageCard.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const emit = defineEmits<{
123123
<dt class="sr-only">{{ $t('package.card.weekly_downloads') }}</dt>
124124
<dd class="flex items-center gap-1.5">
125125
<span class="i-carbon:chart-line w-3.5 h-3.5 inline-block" aria-hidden="true" />
126-
<span class="font-mono">{{ formatNumber(result.downloads.weekly) }}/w</span>
126+
<span class="font-mono">{{ $n(result.downloads.weekly) }}/w</span>
127127
</dd>
128128
</div>
129129
</dl>
@@ -158,7 +158,7 @@ const emit = defineEmits<{
158158
>
159159
<span class="i-carbon:chart-line w-3.5 h-3.5 inline-block" aria-hidden="true" />
160160
<span class="font-mono text-xs">
161-
{{ formatNumber(result.downloads.weekly) }} {{ $t('common.per_week') }}
161+
{{ $n(result.downloads.weekly) }} {{ $t('common.per_week') }}
162162
</span>
163163
</div>
164164
</div>

app/pages/@[org].vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import { formatNumber } from '#imports'
32
import type { FilterChip, SortOption } from '#shared/types/preferences'
43
import { debounce } from 'perfect-debounce'
54
@@ -157,7 +156,7 @@ defineOgImageComponent('Default', {
157156
<div>
158157
<h1 class="font-mono text-2xl sm:text-3xl font-medium">@{{ orgName }}</h1>
159158
<p v-if="status === 'success'" class="text-fg-muted text-sm mt-1">
160-
{{ $t('org.public_packages', { count: formatNumber(packageCount) }, packageCount) }}
159+
{{ $t('org.public_packages', { count: $n(packageCount) }, packageCount) }}
161160
</p>
162161
</div>
163162

@@ -181,7 +180,7 @@ defineOgImageComponent('Default', {
181180
>
182181
<span class="i-carbon:chart-line w-3.5 h-3.5" aria-hidden="true" />
183182
<span class="font-mono"
184-
>{{ formatNumber(totalWeeklyDownloads) }} {{ $t('common.per_week') }}</span
183+
>{{ $n(totalWeeklyDownloads) }} {{ $t('common.per_week') }}</span
185184
>
186185
</p>
187186
</div>

app/pages/[...package].vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,6 @@ function formatBytes(bytes: number): string {
258258
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
259259
}
260260
261-
function formatNumber(num: number): string {
262-
return new Intl.NumberFormat('en-US').format(num)
263-
}
264-
265261
function getDependencyCount(version: PackumentVersion | null): number {
266262
if (!version?.dependencies) return 0
267263
return Object.keys(version.dependencies).length
@@ -360,7 +356,7 @@ onKeyStroke(
360356
defineOgImageComponent('Package', {
361357
name: () => pkg.value?.name ?? 'Package',
362358
version: () => displayVersion.value?.version ?? '',
363-
downloads: () => (downloads.value ? formatNumber(downloads.value.downloads) : ''),
359+
downloads: () => (downloads.value ? $n(downloads.value.downloads) : ''),
364360
license: () => pkg.value?.license ?? '',
365361
primaryColor: '#60a5fa',
366362
})

app/pages/search.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import { formatNumber } from '#imports'
32
import type { FilterChip, SortOption } from '#shared/types/preferences'
43
import { onKeyDown } from '@vueuse/core'
54
import { debounce } from 'perfect-debounce'
@@ -783,7 +782,7 @@ defineOgImageComponent('Default', {
783782
{{
784783
$t(
785784
'search.found_packages',
786-
{ count: formatNumber(visibleResults.total) },
785+
{ count: $n(visibleResults.total) },
787786
visibleResults.total,
788787
)
789788
}}

app/pages/~[username]/index.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import { formatNumber } from '#imports'
32
import { debounce } from 'perfect-debounce'
43
54
const route = useRoute('~username')
@@ -192,7 +191,7 @@ defineOgImageComponent('Default', {
192191
<div>
193192
<h1 class="font-mono text-2xl sm:text-3xl font-medium">~{{ username }}</h1>
194193
<p v-if="results?.total" class="text-fg-muted text-sm mt-1">
195-
{{ $t('org.public_packages', { count: formatNumber(results.total) }, results.total) }}
194+
{{ $t('org.public_packages', { count: $n(results.total) }, results.total) }}
196195
</p>
197196
</div>
198197

@@ -216,7 +215,7 @@ defineOgImageComponent('Default', {
216215
>
217216
<span class="i-carbon:chart-line w-3.5 h-3.5" aria-hidden="true" />
218217
<span class="font-mono"
219-
>{{ formatNumber(totalWeeklyDownloads) }} {{ $t('common.per_week') }}</span
218+
>{{ $n(totalWeeklyDownloads) }} {{ $t('common.per_week') }}</span
220219
>
221220
</p>
222221
</div>

app/utils/formatters.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/** @public */
2-
export function formatNumber(num: number, _locale?: string): string {
3-
// TODO: Support different locales (needs care to ensure hydration works correctly)
4-
return new Intl.NumberFormat('en-US').format(num)
5-
}
6-
71
/** @public */
82
export function toIsoDateString(date: Date): string {
93
const year = date.getUTCFullYear()

scripts/compare-translations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import process from 'node:process'
23
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
34
import { join } from 'node:path'

0 commit comments

Comments
 (0)