@aboutbits/internationalization
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -0,7 +1,43 @@ | ||
/// <reference types="node" /> | ||
import { IncomingMessage } from 'http'; | ||
declare class Internationalization<T extends string> { | ||
supportedLanguages: T[]; | ||
private readonly cookieName; | ||
supportedLanguages: Array<T>; | ||
fallbackLanguage: T; | ||
constructor(supportedLanguages: T[], fallbackLanguage: T); | ||
/** | ||
* ClientInternationalization utility package for working with languages | ||
* | ||
* @param supportedLanguages { Array<string> } - List of all supported languages | ||
* @param fallbackLanguage { string } - Fallback language if no language matches | ||
* @param cookieName { string } - Optional: Set a preferred cookie name | ||
*/ | ||
constructor(supportedLanguages: Array<T>, fallbackLanguage: T, cookieName?: string); | ||
/** | ||
* Detect the browser language of the client | ||
* If no language was found or if none of the | ||
* supported language matches a fallback language will be returned | ||
* | ||
* @return {string} - Detected language | ||
*/ | ||
detectBrowserLanguage(): T; | ||
/** | ||
* Save a language to a cookie | ||
* If the set language isn't in the list of supported languages | ||
* the fallback language will be returned | ||
* | ||
* @param language { string } - Language to set | ||
*/ | ||
setLanguage(language: string): void; | ||
/** | ||
* Get the current language | ||
* This method will first check if a language cookie is present. | ||
* If no cookie was found the method will get the language form the browser | ||
* | ||
* For handling cookies on the server side pass the request object | ||
* | ||
* @param request { IncomingMessage } - Optional: Request object for accessing cookies over request headers | ||
* @return { string } - Found language or fallback language | ||
*/ | ||
detectLanguage(request?: IncomingMessage): T; | ||
} | ||
export { Internationalization as default }; | ||
export default Internationalization; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = void 0; | ||
var utilities_1 = require("./utilities"); | ||
var Internationalization = /** @class */ (function () { | ||
function Internationalization(supportedLanguages, fallbackLanguage) { | ||
/** | ||
* ClientInternationalization utility package for working with languages | ||
* | ||
* @param supportedLanguages { Array<string> } - List of all supported languages | ||
* @param fallbackLanguage { string } - Fallback language if no language matches | ||
* @param cookieName { string } - Optional: Set a preferred cookie name | ||
*/ | ||
function Internationalization(supportedLanguages, fallbackLanguage, cookieName) { | ||
this.supportedLanguages = supportedLanguages; | ||
this.fallbackLanguage = fallbackLanguage; | ||
this.cookieName = typeof cookieName === 'string' ? cookieName : 'language'; | ||
} | ||
/** | ||
* Detect the browser language of the client | ||
* If no language was found or if none of the | ||
* supported language matches a fallback language will be returned | ||
* | ||
* @return {string} - Detected language | ||
*/ | ||
Internationalization.prototype.detectBrowserLanguage = function () { | ||
var browserLanguage = (navigator.languages && navigator.languages[0]) || navigator.language; | ||
return (this.supportedLanguages.find(function (lang) { return lang === browserLanguage; }) || | ||
this.fallbackLanguage); | ||
// Check if the DOM is presented. | ||
// If the DOM isn't accessible the website may be rendered on a server | ||
if (utilities_1.canUseDOM()) { | ||
// Get browser language | ||
var browserLanguage_1 = (navigator.languages && navigator.languages[0]) || navigator.language; | ||
return (this.supportedLanguages.find(function (lang) { return lang === browserLanguage_1; }) || | ||
this.fallbackLanguage); | ||
} | ||
// Return fallback language if no DOM was found | ||
return this.fallbackLanguage; | ||
}; | ||
/** | ||
* Save a language to a cookie | ||
* If the set language isn't in the list of supported languages | ||
* the fallback language will be returned | ||
* | ||
* @param language { string } - Language to set | ||
*/ | ||
Internationalization.prototype.setLanguage = function (language) { | ||
if (utilities_1.canUseDOM()) { | ||
var cookieLanguage = this.supportedLanguages.find(function (lang) { return lang === language; }) || | ||
this.fallbackLanguage; | ||
utilities_1.setCookie(this.cookieName, cookieLanguage); | ||
} | ||
}; | ||
/** | ||
* Get the current language | ||
* This method will first check if a language cookie is present. | ||
* If no cookie was found the method will get the language form the browser | ||
* | ||
* For handling cookies on the server side pass the request object | ||
* | ||
* @param request { IncomingMessage } - Optional: Request object for accessing cookies over request headers | ||
* @return { string } - Found language or fallback language | ||
*/ | ||
Internationalization.prototype.detectLanguage = function (request) { | ||
var _this = this; | ||
// Check if request is not undefined | ||
if (request) { | ||
// Try to get language form cookie | ||
var cookieFromRequest = utilities_1.getCookieFromRequest(this.cookieName, request); | ||
if (cookieFromRequest) { | ||
return cookieFromRequest; | ||
} | ||
// Get accept language from request header | ||
var acceptLanguage = request.headers['accept-language']; | ||
if (acceptLanguage) { | ||
// Extract the locale string and the priority from header string | ||
var requestedLocales = acceptLanguage.split(',').map(function (part) { | ||
var _a = part.trim().split(';q='), locale = _a[0], priority = _a[1]; | ||
return { locale: locale, priority: parseInt(priority) }; | ||
}); | ||
// Get the potential locale from requested locales array | ||
var potentialLocale = requestedLocales | ||
.sort(function (a, b) { return b.priority - a.priority; }) | ||
.find(function (_a) { | ||
var locale = _a.locale; | ||
return locale !== '*' && | ||
_this.supportedLanguages.some(function (lang) { return locale === lang; }); | ||
}); | ||
// Check if the potential locale is not undefined else return the fallback language | ||
return (potentialLocale | ||
? potentialLocale.locale | ||
: this.fallbackLanguage); | ||
} | ||
} | ||
// Check for DOM | ||
if (utilities_1.canUseDOM()) { | ||
// Try to get language form cookie | ||
var language_1 = utilities_1.getCookieFromDocument(this.cookieName); | ||
// If cookie is undefined get the language form the browser | ||
if (language_1 == undefined) { | ||
return this.detectBrowserLanguage(); | ||
} | ||
// Return found language or fallback language | ||
return (this.supportedLanguages.find(function (lang) { return lang === language_1; }) || | ||
this.fallbackLanguage); | ||
} | ||
return this.fallbackLanguage; | ||
}; | ||
return Internationalization; | ||
}()); | ||
exports.default = Internationalization; |
{ | ||
"name": "@aboutbits/internationalization", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Internationalization", | ||
@@ -42,2 +42,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/node": "^14.11.5", | ||
"@types/jest": "^26.0.9", | ||
@@ -44,0 +45,0 @@ "@typescript-eslint/eslint-plugin": "^3.8.0", |
@@ -6,3 +6,4 @@ Internationalization | ||
This package includes utilities for working with different languages in the browser. | ||
This package includes utilities for working with different languages in the browser. | ||
This package works for client side rendered applications as well as for server side rendered applications. | ||
@@ -26,3 +27,3 @@ ## Table of content | ||
```js | ||
import Internationalization from '@aboutbits/internationalization' | ||
import { Internationalization } from '@aboutbits/internationalization' | ||
@@ -34,4 +35,15 @@ let supportedLanguages = ['en', 'de', 'it'] | ||
let language = i18n.detectBrowserLanguage() | ||
// Fetches the language only from the user's browser | ||
let browserLanguage = i18n.detectBrowserLanguage() | ||
// First check the cookies to see if a language is set. If not it will look in the browser. | ||
// You can also pass a requiest obejct with which the language can be recognized already during the server side rendering | ||
let language = i18n.detectLanguage() | ||
// Sets a cookie with the specified language. | ||
// There exists also a static version of this method. | ||
// Ex. Internationalization.setLanguage("de") | ||
i18n.setLanguage("de") | ||
console.log(browserLanguage) | ||
console.log(language) | ||
@@ -38,0 +50,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14275
7
231
76
13
1