I've got a setup with authcache and locale cookie (both latest dev).
I have configured language selection in the order session - cookie - browser - default
I've added a authcache property:
/**
* Implements hook_authcache_key_properties_alter().
*/
function cst_cache_crawler_authcache_key_properties_alter(&$properties) {
global $language;
$properties['lang'] = $language->language;
}
It seems that authcache does not respect the $conf['page_cache_invoke_hooks'] = TRUE; in the settings.php which is adviced from locale_cookie. If i switch the language with the module lang_dropdown the ?language=en get paramenter gets appended to the url, the cookie get set for anonymous users. But authcache does not register it and skips the processing so the locale module will not know the new language and my authcache property does not switch.
For rebuilding the problem here a step by step:
- Clear all caches
- Visit front page in all languages to warm the cache
- switch the language to another than your current
- remove the get parameter "language" from the url
- now you get a cache hit and the language is not determined by cookie or session
- fallback / default language is going to be served from cache
The only workarround for this for now is to create sessions for every anonymous user to store the language in it. But this have to be tested.
For authenticated users there is another behaviour because they have a session where the language will get stored in so the cookie is not relevant for languge selection.
Comment | File | Size | Author |
---|---|---|---|
#15 | htaccess-lang.patch | 1.7 KB | znerol |
Comments
Comment #1
fox_01 CreditAttribution: fox_01 commentedIt seems that there is the same behavior if i save the language information in a cookie for anonymous users. Every time the page is getting served from cache the browser / default language will show up. If i switch to a non-cached page the language automatically switches to the right one.
Comment #2
znerol CreditAttribution: znerol commented$conf['page_cache_invoke_hooks'] = TRUE;
indeed is ignored by Authcache. However, it is possible to influence the way the authcache-key is calculated for anonymous users. The standard implementation simply uses$base_root
as the authcache key for anonymous users. You can override that using theauthcache_key_generator
variable by supplying a callable viasettings.php
like this:Be sure to thoroughly sanitize the cookie value before concatenating with the
$base_root
. You may just compare it to a whitelist of allowed values for example or use aswitch
statement.Comment #3
znerol CreditAttribution: znerol commentedLinked this issue from the documentation. I guess this is answered, otherwise reopen.
Comment #5
chanac_hares CreditAttribution: chanac_hares commentedHello,
Firstly , Sorry for my bad english .
I re-open this bug , because i use authcache for anomymous users and language_cookie module.
in my settings.php i have
But my problem is the folowing,
I visite mysite.fr -> My cookie_language = 'fr' because french is the default language
I visite mysite.fr/en -> My cookie_language = 'en' OK
I back to mysite.fr -> My cookie_language = 'fr' OK
I back to mysite.fr/en -> My cookie stay 'fr' KO
Like if after 3 changes authcache not change cookie_language ever ... Somebody can help me ?
Comment #6
chanac_hares CreditAttribution: chanac_hares commentedPlease see #5 for know why i re-open the bug .
Thx
Comment #7
znerol CreditAttribution: znerol commented@chanac_hares Does your setup work correctly without Authcache? Also note that the OP apparently uses Locale Cookie and not Language Cookie.
Comment #8
chanac_hares CreditAttribution: chanac_hares commented@znerol,
Locale Cookie and Language Cookie. seems to be exactly the same, except the name.
Moreover, I activate authcache only for anonymous user, thus when i use an authentificated user or another role my cookie change itself correctly.
But, in your previous post #2 , you say
, maybe if hook are badly invoked in authcache , is for that that my cookie does not change correctly.
Have you another idea ?
Comment #9
znerol CreditAttribution: znerol commentedOk, I've read through the code of Language Cookie and it indeed looks like it is incompatible with Authcache due to its reliance on
hook_boot
.But frankly I do not understand at all the use-case it solves in this particular case. Especially I do not understand the reason to combine cookie based language negotiation with URL based language negotiation.
My question is: If you have different URLs for each language of your site (which is good also for SEO), what is the reason to additionally take into account a cookie value? I.e.
mysite.fr
andmysite.fr/en
are already separate entries in the cache due to the different URLs. What purpose is the cookie serving?Comment #10
chanac_hares CreditAttribution: chanac_hares commentedArf, there is no solution for me ...
i combine cookie language negociation ans base url negociation, for solve the following use case :
Do you think it's a strange use case ?
Comment #11
znerol CreditAttribution: znerol commented@chanac_hares: Thanks for the explanation. It indeed makes sens to store the last used language in the users browser. However the way the mentioned modules are implemented is inherently incompatible with authcache (and also with external caches).
Still I think this is solvable if the language cookie would be set using JavaScript in the client instead of in hook_boot() on the server. I will try to explore that solution as time permits.
Comment #12
znerol CreditAttribution: znerol commentedI thought about the problem a bit more and this is what I recommend to owners of multilingual sites wishing to use page level caching (including Varnish, Boost and Authcache):
Exclusively use URL based language negotiation (either with subdomains or path prefixes). Do not enable any language negotiation method which depends on request properties which cannot be encoded in the URL such as browser (
Accept-Language
header) or Cookie. Otherwise there is a risk that content in different languages is mixed up when storing/delivering cached pages.Use your web-server or gateway cache to redirect first-time users hitting your front-page to the page in the proper language (e.g. by inspecting the
Accept-Language
header). This could be done withmod_rewrite
(Apache), or a VCL snipped (Varnish).In order to address the use case outlined in #10, i.e. remember the language for a returning user, set a cookie in the users browser with the interface language of the currently viewed page - but use a simple JavaScript implementation instead of trying to set it in
hook_boot
orhook_init
.The code can be as simple as this:
This approach can be reused to also store other site-preferences, e.g. consumer vs business division, country/branch office for multinational sites. Obviously this does not work with user-specific data - the cookie properties will be cached along with the page.
Please post snippets of your
mod_rewrite
,VCL
,Nginx
config here if you've successfully implemented this approach.Comment #13
chanac_hares CreditAttribution: chanac_hares commentedOKay thank you for your answer, and for have take the time to think to my problem !
Your hook_page_build() + js code work like a charm, now my cookie change each time i switch the langage.
Now i have two new problem, but maybe i'am missing something (very possible !) ..
I have set up your hook + js code, and i add the folowing code in my settings.php for recalculate the authcache_key
It doesnt work, snif ...
I have two more question and some tracks digging:
I cant dig deeper for today, i will continue tomorow, But i you have some advises, tracks or tricks i'am your man !
Thx in advance
Comment #14
znerol CreditAttribution: znerol commentedThe
lang
cookie is set when the browser renders the page. This is obviously after it was built by Drupal and after it has been requested by the browser.With the method outlined in #12 you should not use
authcache_key_generator
anymore. Instead you need to set up the following structure:http://mysite.com/
no content, only redirects to /fr or /en based on the cookiehttp://mysite.com/fr/
french sitehttp://mysite.com/en/
english siteIt is not possible to implement the redirection in PHP (at least if you do not want to hack
index.php
), instead you need to find a way to trigger the redirection by your webserver (presumably Apache). I recommend to search the web for something like mod_rewrite redirect based on a cookie value.Comment #15
znerol CreditAttribution: znerol commentedI propose the following
mod_rewrite
scheme. Consider a site with the following languages enabled (ordered by site preference): Dutch (default), French, English.Edit
.htaccess
and scroll down to theRewriteBase
statement. All rewrite rules should be inserted below that line.Start with initializing a new variable
lang
with the default language:If you wish to detect the language in the users browser, add the following condition/rule pair for every language from low to high priority (i.e. if your site is best viewed in Dutch, second best in French and third in English, then first insert a rule for
en
, then one forfr
and finally one fornl
.If you wish to detect the language from the
lang
cookie, add the following condition/rule pair for every language from low to high priority (i.e. if your site is best viewed in Dutch, second best in French and third in English, then first insert a rule foren
, then one forfr
and finally one fornl
.Finally we need one condition/rule pair to redirect users hitting the front-page:
And optionally we can also redirect every page without a language prefix, unless it is a file on the disk:
The attached patch shows the whole example ready for inclusion into a
.htaccess
file.Comment #16
chanac_hares CreditAttribution: chanac_hares commentedHello znerol,
Thx for your eplanation, I think we are closed.
But your solution seems doesnt work with the htaccess below ( definitively is my fault !! ) , because of the two following point :
My htaccess is :
any idea ?
Comment #17
znerol CreditAttribution: znerol commentedPlease take a close look at the patch file I posted in #15 and take that as the basis of your work.
Comment #18
chanac_hares CreditAttribution: chanac_hares commentedHello znerol,
Finally i improve my first solution until the following result :
The probleme of compliance between cookie_language and authcache is resolved by changing the cookie into the key calculating function itself ..
Now every thing is OK . I add a deal whith persistent login too .
Thx for your precious help .
Comment #19
znerol CreditAttribution: znerol commented