Build and Publish Site / docker (push) Successful in 23s
ABER: Die Applikation funktioniert nur lokal. Die deployte Version geht noch nicht.
71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
///<reference path="../index.d.ts"/>
|
|
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.osLocale = void 0;
|
|
/*!
|
|
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
Copyright (C) 2020 jeffy-g <hirotom1107@gmail.com>
|
|
Released under the MIT license
|
|
https://opensource.org/licenses/mit-license.php
|
|
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
*/
|
|
// @ts-check
|
|
const lib = require("./lib");
|
|
/** @type {NsOsLocale.LocaleDetector} */
|
|
let detector;
|
|
{
|
|
const { detectNativeLocale, getEnvLocale, purgeExtraToken } = lib;
|
|
let cacheLocal = "";
|
|
/**
|
|
* @param {string} localeToken
|
|
* @param {boolean} cache
|
|
* @returns {string}
|
|
*/
|
|
const withCache = (localeToken, cache) => {
|
|
localeToken = localeToken.replace(/_/g, "-");
|
|
cacheLocal = cache ? localeToken : "";
|
|
return localeToken;
|
|
};
|
|
/**
|
|
* @template {true | void} IsAsync
|
|
* @template {SelectIf<IsAsync, Promise<string>, string>} R
|
|
* @param {NsOsLocale.LocaleDetectorOptions=} options
|
|
* @param {IsAsync=} isAsync
|
|
* @returns {R}
|
|
*/
|
|
const detectorBase = (options, isAsync) => {
|
|
const { cache = true, spawn = true } = options || {};
|
|
if (cache && cacheLocal.length) {
|
|
return /** @type {R} */ (
|
|
isAsync ? Promise.resolve(cacheLocal) : cacheLocal
|
|
);
|
|
}
|
|
const envLocale = getEnvLocale();
|
|
const useEnv = !!(envLocale || !spawn);
|
|
if (isAsync) {
|
|
const p = useEnv
|
|
? Promise.resolve(purgeExtraToken(envLocale))
|
|
: detectNativeLocale(true);
|
|
return /** @type {R} */ (p.then((loc) => withCache(loc, cache)));
|
|
}
|
|
const s = useEnv ? purgeExtraToken(envLocale) : detectNativeLocale();
|
|
return /** @type {R} */ (withCache(s, cache));
|
|
};
|
|
detector = /** @type {NsOsLocale.LocaleDetector} */ (
|
|
(opt) => detectorBase(opt, true)
|
|
);
|
|
detector.sync = (opt) => detectorBase(opt);
|
|
Object.defineProperties(detector, {
|
|
/** @internal test use */
|
|
purge: {
|
|
value: () => (cacheLocal = ""),
|
|
enumerable: false,
|
|
},
|
|
version: {
|
|
value: "v1.1.3",
|
|
enumerable: true,
|
|
},
|
|
});
|
|
}
|
|
exports.osLocale = detector;
|