From 471a91dfa5e8620cfa577af6e812a0f650c62b6e Mon Sep 17 00:00:00 2001 From: WoLpH Date: Mon, 14 Nov 2011 18:04:45 +0100 Subject: [PATCH 1/7] added support for a default language --- ngx_http_set_lang_module.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ngx_http_set_lang_module.c b/ngx_http_set_lang_module.c index b39ad5c..73cd3ea 100644 --- a/ngx_http_set_lang_module.c +++ b/ngx_http_set_lang_module.c @@ -24,6 +24,7 @@ static ngx_int_t ngx_http_set_lang_from_geoip (ngx_http_request_t *r, static ngx_int_t ngx_http_set_lang_from_host (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v); static ngx_int_t ngx_http_set_lang_from_referer (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v); static ngx_int_t ngx_http_set_lang_from_var (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v); +static ngx_int_t ngx_http_set_lang_from_default (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v); static char * ngx_http_set_lang (ngx_conf_t *cf, ngx_command_t *cmd, void *cnf); static ngx_int_t ngx_http_set_lang_from_methods (ngx_http_request_t *r, ngx_str_t *v); @@ -79,6 +80,7 @@ static ngx_http_set_lang_method_t ngx_http_set_lang_methods [] = { {ngx_http_set_lang_from_geoip, ngx_string ("geoip")}, {ngx_http_set_lang_from_host, ngx_string ("host")}, {ngx_http_set_lang_from_referer, ngx_string ("referer")}, + {ngx_http_set_lang_from_default, ngx_string ("default")}, {NULL, ngx_null_string} }; @@ -399,6 +401,21 @@ ngx_http_set_lang_from_geoip (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_ +static ngx_int_t +ngx_http_set_lang_from_default (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v) +{ + // Default to the first language in the lang_list + ngx_str_t *lang; + + lang = conf->langs->elts; + v->data = lang->data; + v->len = lang->len; + + return NGX_OK; +} + + + static ngx_int_t ngx_http_set_lang_from_host (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v) { From d23ceee1758398a23c25177c961feca0944525ba Mon Sep 17 00:00:00 2001 From: WoLpH Date: Mon, 14 Nov 2011 18:15:32 +0100 Subject: [PATCH 2/7] changed default to the first lang in the list and added validation to the cookie method --- ngx_http_set_lang_module.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ngx_http_set_lang_module.c b/ngx_http_set_lang_module.c index 73cd3ea..de0a618 100644 --- a/ngx_http_set_lang_module.c +++ b/ngx_http_set_lang_module.c @@ -377,15 +377,28 @@ ngx_http_set_lang_from_post (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t static ngx_int_t ngx_http_set_lang_from_cookie (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v) { - ngx_str_t cookie; + ngx_str_t cookie, *lang; + ngx_uint_t i; if (ngx_http_parse_multi_header_lines (&r->headers_in.cookies, &conf->cookie, &cookie) == NGX_DECLINED) return NGX_DECLINED; - v->data = cookie.data; - v->len = cookie.len; + // compare to lang list - return NGX_OK; + lang = conf->langs->elts; + + for (i=conf->langs->nelts; i; i--,lang++) { + + if (cookie.len == lang->len && !ngx_strncasecmp (cookie.data, lang->data, cookie.len)) { + + v->data = cookie.data; + v->len = cookie.len; + + return NGX_OK; + } + } + + return NGX_DECLINED; } @@ -405,9 +418,13 @@ static ngx_int_t ngx_http_set_lang_from_default (ngx_http_request_t *r, ngx_http_set_lang_loc_conf_t *conf, ngx_str_t *v) { // Default to the first language in the lang_list - ngx_str_t *lang; + ngx_str_t *lang; + ngx_uint_t i; lang = conf->langs->elts; + i = conf->langs->nelts; + while(--i)lang++; + v->data = lang->data; v->len = lang->len; From d858e8721017a3a91cdd1b34d1a3da3494104bac Mon Sep 17 00:00:00 2001 From: WoLpH Date: Mon, 14 Nov 2011 18:22:32 +0100 Subject: [PATCH 3/7] changed readme to rst --- README => README.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README => README.rst (100%) diff --git a/README b/README.rst similarity index 100% rename from README rename to README.rst From a4c100be38426de523fc94735628cc176a2b464a Mon Sep 17 00:00:00 2001 From: WoLpH Date: Mon, 14 Nov 2011 18:35:35 +0100 Subject: [PATCH 4/7] Added simple usage example --- README.rst | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 05cddc2..c739255 100644 --- a/README.rst +++ b/README.rst @@ -23,7 +23,31 @@ Methods for setting language variable Usage ----- - TODO +List the supported locales:: + + lang_list en nl fr; + +If you want to read from and write to cookies (``lang`` is the cookie name):: + + lang_cookie lang; + +To make a (top-level) domain map to a certain locale:: + + lang_host com en; + +And to read the language from the user and put it in a variable:: + + set_lang '$lang' accept_lang get post cookie geoip host referer default; + + +Working example:: + + lang_list en pt_BR; + lang_cookie lang; + set_lang '$lang' cookie accept_lang default; + +The example above uses accept-language to give you any supported language. +If no supported language is found than the ``$lang`` variable will be set to the first language in ``lang_list`` (i.e. ``en``). Installation From 86c1d1c502bdb8d22b9b553dde2b8083633b3a4b Mon Sep 17 00:00:00 2001 From: WoLpH Date: Mon, 14 Nov 2011 18:36:36 +0100 Subject: [PATCH 5/7] Updated copyright --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index c739255..2717d59 100644 --- a/README.rst +++ b/README.rst @@ -60,8 +60,8 @@ Installation Copyright --------- - Marcus Clyne (c) 2010 - + - Marcus Clyne (c) 2010 + - Rick van Hattem (c) 2011 License ------- From 7b0cd0cbbd928b72f181f8aafd8427b5299769e5 Mon Sep 17 00:00:00 2001 From: WoLpH Date: Fri, 18 Nov 2011 02:25:52 +0100 Subject: [PATCH 6/7] added expires header to the cookies --- ngx_http_set_lang_module.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ngx_http_set_lang_module.c b/ngx_http_set_lang_module.c index de0a618..4f3cf43 100644 --- a/ngx_http_set_lang_module.c +++ b/ngx_http_set_lang_module.c @@ -6,6 +6,11 @@ #include +#define MINUTE 60 +#define HOUR 60 * MINUTE +#define DAY 24 * MINUTE +#define YEAR 365 * DAY +#define COOKIE_PARAM_BUFFER 40 // TODO : add setting a variable that dictates the method - $set_lang_method @@ -199,6 +204,27 @@ ngx_http_set_lang_from_methods (ngx_http_request_t *r, ngx_str_t *v) ngx_http_variable_value_t *mv; ngx_http_set_lang_func_pt func; + // Generate the extra cookie params (path + expires) + static char cookie_params[COOKIE_PARAM_BUFFER]; + struct tm timestruct; + time_t now; + + // Fetch the current time + time(&now); + + // Default the expires date to 1 year in the future + now += YEAR; + + // Format the string as RFC 2616 specifies + gmtime_r(&now, ×truct); + + strftime( + cookie_params, + COOKIE_PARAM_BUFFER, + "; path=/; expires=%a, %d %b %Y %H:%M:%S GMT", + ×truct + ); + llcf = ngx_http_get_module_loc_conf (r, ngx_http_set_lang_module); if (!llcf->enable) @@ -226,7 +252,7 @@ ngx_http_set_lang_from_methods (ngx_http_request_t *r, ngx_str_t *v) // create lang cookie - len = llcf->cookie.len + v->len + sizeof ("; path=/"); + len = llcf->cookie.len + v->len + sizeof (cookie_params); cookie = ngx_pnalloc (r->pool, len + 1); if (cookie == NULL) { @@ -237,7 +263,7 @@ ngx_http_set_lang_from_methods (ngx_http_request_t *r, ngx_str_t *v) *p++ = '='; p = ngx_copy (p, v->data, v->len); - p = ngx_copy (p, "; path=/", sizeof ("; path=/")); + p = ngx_copy (p, cookie_params, sizeof (cookie_params)); // add lang cookie to main response From 291905ac701a252dd09b8dda1d5ece399e6c9953 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Mon, 12 Dec 2011 18:24:39 +0100 Subject: [PATCH 7/7] Added more efficient patch from simpl --- ngx_http_set_lang_module.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ngx_http_set_lang_module.c b/ngx_http_set_lang_module.c index 4f3cf43..64fa42a 100644 --- a/ngx_http_set_lang_module.c +++ b/ngx_http_set_lang_module.c @@ -1,4 +1,3 @@ - /* * Copyright (c) 2009 Marcus Clyne */ @@ -445,12 +444,9 @@ ngx_http_set_lang_from_default (ngx_http_request_t *r, ngx_http_set_lang_loc_con { // Default to the first language in the lang_list ngx_str_t *lang; - ngx_uint_t i; - - lang = conf->langs->elts; - i = conf->langs->nelts; - while(--i)lang++; + lang = ((ngx_str_t *)conf->langs->elts) + (conf->langs->nelts - 1); + v->data = lang->data; v->len = lang->len;