UNPKG

6.21 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const SUB_DIR = /^(\/(\w\w(_\w\w)?))(?:\/|$)/;
4const SUB_DOMAIN_PIPE_REGEX = /(?:^|_)(\w\w(?:_\w\w)?)_(?:b\-[mt]\-)?[0-9a-f]{24,25}_pipe/i;
5const SUB_DOMAIN_REGEX = /^(?:www\.)?(\w\w(?:_\w\w)?)\./i;
6function getLocaleFromFolder(folderLocale, locales) {
7 var index = locales.indexOf(folderLocale);
8 if (index > -1)
9 return locales[index];
10 folderLocale = folderLocale.substr(0, 2);
11 for (var i = 0; i < locales.length; i++) {
12 if (locales[i].substr(0, 2) == folderLocale)
13 return locales[i];
14 }
15 return '';
16}
17function getLocaleByURL(parsedUrl, locale_detection, localeConfigs, cookieLocale, siteDefaultLocale, detectedLocale, isProxy, explicitLocale, subDirBase, folders, locales) {
18 switch (locale_detection) {
19 case 'querystring':
20 if (parsedUrl.query && typeof (parsedUrl.query) == 'object')
21 return parsedUrl.query.locale;
22 var matches = /locale=([^&]+)/.exec(parsedUrl.query || '');
23 if (matches && matches.length == 2 && matches[1])
24 return matches[1];
25 return cookieLocale || detectedLocale || siteDefaultLocale;
26 case "subdomain":
27 var regex = isProxy ? SUB_DOMAIN_PIPE_REGEX : SUB_DOMAIN_REGEX;
28 var matches = regex.exec(parsedUrl.hostname);
29 if (matches && matches.length > 1 && matches[1])
30 return matches[1];
31 return siteDefaultLocale;
32 case "subdir":
33 var pathname = parsedUrl.pathname;
34 if (subDirBase)
35 pathname = pathname.replace(subDirBase, '');
36 var match = SUB_DIR.exec(pathname);
37 if (match) {
38 if (folders && locales && folders[match[2]])
39 return getLocaleFromFolder(folders[match[2]], locales) || siteDefaultLocale;
40 return match[2];
41 }
42 if (explicitLocale)
43 return explicitLocale;
44 if (cookieLocale)
45 return cookieLocale;
46 return detectedLocale || siteDefaultLocale;
47 case "tld":
48 var matches = /\.(\w\w)$/.exec(parsedUrl.hostname);
49 if (matches && matches.length > 1 && matches[1])
50 return matches[1];
51 return null;
52 case "custom":
53 var parseDomainRule = function (str) {
54 return RegExp((str + '').replace(/([.?+^$[\]\\(){}|-])/g, "\\$1").replace(/\*/g, '.*'), 'i');
55 };
56 for (var locale in localeConfigs) {
57 var urlPattern = localeConfigs[locale];
58 if (urlPattern && parseDomainRule(urlPattern).test(parsedUrl.href))
59 return locale;
60 }
61 return explicitLocale || siteDefaultLocale;
62 case "hash":
63 var matches = /^#!locale=(\w\w(?:_\w\w)?)$/.exec(parsedUrl.hash || '');
64 if (matches && matches.length > 1 && matches[1])
65 return matches[1];
66 return cookieLocale || detectedLocale || siteDefaultLocale;
67 default:
68 return cookieLocale;
69 }
70}
71exports.getLocaleByURL = getLocaleByURL;
72function getLink(locale, parsed, meta, options) {
73 options = options || {};
74 let protocol = parsed.protocol || '';
75 let hostname = parsed.hostname;
76 let pathname = parsed.pathname || '/';
77 let search = parsed.search || '';
78 let hash = parsed.hash || '';
79 let returnFull = options.returnFull;
80 let localeDetection = meta.localeDetection;
81 let original = meta.original;
82 if (options.subDir)
83 localeDetection = 'subdir';
84 if (localeDetection == 'custom' && !(meta.customUrls && meta.customUrls[locale]))
85 localeDetection = 'querystring';
86 switch (localeDetection) {
87 case 'custom':
88 let customUrl = meta.customUrls[locale];
89 let confDomain = customUrl.indexOf('/') > -1 ? customUrl.substr(0, customUrl.indexOf('/')) : customUrl;
90 return protocol + '//' + confDomain + pathname + search + hash;
91 case 'querystring':
92 if (/[?&]locale=([^&]+)/.test(search))
93 search = search.replace(/([?&]locale=)([^&]+)/, '$1' + locale);
94 else {
95 if (search)
96 search = search + '&locale=' + locale;
97 else
98 search = '?locale=' + locale;
99 }
100 if (returnFull)
101 return protocol + '//' + hostname + pathname + search + hash;
102 return pathname + search + hash;
103 case 'subdir':
104 if (options.subDirBase)
105 pathname = pathname.replace(options.subDirBase, '');
106 let match = SUB_DIR.exec(pathname);
107 if (match) {
108 pathname = pathname.substr(match[1].length);
109 if (pathname.length == 0)
110 pathname = '/';
111 }
112 let prefix = '';
113 if (options.folders)
114 prefix = '/' + getFolder(locale, options.folders);
115 else if (locale != original)
116 prefix = '/' + locale;
117 if (options.subDirBase && (!options.subDirOptional || locale != original))
118 prefix = options.subDirBase + prefix;
119 if (returnFull)
120 return protocol + '//' + hostname + prefix + pathname + search + hash;
121 return prefix + pathname + search + hash;
122 case 'hash':
123 if (returnFull)
124 return protocol + '//' + hostname + pathname + search + '#locale=' + locale;
125 return '#locale_' + locale;
126 }
127 return `javascript:bablic.setLanguage("${locale}");`;
128}
129exports.getLink = getLink;
130function getFolder(locale, folders) {
131 for (let folder in folders) {
132 if (folders[folder] == locale)
133 return folder;
134 }
135 locale = locale.substr(0, 2);
136 for (let folder in folders) {
137 if (folders[folder].substr(0, 2) == locale)
138 return folder;
139 }
140 return locale;
141}