From c15edbe808649eeab0dad44d912e00590d58308b Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sat, 15 Oct 2011 10:10:58 +0200 Subject: Fix language switching. Switching between languages didn't work in some constellations. This seems to be because of caching of the pages in the browser. I think the browser should not use the cached page, because it has a different cookie, but it did. So I changed the expire on normal web pages to 0. (API pages still have a longer expire.) --- taginfo-config-example.json | 2 ++ web/taginfo.rb | 15 ++++++++++++--- web/views/layout.erb | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/taginfo-config-example.json b/taginfo-config-example.json index 9dbc624..efcbcab 100644 --- a/taginfo-config-example.json +++ b/taginfo-config-example.json @@ -3,6 +3,8 @@ { // You probably want to change all these. "instance": { + // url prefix for the site + "url": "http://localhost:4567", // Used in the title of all HTML pages. "name": "Test Taginfo", // Description that appears if you click on the instance icon in the upper left. diff --git a/web/taginfo.rb b/web/taginfo.rb index d76c65d..67310eb 100755 --- a/web/taginfo.rb +++ b/web/taginfo.rb @@ -95,6 +95,12 @@ class Taginfo < Sinatra::Base erb_orig template, options, locals end + # when do we expect the next data update + def next_update + # three hours after midnight UTC + ((Time.utc(Time.now.year(), Time.now.month(), Time.now.day(), 3, 0, 0) + (Time.now.hour < 3 ? 0 : 24)*60*60)-Time.now).to_i.to_i + end + before do if request.cookies['taginfo_locale'] && request.path != '/switch_locale' params[:locale] = request.cookies['taginfo_locale'] @@ -107,8 +113,9 @@ class Taginfo < Sinatra::Base javascript 'lang/' + r18n.locale.code javascript 'taginfo' - # set expire to three hours after midnight UTC - expires(((Time.utc(Time.now.year(), Time.now.month(), Time.now.day(), 3, 0, 0) + (Time.now.hour < 3 ? 0 : 24)*60*60)-Time.now).to_i.to_i) + # set to immediate expire on normal pages + # (otherwise switching languages doesn't work) + expires 0, :no_cache @db = SQL::Database.new('../../data') @@ -125,6 +132,7 @@ class Taginfo < Sinatra::Base before '/api/*' do content_type :json + expires next_update end #------------------------------------- @@ -133,7 +141,7 @@ class Taginfo < Sinatra::Base # It sets a cookie and redirects back to the page the user was coming from. get '/switch_locale' do response.set_cookie('taginfo_locale', params[:locale]) - redirect params[:url] + redirect(TaginfoConfig.get('instance.url') + params[:url]) end #------------------------------------- @@ -268,6 +276,7 @@ class Taginfo < Sinatra::Base #------------------------------------- get '/js/lang/:lang.js' do + expires next_update trans = R18n::I18n.new(params[:lang], 'i18n') return 'var texts = ' + { :flexigrid => { diff --git a/web/views/layout.erb b/web/views/layout.erb index c91f8ac..144e7e2 100644 --- a/web/views/layout.erb +++ b/web/views/layout.erb @@ -14,7 +14,7 @@