basklee-nuxt 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
@@ -516,7 +516,7 @@ route data. The homepage retains additional backend Open Graph and Twitter tags
516
516
  through `useHead()`. Profile and logout pages remain excluded from indexing;
517
517
  search results retain their `noindex, follow` policy.
518
518
  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.
519
- 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.
519
+ 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.
520
520
 
521
521
  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.
522
522
 
@@ -657,7 +657,11 @@ from `store.locales` directly. Import it without a data prop. It accepts a
657
657
  `label` (default `Language`), `disabled` (default `false`) and `codeOnly`
658
658
  (default `false`). Missing names
659
659
  fall back to another translation, then the human name or code. Empty data
660
- disables the selector. The store initially selects its first available language.
660
+ disables the selector. A first visit without `?locale` opens in the storefront
661
+ language closest to the browser's languages (`Accept-Language` during the server
662
+ render, `navigator.languages` in the browser): the same code first, then the same
663
+ language (`ru-RU` finds `ru`, `en` finds `en-GB`); the first available language when
664
+ none of them is offered. A language picked in the selector goes into the address.
661
665
 
662
666
  Flags from `flag_img` appear beside the selected language and in every dropdown
663
667
  option that has an available image. Missing or failed images leave the language
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basklee-nuxt",
3
- "version": "0.9.11",
3
+ "version": "0.9.12",
4
4
  "description": "Nuxt 4 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",
@@ -1,6 +1,11 @@
1
- import { abortNavigation, defineNuxtRouteMiddleware } from '#imports';
1
+ import {
2
+ abortNavigation,
3
+ defineNuxtRouteMiddleware,
4
+ useRequestHeaders,
5
+ } from '#imports';
2
6
  import { useBaskleeAppLoad } from '@/composables/useBaskleeAppLoad';
3
7
  import { storefrontPageError } from '@/composables/useBaskleeMissingPage';
8
+ import { wantedLanguages } from '@/utils/basklee/presentation';
4
9
 
5
10
  /**
6
11
  * Bootstrap precedes page setup. A failed Application Load opens the
@@ -13,6 +18,21 @@ export default defineNuxtRouteMiddleware(async (to) => {
13
18
  ? to.query.locale
14
19
  : '';
15
20
 
21
+ // A first visit without ?locale= opens in the storefront language closest
22
+ // to the browser's languages (the request's Accept-Language while the
23
+ // server renders, navigator.languages in the browser); the storefront's
24
+ // first language when none of them is offered.
25
+ if (store.status === 'idle') {
26
+ store.wantLocales([
27
+ locale,
28
+ ...wantedLanguages(
29
+ import.meta.server
30
+ ? useRequestHeaders(['accept-language'])['accept-language']
31
+ : navigator.languages,
32
+ ),
33
+ ]);
34
+ }
35
+
16
36
  await initialize(locale);
17
37
 
18
38
  if (!to.matched.length) {
@@ -74,6 +74,7 @@ function createStoreState(scope: string, pinia: Pinia) {
74
74
  const lengthId = forwardedState(selectionRefs.lengthId);
75
75
  const {
76
76
  setLocale,
77
+ wantLocales,
77
78
  setCurrency,
78
79
  setUnit,
79
80
  } = selections;
@@ -409,6 +410,7 @@ function createStoreState(scope: string, pinia: Pinia) {
409
410
  currentLocale,
410
411
  currentCurrency,
411
412
  setLocale,
413
+ wantLocales,
412
414
  setCurrency,
413
415
  setUnit,
414
416
  getProducts,
@@ -3,6 +3,7 @@ import type { ComputedRef } from 'vue';
3
3
  import { defineStore } from 'pinia';
4
4
  import type { Pinia } from 'pinia';
5
5
  import { resolveBaskleeDomain } from '@/stores/baskleeDomainAccess';
6
+ import { closestLocale } from '@/utils/basklee/presentation';
6
7
  import '@/stores/basklee';
7
8
  import type { IAppLoadCommon } from '@/interfaces/basklee/appBase/IAppLoadCommon';
8
9
 
@@ -17,6 +18,9 @@ export function useBaskleeSelections(
17
18
  const currencyId = ref('');
18
19
  const weightId = ref('');
19
20
  const lengthId = ref('');
21
+ // What the customer asked for before the storefront knew its languages:
22
+ // the address's ?locale=, then the browser's languages.
23
+ const wantedLocales = ref<string[]>([]);
20
24
  const locales = computed(() => init.value?.locale ?? {});
21
25
  const currencies = computed(() => init.value?.currencies ?? {});
22
26
  const weights = computed(() => init.value?.weights ?? {});
@@ -75,8 +79,30 @@ export function useBaskleeSelections(
75
79
  }
76
80
  }
77
81
 
82
+ /** Languages the customer wants, most wanted first (see reconcile). */
83
+ function wantLocales(tags: readonly (string | null | undefined)[]) {
84
+ wantedLocales.value = tags
85
+ .map((tag) => tag?.trim() ?? '')
86
+ .filter(Boolean);
87
+ }
88
+
89
+ function wantedLocale() {
90
+ const code = closestLocale(
91
+ Object.entries(locales.value).map(
92
+ ([key, value]) => value.code?.trim() || key,
93
+ ),
94
+ wantedLocales.value,
95
+ );
96
+ return code ? findLocale(code) : undefined;
97
+ }
98
+
99
+ // A language still offered stays; otherwise the one closest to what the
100
+ // customer wants; otherwise the storefront's first language.
78
101
  function reconcile() {
79
- const selected = findLocale(localeCode.value) ?? Object.entries(locales.value)[0];
102
+ const selected =
103
+ findLocale(localeCode.value) ??
104
+ wantedLocale() ??
105
+ Object.entries(locales.value)[0];
80
106
  localeCode.value = selected?.[1].code?.trim() || selected?.[0] || '';
81
107
  currencyId.value = availableCurrency(
82
108
  currencies.value[currencyId.value]?.allowed_select
@@ -109,6 +135,7 @@ export function useBaskleeSelections(
109
135
  currencyId,
110
136
  weightId,
111
137
  lengthId,
138
+ wantedLocales,
112
139
  locales,
113
140
  currencies,
114
141
  weights,
@@ -116,6 +143,7 @@ export function useBaskleeSelections(
116
143
  currentLocale,
117
144
  currentCurrency,
118
145
  setLocale,
146
+ wantLocales,
119
147
  setCurrency,
120
148
  setUnit,
121
149
  reconcile,
@@ -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
+ }
@@ -9,7 +9,8 @@ Field pages describe their data and usage with a physical interface reference.
9
9
  Fields used by supplied components also link to their previews and source files.
10
10
  Configuration and shared counters have dedicated data references.
11
11
 
12
- The store chooses the first available language and its default currency and
12
+ The store chooses the available language closest to the browser's languages
13
+ (the first available one when none is close) and its default currency and
13
14
  units. `MainLanguageSwitcher` updates `store.localeCode` and replaces the current
14
15
  currency and unit selections with that language's defaults;
15
16
  `MainCurrencySwitcher` updates `store.currencyId`. `UnitSwitcher` uses