next-intl 3.11.1 → 3.11.3

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.
@@ -36,6 +36,19 @@ function getAcceptLanguageLocale(requestHeaders, locales, defaultLocale) {
36
36
  }
37
37
  return locale;
38
38
  }
39
+ function getLocaleFromPrefix(pathname, locales) {
40
+ const pathLocaleCandidate = utils.getFirstPathnameSegment(pathname);
41
+ return utils.findCaseInsensitiveLocale(pathLocaleCandidate, locales);
42
+ }
43
+ function getLocaleFromCookie(requestCookies, locales) {
44
+ if (requestCookies.has(constants.COOKIE_LOCALE_NAME)) {
45
+ var _requestCookies$get;
46
+ const value = (_requestCookies$get = requestCookies.get(constants.COOKIE_LOCALE_NAME)) === null || _requestCookies$get === void 0 ? void 0 : _requestCookies$get.value;
47
+ if (value && locales.includes(value)) {
48
+ return value;
49
+ }
50
+ }
51
+ }
39
52
  function resolveLocaleFromPrefix(_ref, requestHeaders, requestCookies, pathname) {
40
53
  let {
41
54
  defaultLocale,
@@ -46,22 +59,12 @@ function resolveLocaleFromPrefix(_ref, requestHeaders, requestCookies, pathname)
46
59
 
47
60
  // Prio 1: Use route prefix
48
61
  if (pathname) {
49
- const pathLocaleCandidate = utils.getFirstPathnameSegment(pathname);
50
- const matchedLocale = utils.findCaseInsensitiveLocale(pathLocaleCandidate, locales);
51
- if (matchedLocale) {
52
- locale = matchedLocale;
53
- }
62
+ locale = getLocaleFromPrefix(pathname, locales);
54
63
  }
55
64
 
56
65
  // Prio 2: Use existing cookie
57
66
  if (!locale && localeDetection && requestCookies) {
58
- if (requestCookies.has(constants.COOKIE_LOCALE_NAME)) {
59
- var _requestCookies$get;
60
- const value = (_requestCookies$get = requestCookies.get(constants.COOKIE_LOCALE_NAME)) === null || _requestCookies$get === void 0 ? void 0 : _requestCookies$get.value;
61
- if (value && locales.includes(value)) {
62
- locale = value;
63
- }
64
- }
67
+ locale = getLocaleFromCookie(requestCookies, locales);
65
68
  }
66
69
 
67
70
  // Prio 3: Use the `accept-language` header
@@ -76,26 +79,56 @@ function resolveLocaleFromPrefix(_ref, requestHeaders, requestCookies, pathname)
76
79
  return locale;
77
80
  }
78
81
  function resolveLocaleFromDomain(config, requestHeaders, requestCookies, pathname) {
79
- const {
80
- domains
81
- } = config;
82
- const localeFromPrefixStrategy = resolveLocaleFromPrefix(config, requestHeaders, requestCookies, pathname);
82
+ const domains = config.domains;
83
+ const domain = findDomainFromHost(requestHeaders, domains);
84
+ if (!domain) {
85
+ return {
86
+ locale: resolveLocaleFromPrefix(config, requestHeaders, requestCookies, pathname)
87
+ };
88
+ }
89
+ let locale;
83
90
 
84
- // Prio 1: Use a domain
85
- if (domains) {
86
- const domain = findDomainFromHost(requestHeaders, domains);
87
- const hasLocalePrefix = pathname && pathname.startsWith("/".concat(localeFromPrefixStrategy));
88
- if (domain) {
89
- return {
90
- locale: utils.isLocaleSupportedOnDomain(localeFromPrefixStrategy, domain) || hasLocalePrefix ? localeFromPrefixStrategy : domain.defaultLocale,
91
- domain
92
- };
91
+ // Prio 1: Use route prefix
92
+ if (pathname) {
93
+ const prefixLocale = getLocaleFromPrefix(pathname, config.locales);
94
+ if (prefixLocale) {
95
+ if (utils.isLocaleSupportedOnDomain(prefixLocale, domain)) {
96
+ locale = prefixLocale;
97
+ } else {
98
+ // Causes a redirect to a domain that supports the locale
99
+ return {
100
+ locale: prefixLocale,
101
+ domain
102
+ };
103
+ }
93
104
  }
94
105
  }
95
106
 
96
- // Prio 2: Use prefix strategy
107
+ // Prio 2: Use existing cookie
108
+ if (!locale && config.localeDetection && requestCookies) {
109
+ const cookieLocale = getLocaleFromCookie(requestCookies, config.locales);
110
+ if (cookieLocale) {
111
+ if (utils.isLocaleSupportedOnDomain(cookieLocale, domain)) {
112
+ locale = cookieLocale;
113
+ }
114
+ }
115
+ }
116
+
117
+ // Prio 3: Use the `accept-language` header
118
+ if (!locale && config.localeDetection && requestHeaders) {
119
+ const headerLocale = getAcceptLanguageLocale(requestHeaders, domain.locales || config.locales, domain.defaultLocale);
120
+ if (headerLocale) {
121
+ locale = headerLocale;
122
+ }
123
+ }
124
+
125
+ // Prio 4: Use default locale
126
+ if (!locale) {
127
+ locale = domain.defaultLocale;
128
+ }
97
129
  return {
98
- locale: localeFromPrefixStrategy
130
+ locale,
131
+ domain
99
132
  };
100
133
  }
101
134
  function resolveLocale(config, requestHeaders, requestCookies, pathname) {
@@ -109,3 +142,4 @@ function resolveLocale(config, requestHeaders, requestCookies, pathname) {
109
142
  }
110
143
 
111
144
  exports.default = resolveLocale;
145
+ exports.getAcceptLanguageLocale = getAcceptLanguageLocale;
@@ -104,7 +104,12 @@ function getRoute(_ref3) {
104
104
  return template;
105
105
  }
106
106
  function getBasePath(pathname) {
107
- return window.location.pathname.replace(pathname, '');
107
+ let windowPathname = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.pathname;
108
+ if (pathname === '/') {
109
+ return windowPathname;
110
+ } else {
111
+ return windowPathname.replace(pathname, '');
112
+ }
108
113
  }
109
114
 
110
115
  exports.compileLocalizedPathname = compileLocalizedPathname;
@@ -1 +1 @@
1
- import{match as t}from"@formatjs/intl-localematcher";import o from"negotiator";import{COOKIE_LOCALE_NAME as e}from"../shared/constants.js";import{getFirstPathnameSegment as n,findCaseInsensitiveLocale as a,isLocaleSupportedOnDomain as l,getHost as c}from"./utils.js";function r(l,c,r,i){let s,{defaultLocale:u,localeDetection:f,locales:d}=l;if(i){const t=n(i),o=a(t,d);o&&(s=o)}if(!s&&f&&r&&r.has(e)){var m;const t=null===(m=r.get(e))||void 0===m?void 0:m.value;t&&d.includes(t)&&(s=t)}return!s&&f&&c&&(s=function(e,n,a){let l;const c=new o({headers:{"accept-language":e.get("accept-language")||void 0}}).languages();try{l=t(c,n,a)}catch(t){}return l}(c,d,u)),s||(s=u),s}function i(t,o,e,n){const{domains:a}=t,i=r(t,o,e,n);if(a){const t=function(t,o){var e;let n=c(t);if(n=null===(e=n)||void 0===e?void 0:e.replace(/:\d+$/,""),n&&o)return o.find((t=>t.domain===n))}(o,a),e=n&&n.startsWith("/".concat(i));if(t)return{locale:l(i,t)||e?i:t.defaultLocale,domain:t}}return{locale:i}}function s(t,o,e,n){return t.domains?i(t,o,e,n):{locale:r(t,o,e,n)}}export{s as default};
1
+ import{match as e}from"@formatjs/intl-localematcher";import o from"negotiator";import{COOKIE_LOCALE_NAME as t}from"../shared/constants.js";import{isLocaleSupportedOnDomain as n,getHost as l,getFirstPathnameSegment as a,findCaseInsensitiveLocale as c}from"./utils.js";function i(t,n,l){let a;const c=new o({headers:{"accept-language":t.get("accept-language")||void 0}}).languages();try{a=e(c,n,l)}catch(e){}return a}function r(e,o){const t=a(e);return c(t,o)}function u(e,o){if(e.has(t)){var n;const l=null===(n=e.get(t))||void 0===n?void 0:n.value;if(l&&o.includes(l))return l}}function f(e,o,t,n){let l,{defaultLocale:a,localeDetection:c,locales:f}=e;return n&&(l=r(n,f)),!l&&c&&t&&(l=u(t,f)),!l&&c&&o&&(l=i(o,f,a)),l||(l=a),l}function s(e,o,t,a){const c=function(e,o){var t;let n=l(e);if(n=null===(t=n)||void 0===t?void 0:t.replace(/:\d+$/,""),n&&o)return o.find((e=>e.domain===n))}(o,e.domains);if(!c)return{locale:f(e,o,t,a)};let s;if(a){const o=r(a,e.locales);if(o){if(!n(o,c))return{locale:o,domain:c};s=o}}if(!s&&e.localeDetection&&t){const o=u(t,e.locales);o&&n(o,c)&&(s=o)}if(!s&&e.localeDetection&&o){const t=i(o,c.locales||e.locales,c.defaultLocale);t&&(s=t)}return s||(s=c.defaultLocale),{locale:s,domain:c}}function d(e,o,t,n){return e.domains?s(e,o,t,n):{locale:f(e,o,t,n)}}export{d as default,i as getAcceptLanguageLocale};
@@ -1 +1 @@
1
- import{unlocalizePathname as t,matchesPathname as n}from"../../shared/utils.js";function e(t){return"string"==typeof t?{pathname:t}:t}function r(t){function n(t){return String(t)}const e=new URLSearchParams;for(const[r,a]of Object.entries(t))Array.isArray(a)?a.forEach((t=>{e.append(r,n(t))})):e.set(r,n(a));return"?"+e.toString()}function a(t){let{pathname:n,locale:e,params:a,pathnames:o,query:c}=t;function i(t){let n=o[t];return n||(n=t),n}function s(t){const n="string"==typeof t?t:t[e];let o=n;if(a&&Object.entries(a).forEach((t=>{let[n,e]=t;o=Array.isArray(e)?o.replace(new RegExp("(\\[)?\\[...".concat(n,"\\](\\])?"),"g"),e.map((t=>String(t))).join("/")):o.replace("[".concat(n,"]"),String(e))})),o.includes("["))throw new Error("Insufficient params provided for localized pathname.\nTemplate: ".concat(n,"\nParams: ").concat(JSON.stringify(a)));return c&&(o+=r(c)),o}if("string"==typeof n){return s(i(n))}{const{pathname:t,...e}=n;return{...e,pathname:s(i(t))}}}function o(e){var r;let{locale:a,pathname:o,pathnames:c}=e;const i=t(decodeURI(o),a);let s=null===(r=Object.entries(c).find((t=>{let[,e]=t;const r="string"!=typeof e?e[a]:e;return n(r,i)})))||void 0===r?void 0:r[0];return s||(s=o),s}function c(t){return window.location.pathname.replace(t,"")}export{a as compileLocalizedPathname,c as getBasePath,o as getRoute,e as normalizeNameOrNameWithParams,r as serializeSearchParams};
1
+ import{unlocalizePathname as t,matchesPathname as n}from"../../shared/utils.js";function e(t){return"string"==typeof t?{pathname:t}:t}function r(t){function n(t){return String(t)}const e=new URLSearchParams;for(const[r,a]of Object.entries(t))Array.isArray(a)?a.forEach((t=>{e.append(r,n(t))})):e.set(r,n(a));return"?"+e.toString()}function a(t){let{pathname:n,locale:e,params:a,pathnames:o,query:i}=t;function c(t){let n=o[t];return n||(n=t),n}function s(t){const n="string"==typeof t?t:t[e];let o=n;if(a&&Object.entries(a).forEach((t=>{let[n,e]=t;o=Array.isArray(e)?o.replace(new RegExp("(\\[)?\\[...".concat(n,"\\](\\])?"),"g"),e.map((t=>String(t))).join("/")):o.replace("[".concat(n,"]"),String(e))})),o.includes("["))throw new Error("Insufficient params provided for localized pathname.\nTemplate: ".concat(n,"\nParams: ").concat(JSON.stringify(a)));return i&&(o+=r(i)),o}if("string"==typeof n){return s(c(n))}{const{pathname:t,...e}=n;return{...e,pathname:s(c(t))}}}function o(e){var r;let{locale:a,pathname:o,pathnames:i}=e;const c=t(decodeURI(o),a);let s=null===(r=Object.entries(i).find((t=>{let[,e]=t;const r="string"!=typeof e?e[a]:e;return n(r,c)})))||void 0===r?void 0:r[0];return s||(s=o),s}function i(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:window.location.pathname;return"/"===t?n:n.replace(t,"")}export{a as compileLocalizedPathname,i as getBasePath,o as getRoute,e as normalizeNameOrNameWithParams,r as serializeSearchParams};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@formatjs/intl-localematcher"),t=require("negotiator"),n=require("../shared/constants.js"),a=require("./utils.js");function o(e){return e&&e.__esModule?e:{default:e}}var r=o(t);function i(t,o,i,l){let c,{defaultLocale:u,localeDetection:s,locales:d}=t;if(l){const e=a.getFirstPathnameSegment(l),t=a.findCaseInsensitiveLocale(e,d);t&&(c=t)}if(!c&&s&&i&&i.has(n.COOKIE_LOCALE_NAME)){var f;const e=null===(f=i.get(n.COOKIE_LOCALE_NAME))||void 0===f?void 0:f.value;e&&d.includes(e)&&(c=e)}return!c&&s&&o&&(c=function(t,n,a){let o;const i=new r.default({headers:{"accept-language":t.get("accept-language")||void 0}}).languages();try{o=e.match(i,n,a)}catch(e){}return o}(o,d,u)),c||(c=u),c}function l(e,t,n,o){const{domains:r}=e,l=i(e,t,n,o);if(r){const e=function(e,t){var n;let o=a.getHost(e);if(o=null===(n=o)||void 0===n?void 0:n.replace(/:\d+$/,""),o&&t)return t.find((e=>e.domain===o))}(t,r),n=o&&o.startsWith("/".concat(l));if(e)return{locale:a.isLocaleSupportedOnDomain(l,e)||n?l:e.defaultLocale,domain:e}}return{locale:l}}exports.default=function(e,t,n,a){return e.domains?l(e,t,n,a):{locale:i(e,t,n,a)}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@formatjs/intl-localematcher"),t=require("negotiator"),n=require("../shared/constants.js"),o=require("./utils.js");function a(e){return e&&e.__esModule?e:{default:e}}var l=a(t);function c(t,n,o){let a;const c=new l.default({headers:{"accept-language":t.get("accept-language")||void 0}}).languages();try{a=e.match(c,n,o)}catch(e){}return a}function i(e,t){const n=o.getFirstPathnameSegment(e);return o.findCaseInsensitiveLocale(n,t)}function r(e,t){if(e.has(n.COOKIE_LOCALE_NAME)){var o;const a=null===(o=e.get(n.COOKIE_LOCALE_NAME))||void 0===o?void 0:o.value;if(a&&t.includes(a))return a}}function u(e,t,n,o){let a,{defaultLocale:l,localeDetection:u,locales:s}=e;return o&&(a=i(o,s)),!a&&u&&n&&(a=r(n,s)),!a&&u&&t&&(a=c(t,s,l)),a||(a=l),a}function s(e,t,n,a){const l=function(e,t){var n;let a=o.getHost(e);if(a=null===(n=a)||void 0===n?void 0:n.replace(/:\d+$/,""),a&&t)return t.find((e=>e.domain===a))}(t,e.domains);if(!l)return{locale:u(e,t,n,a)};let s;if(a){const t=i(a,e.locales);if(t){if(!o.isLocaleSupportedOnDomain(t,l))return{locale:t,domain:l};s=t}}if(!s&&e.localeDetection&&n){const t=r(n,e.locales);t&&o.isLocaleSupportedOnDomain(t,l)&&(s=t)}if(!s&&e.localeDetection&&t){const n=c(t,l.locales||e.locales,l.defaultLocale);n&&(s=n)}return s||(s=l.defaultLocale),{locale:s,domain:l}}exports.default=function(e,t,n,o){return e.domains?s(e,t,n,o):{locale:u(e,t,n,o)}},exports.getAcceptLanguageLocale=c;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../shared/utils.js");function t(e){function t(e){return String(e)}const r=new URLSearchParams;for(const[n,a]of Object.entries(e))Array.isArray(a)?a.forEach((e=>{r.append(n,t(e))})):r.set(n,t(a));return"?"+r.toString()}exports.compileLocalizedPathname=function(e){let{pathname:r,locale:n,params:a,pathnames:o,query:i}=e;function c(e){let t=o[e];return t||(t=e),t}function s(e){let r="string"==typeof e?e:e[n];return a&&Object.entries(a).forEach((e=>{let[t,n]=e;r=Array.isArray(n)?r.replace(new RegExp("(\\[)?\\[...".concat(t,"\\](\\])?"),"g"),n.map((e=>String(e))).join("/")):r.replace("[".concat(t,"]"),String(n))})),i&&(r+=t(i)),r}if("string"==typeof r){return s(c(r))}{const{pathname:e,...t}=r;return{...t,pathname:s(c(e))}}},exports.getBasePath=function(e){return window.location.pathname.replace(e,"")},exports.getRoute=function(t){var r;let{locale:n,pathname:a,pathnames:o}=t;const i=e.unlocalizePathname(decodeURI(a),n);let c=null===(r=Object.entries(o).find((t=>{let[,r]=t;const a="string"!=typeof r?r[n]:r;return e.matchesPathname(a,i)})))||void 0===r?void 0:r[0];return c||(c=a),c},exports.normalizeNameOrNameWithParams=function(e){return"string"==typeof e?{pathname:e}:e},exports.serializeSearchParams=t;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../shared/utils.js");function t(e){function t(e){return String(e)}const r=new URLSearchParams;for(const[n,a]of Object.entries(e))Array.isArray(a)?a.forEach((e=>{r.append(n,t(e))})):r.set(n,t(a));return"?"+r.toString()}exports.compileLocalizedPathname=function(e){let{pathname:r,locale:n,params:a,pathnames:o,query:i}=e;function c(e){let t=o[e];return t||(t=e),t}function s(e){let r="string"==typeof e?e:e[n];return a&&Object.entries(a).forEach((e=>{let[t,n]=e;r=Array.isArray(n)?r.replace(new RegExp("(\\[)?\\[...".concat(t,"\\](\\])?"),"g"),n.map((e=>String(e))).join("/")):r.replace("[".concat(t,"]"),String(n))})),i&&(r+=t(i)),r}if("string"==typeof r){return s(c(r))}{const{pathname:e,...t}=r;return{...t,pathname:s(c(e))}}},exports.getBasePath=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:window.location.pathname;return"/"===e?t:t.replace(e,"")},exports.getRoute=function(t){var r;let{locale:n,pathname:a,pathnames:o}=t;const i=e.unlocalizePathname(decodeURI(a),n);let c=null===(r=Object.entries(o).find((t=>{let[,r]=t;const a="string"!=typeof r?r[n]:r;return e.matchesPathname(a,i)})))||void 0===r?void 0:r[0];return c||(c=a),c},exports.normalizeNameOrNameWithParams=function(e){return"string"==typeof e?{pathname:e}:e},exports.serializeSearchParams=t;
@@ -1,6 +1,7 @@
1
1
  import { RequestCookies } from 'next/dist/server/web/spec-extension/cookies';
2
2
  import { AllLocales } from '../shared/types';
3
3
  import { DomainConfig, MiddlewareConfigWithDefaults } from './NextIntlMiddlewareConfig';
4
+ export declare function getAcceptLanguageLocale<Locales extends AllLocales>(requestHeaders: Headers, locales: Locales, defaultLocale: string): string | undefined;
4
5
  export default function resolveLocale<Locales extends AllLocales>(config: MiddlewareConfigWithDefaults<Locales>, requestHeaders: Headers, requestCookies: RequestCookies, pathname: string): {
5
6
  locale: Locales[number];
6
7
  domain?: DomainConfig<Locales>;
@@ -48,5 +48,5 @@ export declare function getRoute<Locales extends AllLocales>({ locale, pathname,
48
48
  pathname: string;
49
49
  pathnames: Pathnames<Locales>;
50
50
  }): string;
51
- export declare function getBasePath(pathname: string): string;
51
+ export declare function getBasePath(pathname: string, windowPathname?: string): string;
52
52
  export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intl",
3
- "version": "3.11.1",
3
+ "version": "3.11.3",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.work>",
6
6
  "funding": [
@@ -82,7 +82,7 @@
82
82
  "dependencies": {
83
83
  "@formatjs/intl-localematcher": "^0.2.32",
84
84
  "negotiator": "^0.6.3",
85
- "use-intl": "^3.11.1"
85
+ "use-intl": "^3.11.3"
86
86
  },
87
87
  "peerDependencies": {
88
88
  "next": "^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0",
@@ -122,11 +122,11 @@
122
122
  },
123
123
  {
124
124
  "path": "dist/production/navigation.react-client.js",
125
- "limit": "2.94 KB"
125
+ "limit": "2.96 KB"
126
126
  },
127
127
  {
128
128
  "path": "dist/production/navigation.react-server.js",
129
- "limit": "3.03 KB"
129
+ "limit": "3.06 KB"
130
130
  },
131
131
  {
132
132
  "path": "dist/production/server.react-client.js",
@@ -138,8 +138,8 @@
138
138
  },
139
139
  {
140
140
  "path": "dist/production/middleware.js",
141
- "limit": "5.95 KB"
141
+ "limit": "6 KB"
142
142
  }
143
143
  ],
144
- "gitHead": "78f88d1493eb796498b7fb2c986536ac49cffda0"
144
+ "gitHead": "c4eac9cae1b10254486ab71cbfa35fb1f39e1b05"
145
145
  }