Skip to content

Commit 03d31ba

Browse files
author
Simon Planinschek
committed
Add client side Internationalization class.
1 parent 08d3e29 commit 03d31ba

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

src/ClientInternationalization.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { getCookieFromDocument, canUseDOM, setCookie } from './utilities'
2+
3+
class ClientInternationalization<T extends string> {
4+
private readonly cookieName: string
5+
6+
supportedLanguages: Array<T>
7+
fallbackLanguage: T
8+
9+
/**
10+
* ClientInternationalization utility package for working with languages
11+
*
12+
* Please note! All methods in this class will only run on the client side
13+
*
14+
* @param supportedLanguages { Array<string> } - List of all supported languages
15+
* @param fallbackLanguage { string } - Fallback language if no language matches
16+
* @param cookieName { string } - Optional: Set a preferred cookie name
17+
*/
18+
constructor(
19+
supportedLanguages: Array<T>,
20+
fallbackLanguage: T,
21+
cookieName?: string
22+
) {
23+
this.supportedLanguages = supportedLanguages
24+
this.fallbackLanguage = fallbackLanguage
25+
this.cookieName = typeof cookieName === 'string' ? cookieName : 'language'
26+
}
27+
28+
/**
29+
* Detect the browser language of the client
30+
* If no language was found or if none of the
31+
* supported language matches a fallback language will be returned
32+
*
33+
* @return {string} - Detected language
34+
*/
35+
detectBrowserLanguage(): T {
36+
// Check if the DOM is presented.
37+
// If the DOM isn't accessible the website may be rendered on a server
38+
if (canUseDOM()) {
39+
// Get browser language
40+
const browserLanguage =
41+
(navigator.languages && navigator.languages[0]) || navigator.language
42+
43+
return (
44+
this.supportedLanguages.find((lang) => lang === browserLanguage) ||
45+
this.fallbackLanguage
46+
)
47+
}
48+
49+
// Return fallback language if no DOM was found
50+
return this.fallbackLanguage
51+
}
52+
53+
/**
54+
* Save a language to a cookie
55+
* If the set language isn't in the list of supported languages
56+
* the fallback fallback language is returned
57+
*
58+
* @param language { string } - Language to set
59+
*/
60+
saveSetLanguage(language: string): void {
61+
if (canUseDOM()) {
62+
const cookieLanguage =
63+
this.supportedLanguages.find((lang) => lang === language) ||
64+
this.fallbackLanguage
65+
66+
setCookie(this.cookieName, cookieLanguage)
67+
}
68+
}
69+
70+
/**
71+
* Get the current language
72+
* This method will first check if a language cookie is present.
73+
* If no cookie was found the method will get the language form the browser
74+
*
75+
* If no language was found, this method simply calls the detectBrowserLanguage method
76+
*
77+
* @return { string } - Found language or fallback language
78+
*/
79+
detectLanguage(): T {
80+
// Check for DOM
81+
if (canUseDOM()) {
82+
// Try to get language form cookie
83+
const language = getCookieFromDocument(this.cookieName)
84+
85+
// If cookie is undefined get the language form the browser
86+
if (language == undefined) {
87+
return this.detectBrowserLanguage()
88+
}
89+
90+
// Return found language or fallback language
91+
return (
92+
this.supportedLanguages.find((lang) => lang === language) ||
93+
this.fallbackLanguage
94+
)
95+
}
96+
97+
// Return fallback language is DOM is null
98+
return this.fallbackLanguage
99+
}
100+
}
101+
102+
export default ClientInternationalization

0 commit comments

Comments
 (0)