11import { getCookieFromDocument , canUseDOM , setCookie } from './utilities'
22
33class ClientInternationalization < T extends string > {
4- private readonly cookieName : string
4+ protected readonly cookieName : string
55
66 supportedLanguages : Array < T >
77 fallbackLanguage : T
@@ -22,6 +22,7 @@ class ClientInternationalization<T extends string> {
2222 ) {
2323 this . supportedLanguages = supportedLanguages
2424 this . fallbackLanguage = fallbackLanguage
25+
2526 this . cookieName = typeof cookieName === 'string' ? cookieName : 'language'
2627 }
2728
@@ -53,11 +54,11 @@ class ClientInternationalization<T extends string> {
5354 /**
5455 * Save a language to a cookie
5556 * If the set language isn't in the list of supported languages
56- * the fallback fallback language is returned
57+ * the fallback language will be returned
5758 *
5859 * @param language { string } - Language to set
5960 */
60- saveSetLanguage ( language : string ) : void {
61+ saveLanguage ( language : string ) : void {
6162 if ( canUseDOM ( ) ) {
6263 const cookieLanguage =
6364 this . supportedLanguages . find ( ( lang ) => lang === language ) ||
@@ -67,13 +68,27 @@ class ClientInternationalization<T extends string> {
6768 }
6869 }
6970
71+ /**
72+ * Save a language to a cookie
73+ * This method will not check if the language you want to set is supported
74+ * Please note! If you have changed the cookie name, you must change it here as well
75+ *
76+ * @param language { string } - Language to set
77+ * @param cookieName { string } - Optional: Cookie name
78+ */
79+ static setLanguage ( language : string , cookieName ?: string ) : void {
80+ if ( canUseDOM ( ) ) {
81+ cookieName = typeof cookieName === 'string' ? cookieName : 'language'
82+
83+ setCookie ( cookieName , language )
84+ }
85+ }
86+
7087 /**
7188 * Get the current language
7289 * This method will first check if a language cookie is present.
7390 * If no cookie was found the method will get the language form the browser
7491 *
75- * If no language was found, this method simply calls the detectBrowserLanguage method
76- *
7792 * @return { string } - Found language or fallback language
7893 */
7994 detectLanguage ( ) : T {
0 commit comments