-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): format currency based on Sanity locale fields
- Loading branch information
Showing
12 changed files
with
198 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,9 @@ | ||
export function formatAsCurrency( | ||
number: number, | ||
locale: string, | ||
currency: string, | ||
) { | ||
return new Intl.NumberFormat(locale, { style: "currency", currency }).format( | ||
number, | ||
); | ||
} |
This file contains 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,4 @@ | ||
export interface LocaleDocument { | ||
locale: string; | ||
currency: string; | ||
} |
This file contains 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,5 @@ | ||
import { groq } from "next-sanity"; | ||
|
||
import { localeID } from "studio/schemas/documents/locale"; | ||
|
||
export const LOCALE_QUERY = groq`*[_type == "${localeID}" && _id == "${localeID}"][0]`; |
This file contains 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 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 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 { defineField, defineType } from "sanity"; | ||
|
||
import { relevantCurrencies } from "studio/utils/currencies"; | ||
import { relevantLocales } from "studio/utils/locales"; | ||
|
||
export const localeID = "locale"; | ||
|
||
const locale = defineType({ | ||
name: localeID, | ||
type: "document", | ||
fields: [ | ||
defineField({ | ||
name: "locale", | ||
type: "string", | ||
title: "Locale", | ||
description: "Select the most relevant locale for the website.", | ||
initialValue: relevantLocales[0].code, | ||
options: { | ||
list: relevantLocales.map(({ title, code }) => ({ | ||
title: `${title} (${code})`, | ||
value: code, | ||
})), | ||
}, | ||
}), | ||
defineField({ | ||
name: "currency", | ||
type: "string", | ||
title: "Currency", | ||
description: "Select the most relevant currency for the website.", | ||
initialValue: relevantCurrencies[0].code, | ||
options: { | ||
list: relevantCurrencies.map(({ title, code }) => ({ | ||
title: `${title} (${code})`, | ||
value: code, | ||
})), | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
export default locale; |
This file contains 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,36 @@ | ||
interface Currency { | ||
title: string; | ||
code: string; | ||
} | ||
|
||
// ISO 4217 currency codes (https://en.wikipedia.org/wiki/ISO_4217#List_of_ISO_4217_currency_codes) | ||
export const relevantCurrencies: Currency[] = [ | ||
{ | ||
title: "🇳🇴 Norwegian Krone", | ||
code: "NOK", | ||
}, | ||
{ | ||
title: "🇸🇪 Swedish Krona", | ||
code: "SEK", | ||
}, | ||
{ | ||
title: "🇪🇺 Euro", | ||
code: "EUR", | ||
}, | ||
{ | ||
title: "🇬🇧 British Pound", | ||
code: "GBP", | ||
}, | ||
{ | ||
title: "🇺🇸 US Dollar", | ||
code: "USD", | ||
}, | ||
{ | ||
title: "🇩🇰 Danish Krone", | ||
code: "DKK", | ||
}, | ||
{ | ||
title: "🇮🇸 Icelandic Króna", | ||
code: "ISK", | ||
}, | ||
]; |
This file contains 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,39 @@ | ||
interface Locale { | ||
title: string; | ||
code: string; | ||
} | ||
|
||
export const relevantLocales: Locale[] = [ | ||
{ | ||
title: "🇳🇴 Norway", | ||
code: "nb-NO", | ||
}, | ||
// { | ||
// title: "🇳🇴 Norway", | ||
// code: "nn-NO", | ||
// }, | ||
{ | ||
title: "🇸🇪 Sweden", | ||
code: "sv-SE", | ||
}, | ||
{ | ||
title: "🇬🇧 United Kingdom", | ||
code: "en-GB", | ||
}, | ||
{ | ||
title: "🇺🇸 United States", | ||
code: "en-US", | ||
}, | ||
{ | ||
title: "🇩🇰 Denmark", | ||
code: "da-DK", | ||
}, | ||
{ | ||
title: "🇫🇮 Finland", | ||
code: "fi-FI", | ||
}, | ||
{ | ||
title: "🇮🇸 Iceland", | ||
code: "is-IS", | ||
}, | ||
]; |