Shopify offers a rather fustrating solution for localization on the front-end and developers are likely at the behest of their slop served up as the "Geo" app... We are not animals. Anyway, this is a tiny little url parser to extract subfolder prefixes and provides determination for cases where you are employing your own solution and have a webshop operating multiple domains and/or targeting different markets using subfolders.
The typings will explain in rich detail each value in the closure. If you don't use TypeScript then maybe try acting like an adult.
Usage is simple, and only configuration required is setting you main domain, by default the function assumed your main domain is using a .com
extension, change this if it differs.
const url = root();
url.domain: string // your-store.com
url.root: string // The root subfolder prefix, e.g: /en-us
url.path: string // pathname excluding subfolders or '' if at root
url.localizedDomain: boolean // Whether or not domain differs from main one
url.localizedSubfolder: boolean // Whether url contains both language and country, e.g: en-us
url.languageCode: string // The extracted language code in uppercase, e.g: EN
url.countryCode: string // The extracted country code in uppercase, e.g: US
url.subfolderCountry: boolean // Whether or not url contains a subfolder country code
url.subfolderLanguage: boolean // Whether or not url contains a subfolder language code
Couple it with the following GraphQL logic with custom metafields.
export function market (countryCode: string) {
return `query@inContext(country:${countryCode}){localization{country{availableLanguages{isoCode name endonymName}currency{isoCode symbol}isoCode name unitSystem market{shippingFree:metafield(namespace:"shipping",key:"free"){value}shippingThreshold:metafield(namespace:"shipping"key:"threshold"){value}shippingPrice:metafield(namespace:"shipping"key:"price"){value}returnThreshold:metafield(namespace:"return"key:"threshold"){value}returnFree:metafield(namespace:"return",key:"free"){value}returnPrice:metafield(namespace:"return",key:"price"){value}isEU:metafield(namespace:"zone",key:"european_union"){value}domain:metafield(namespace:"domain",key:"host"){value}domainLanguage:metafield(namespace:"domain"key:"language"){value}root:metafield(namespace:"domain",key:"subfolder"){value}}}}}`;
}