|
| 1 | +import { IncomingMessage } from 'http' |
| 2 | +import { |
| 3 | + getCookieFromDocument, |
| 4 | + canUseDOM, |
| 5 | + setCookie, |
| 6 | + getCookieFromRequest, |
| 7 | +} from './utilities' |
| 8 | + |
1 | 9 | class Internationalization<T extends string> { |
2 | | - supportedLanguages: T[] |
| 10 | + private readonly cookieName: string |
| 11 | + |
| 12 | + supportedLanguages: Array<T> |
3 | 13 | fallbackLanguage: T |
4 | 14 |
|
5 | | - constructor(supportedLanguages: T[], fallbackLanguage: T) { |
| 15 | + /** |
| 16 | + * ClientInternationalization utility package for working with languages |
| 17 | + * |
| 18 | + * @param supportedLanguages { Array<string> } - List of all supported languages |
| 19 | + * @param fallbackLanguage { string } - Fallback language if no language matches |
| 20 | + * @param cookieName { string } - Optional: Set a preferred cookie name |
| 21 | + */ |
| 22 | + constructor( |
| 23 | + supportedLanguages: Array<T>, |
| 24 | + fallbackLanguage: T, |
| 25 | + cookieName?: string |
| 26 | + ) { |
6 | 27 | this.supportedLanguages = supportedLanguages |
7 | 28 | this.fallbackLanguage = fallbackLanguage |
| 29 | + |
| 30 | + this.cookieName = typeof cookieName === 'string' ? cookieName : 'language' |
8 | 31 | } |
9 | 32 |
|
| 33 | + /** |
| 34 | + * Detect the browser language of the client |
| 35 | + * If no language was found or if none of the |
| 36 | + * supported language matches a fallback language will be returned |
| 37 | + * |
| 38 | + * @return {string} - Detected language |
| 39 | + */ |
10 | 40 | detectBrowserLanguage(): T { |
11 | | - const browserLanguage = |
12 | | - (navigator.languages && navigator.languages[0]) || navigator.language |
| 41 | + // Check if the DOM is presented. |
| 42 | + // If the DOM isn't accessible the website may be rendered on a server |
| 43 | + if (canUseDOM()) { |
| 44 | + // Get browser language |
| 45 | + const browserLanguage = |
| 46 | + (navigator.languages && navigator.languages[0]) || navigator.language |
| 47 | + |
| 48 | + return ( |
| 49 | + this.supportedLanguages.find((lang) => lang === browserLanguage) || |
| 50 | + this.fallbackLanguage |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + // Return fallback language if no DOM was found |
| 55 | + return this.fallbackLanguage |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Save a language to a cookie |
| 60 | + * If the set language isn't in the list of supported languages |
| 61 | + * the fallback language will be returned |
| 62 | + * |
| 63 | + * @param language { string } - Language to set |
| 64 | + */ |
| 65 | + setLanguage(language: string): void { |
| 66 | + if (canUseDOM()) { |
| 67 | + const cookieLanguage = |
| 68 | + this.supportedLanguages.find((lang) => lang === language) || |
| 69 | + this.fallbackLanguage |
| 70 | + |
| 71 | + setCookie(this.cookieName, cookieLanguage) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the current language |
| 77 | + * This method will first check if a language cookie is present. |
| 78 | + * If no cookie was found the method will get the language form the browser |
| 79 | + * |
| 80 | + * For handling cookies on the server side pass the request object |
| 81 | + * |
| 82 | + * @param request { IncomingMessage } - Optional: Request object for accessing cookies over request headers |
| 83 | + * @return { string } - Found language or fallback language |
| 84 | + */ |
| 85 | + detectLanguage(request?: IncomingMessage): T { |
| 86 | + // Check if request is not undefined |
| 87 | + if (request) { |
| 88 | + // Try to get language form cookie |
| 89 | + const cookieFromRequest = getCookieFromRequest(this.cookieName, request) |
| 90 | + |
| 91 | + if (cookieFromRequest) { |
| 92 | + return cookieFromRequest as T |
| 93 | + } |
| 94 | + |
| 95 | + // Get accept language from request header |
| 96 | + const acceptLanguage = request.headers['accept-language'] |
| 97 | + |
| 98 | + if (acceptLanguage) { |
| 99 | + // Extract the locale string and the priority from header string |
| 100 | + const requestedLocales = acceptLanguage.split(',').map((part) => { |
| 101 | + const [locale, priority] = part.trim().split(';q=') |
| 102 | + |
| 103 | + return { locale, priority: parseInt(priority) } |
| 104 | + }) |
| 105 | + |
| 106 | + // Get the potential locale from requested locales array |
| 107 | + const potentialLocale = requestedLocales |
| 108 | + .sort((a, b) => b.priority - a.priority) |
| 109 | + .find( |
| 110 | + ({ locale }) => |
| 111 | + locale !== '*' && |
| 112 | + this.supportedLanguages.some((lang) => locale === lang) |
| 113 | + ) |
| 114 | + |
| 115 | + // Check if the potential locale is not undefined else return the fallback language |
| 116 | + return (potentialLocale |
| 117 | + ? potentialLocale.locale |
| 118 | + : this.fallbackLanguage) as T |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Check for DOM |
| 123 | + if (canUseDOM()) { |
| 124 | + // Try to get language form cookie |
| 125 | + const language = getCookieFromDocument(this.cookieName) |
| 126 | + |
| 127 | + // If cookie is undefined get the language form the browser |
| 128 | + if (language == undefined) { |
| 129 | + return this.detectBrowserLanguage() |
| 130 | + } |
| 131 | + |
| 132 | + // Return found language or fallback language |
| 133 | + return ( |
| 134 | + this.supportedLanguages.find((lang) => lang === language) || |
| 135 | + this.fallbackLanguage |
| 136 | + ) |
| 137 | + } |
13 | 138 |
|
14 | | - return ( |
15 | | - this.supportedLanguages.find((lang) => lang === browserLanguage) || |
16 | | - this.fallbackLanguage |
17 | | - ) |
| 139 | + return this.fallbackLanguage |
18 | 140 | } |
19 | 141 | } |
20 | 142 |
|
21 | | -export { Internationalization as default } |
| 143 | +export default Internationalization |
0 commit comments