Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language autodetection strategy based on <html lang> #115

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ A __lightweight__ & __gdpr compliant__ cookie consent plugin written in plain ja
page_scripts: true, // default: false

// delay: 0, // default: 0
// auto_language: false, // default: false
// auto_language: null // default: null; could also be 'browser' or 'document'
// autorun: true, // default: true
// force_consent: false, // default: false
// hide_from_bots: false, // default: false
Expand Down Expand Up @@ -219,7 +219,7 @@ A __lightweight__ & __gdpr compliant__ cookie consent plugin written in plain ja
page_scripts: true, // default: false

// delay: 0, // default: 0
// auto_language: false, // default: false
// auto_language: '', // default: null; could also be 'browser' or 'document'
// autorun: true, // default: true
// force_consent: false, // default: false
// hide_from_bots: false, // default: false
Expand Down Expand Up @@ -427,7 +427,7 @@ Additional methods for an easier management of your scripts and cookie settings
// ...
toggle: {
value: 'analytics', // cookie category
enabled : false, // default status
enabled: false, // default status
readonly: false // allow to enable/disable
// reload: 'on_disable', // allows to reload page when the current cookie category is deselected
}
Expand Down Expand Up @@ -564,7 +564,7 @@ Below a table which sums up all of the available options (must be passed to the
| `force_consent` | boolean | false | Enable if you want to block page navigation until user action (check [faq](#faq) for a proper implementation) |
| `revision` | number | 0 | Specify this option to enable revisions. [Check below](#how-to-enablemanage-revisions) for a proper usage |
| `current_lang` | string | - | Specify one of the languages you have defined (can also be dynamic): `'en'`, `'de'` ... |
| `auto_language` | boolean | false | Automatically detect language based on the user's browser language, if language is not defined => use specified `current_lang` |
| `auto_language` | string | null | Language auto-detection strategy. Null to disable (default), `"browser"` to get user's browser language or `"document"` to read value from `<html lang="...">` of current page. If language is not defined => use specified `current_lang` |
| `autoclear_cookies` | boolean | false | Enable if you want to automatically delete cookies when user opts-out of a specific category inside cookie settings |
| `page_scripts` | boolean | false | Enable if you want to easily `manage existing <script>` tags. Check [manage third party scripts](#manage-third-party-scripts) |
| `remove_cookie_tables`| boolean | false | Enable if you want to remove the html cookie tables (but still want to make use of `autoclear_cookies`) |
Expand Down
6 changes: 3 additions & 3 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ cc.run({
cookie_expiration : 365, // default: 182
page_scripts: true, // default: false

// auto_language : false, // default: false
// autorun : true, // default: true
// delay : 0, // default: 0
// auto_language: null, // default: null; could also be 'browser' or 'document'
// autorun: true, // default: true
// delay: 0, // default: 0
// force_consent: false,
// hide_from_bots: false, // default: false
// remove_cookie_tables: false // default: false
Expand Down
6 changes: 3 additions & 3 deletions demo/app2.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ cc.run({
page_scripts: true, // default: false
force_consent: true, // default: false

// auto_language : false, // default: false
// autorun : true, // default: true
// delay : 0, // default: 0
// auto_language: null, // default: null; could also be 'browser' or 'document'
// autorun: true, // default: true
// delay: 0, // default: 0
// hide_from_bots: false, // default: false
// remove_cookie_tables: false // default: false
// cookie_domain: location.hostname, // default: current domain
Expand Down
40 changes: 32 additions & 8 deletions src/cookieconsent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

var _config = {
current_lang: 'en',
auto_language: null,
autorun: true, // run as soon as loaded
cookie_name: 'cc_cookie',
cookie_expiration: 182, // default: 6 months (in days)
Expand Down Expand Up @@ -143,14 +144,13 @@
_config.page_scripts = conf_params['page_scripts'] === true;
_config.page_scripts_order = conf_params['page_scripts_order'] !== false;

if(conf_params['auto_language'] === true){
_config.current_lang = _getValidatedLanguage(_getBrowserLang(), conf_params.languages);
}else{
if(typeof conf_params['current_lang'] === "string")
_config.current_lang = _getValidatedLanguage(conf_params['current_lang'], conf_params.languages);
if (conf_params['auto_language'] === 'browser' || conf_params['auto_language'] === true) {
_config.auto_language = 'browser';
} else if (conf_params['auto_language'] === 'document') {
_config.auto_language = 'document';
}

_log("CookieConsent [LANG]: setting current_lang = '" + _config.current_lang + "'");
_config.current_lang = _resolveCurrentLang(conf_params.languages, conf_params['current_lang']);
}

/**
Expand Down Expand Up @@ -940,7 +940,7 @@
/**
* Helper function which prints info (console.log())
* @param {Object} print_msg
* @param {Object} optional_param
* @param {Object} [optional_param]
*/
var _log = function(print_msg, optional_param, error){
ENABLE_LOGS && (!error ? console.log(print_msg, optional_param !== undefined ? optional_param : ' ') : console.error(print_msg, optional_param || ""));
Expand All @@ -959,6 +959,30 @@
return el;
}

/**
* Resolve which language should be used.
*
* @param {Object} languages Object with language translations
* @param {string} [requested_language] Language specified by given configuration parameters
* @returns {string}
*/
var _resolveCurrentLang = function (languages, requested_language) {
_log("CookieConsent [LANG]: auto_language strategy is '" + _config.auto_language + "'");

if (_config.auto_language === 'browser') {
return _getValidatedLanguage(_getBrowserLang(), languages);
} else if (_config.auto_language === 'document') {
return _getValidatedLanguage(document.documentElement.lang, languages);
} else {
if (typeof requested_language === 'string') {
return _config.current_lang = _getValidatedLanguage(requested_language, languages);
}
}

_log("CookieConsent [LANG]: setting current_lang = '" + _config.current_lang + "'");
return _config.current_lang; // otherwise return default
}

/**
* Get current client's browser language
* @returns {string}
Expand Down Expand Up @@ -1452,7 +1476,7 @@
/**
* Function which will run after script load
* @callback scriptLoaded
*/
*/

/**
* Dynamically load script (append to head)
Expand Down