basklee-svelte 0.9.11 → 0.9.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -528,7 +528,7 @@ Page titles, descriptions and link-preview metadata follow the current page
528
528
  and selected language, including client navigation. Public events and banner
529
529
  images come from Application Load.
530
530
  A product page takes the product's SEO tab: its heading is the page title (the product name without one), its meta description and keywords fill their tags, and its own Open Graph and Twitter Cards tags are added in the page's language only, replacing a default tag of the same name.
531
- Pages with a canonical address also link the same page in the other storefront languages (`hreflang`) and set `og:type` and `og:locale`; a journal article adds its publication date (`article:published_time`) and schema.org `BlogPosting` data. The store icon (`logos.ico`) is the page's `<link rel="icon">`. An unknown `?locale` opens the default language.
531
+ Pages with a canonical address also link the same page in the other storefront languages (`hreflang`) and set `og:type` and `og:locale`; a journal article adds its publication date (`article:published_time`) and schema.org `BlogPosting` data. The store icon (`logos.ico`) is the page's `<link rel="icon">`. An unknown `?locale` opens the language closest to the browser's, else the first one. A first visit without `?locale` opens in the storefront language closest to the browser's languages (`Accept-Language`; `navigator.languages` in the app build): the same code first, then the same language (`ru-RU` finds `ru`, `en` finds `en-GB`); the first available language when none of them is offered.
532
532
 
533
533
  List pages (journal, categories, stores) link their previous and next pages as real links (`?page=2`). Each later page has its own canonical address and a title with its page number; a journal page past the last one answers 404.
534
534
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basklee-svelte",
3
- "version": "0.9.11",
3
+ "version": "0.9.12",
4
4
  "description": "SvelteKit 2 storefront for your Basklee marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English, Russian and Spanish, plus an Android app build. Powered by Basklee: https://basklee.com",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -3,6 +3,7 @@ import type { ComputedRef } from '@/lib/basklee/reactivity.svelte';
3
3
  import { defineStore } from '@/lib/basklee/scope.svelte';
4
4
  import type { StoreScope } from '@/lib/basklee/scope.svelte';
5
5
  import { resolveBaskleeDomain } from '@/lib/stores/baskleeDomainAccess.svelte';
6
+ import { closestLocale } from '@/lib/utils/basklee/presentation';
6
7
  import '@/lib/stores/basklee.svelte';
7
8
  import type { IAppLoadCommon } from '@/lib/interfaces/basklee/appBase/IAppLoadCommon';
8
9
 
@@ -81,9 +82,26 @@ export function useBaskleeSelections(
81
82
  }
82
83
  }
83
84
 
85
+ // A language still offered stays; in the browser (the app build loads
86
+ // there) the one closest to navigator.languages; else the first one.
87
+ function browserLocale() {
88
+ if (typeof window === 'undefined') {
89
+ return undefined;
90
+ }
91
+ const code = closestLocale(
92
+ Object.entries(locales.value).map(
93
+ ([key, value]) => value.code?.trim() || key,
94
+ ),
95
+ window.navigator.languages ?? [],
96
+ );
97
+ return code ? findLocale(code) : undefined;
98
+ }
99
+
84
100
  function reconcile() {
85
101
  const selected =
86
- findLocale(localeCode.value) ?? Object.entries(locales.value)[0];
102
+ findLocale(localeCode.value) ??
103
+ browserLocale() ??
104
+ Object.entries(locales.value)[0];
87
105
  localeCode.value = selected?.[1].code?.trim() || selected?.[0] || '';
88
106
  currencyId.value = availableCurrency(
89
107
  currencies.value[currencyId.value]?.allowed_select
@@ -70,3 +70,56 @@ export function localesMatch(left: string, right: string) {
70
70
  value.trim().toLowerCase().replaceAll('_', '-');
71
71
  return normalize(left) === normalize(right);
72
72
  }
73
+
74
+ /**
75
+ * The languages a customer's browser asks for, most wanted first: an
76
+ * Accept-Language header («ru-RU,ru;q=0.9,en;q=0.8») or navigator.languages.
77
+ */
78
+ export function wantedLanguages(
79
+ source: string | readonly string[] | null | undefined,
80
+ ): string[] {
81
+ if (!source) {
82
+ return [];
83
+ }
84
+ if (typeof source !== 'string') {
85
+ return source.map((tag) => tag.trim()).filter(Boolean);
86
+ }
87
+ return source
88
+ .split(',')
89
+ .map((part, index) => {
90
+ const [tag = '', ...parameters] = part.split(';');
91
+ const weight = parameters
92
+ .map((parameter) => /^q=([0-9.]+)$/i.exec(parameter.trim()))
93
+ .find(Boolean);
94
+ return { tag: tag.trim(), weight: weight ? Number(weight[1]) : 1, index };
95
+ })
96
+ .filter((item) => item.tag && item.tag !== '*' && item.weight > 0)
97
+ .sort((left, right) => right.weight - left.weight || left.index - right.index)
98
+ .map((item) => item.tag);
99
+ }
100
+
101
+ /**
102
+ * The shop's language closest to what the customer wants, in the customer's
103
+ * order: the same tag first (pt_BR = pt-BR), then the same language (ru-RU
104
+ * finds ru, en finds en-GB). Nothing when no wanted language is offered.
105
+ */
106
+ export function closestLocale(
107
+ available: readonly string[],
108
+ wanted: readonly string[],
109
+ ): string | undefined {
110
+ const language = (tag: string) =>
111
+ tag.trim().toLowerCase().split(/[-_]/)[0] ?? '';
112
+ for (const tag of wanted) {
113
+ const same = available.find((code) => localesMatch(code, tag));
114
+ if (same) {
115
+ return same;
116
+ }
117
+ const kin = language(tag)
118
+ ? available.find((code) => language(code) === language(tag))
119
+ : undefined;
120
+ if (kin) {
121
+ return kin;
122
+ }
123
+ }
124
+ return undefined;
125
+ }
@@ -1,6 +1,10 @@
1
1
  import { loadBaskleeAppLoad } from '@/lib/basklee/appLoadServer';
2
2
  import { BaskleeStore } from '@/lib/basklee/store.svelte';
3
3
  import { storefrontErrorLocale } from '@/lib/utils/basklee/errorPageLabels';
4
+ import {
5
+ closestLocale,
6
+ wantedLanguages,
7
+ } from '@/lib/utils/basklee/presentation';
4
8
  import type { LayoutServerLoad } from './$types';
5
9
 
6
10
  // Initialization depends on the request domain and session, so pages are dynamic.
@@ -18,8 +22,19 @@ export const load: LayoutServerLoad = async (event) => {
18
22
  const locale = event.untrack(() =>
19
23
  event.url.searchParams.get('locale'),
20
24
  );
21
- if (locale) {
22
- store.setLocale(locale);
25
+ // ?locale= first, then the languages the browser asks for
26
+ // (Accept-Language); the storefront's first language when none is offered.
27
+ const wanted = closestLocale(
28
+ Object.entries(store.locales ?? {}).map(
29
+ ([key, value]) => value.code?.trim() || key,
30
+ ),
31
+ [
32
+ locale ?? '',
33
+ ...wantedLanguages(event.request.headers.get('accept-language')),
34
+ ].filter(Boolean),
35
+ );
36
+ if (wanted) {
37
+ store.setLocale(wanted);
23
38
  }
24
39
  if (!store.isReady) {
25
40
  store.localeCode = storefrontErrorLocale(locale, store.localeCode);