Improve locale selection for Potlatch 2 and iD
authorTom Hughes <tom@compton.nu>
Wed, 8 May 2013 21:28:41 +0000 (22:28 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 8 May 2013 21:28:41 +0000 (22:28 +0100)
app/controllers/application_controller.rb
app/views/site/_id.html.erb
app/views/site/_potlatch2.html.erb
app/views/site/id.html.erb

index d541eba22f55ab90bf0ee972e964a7f968b7073f..004db5f8403665200bcb162f848b9e35c4945f41 100644 (file)
@@ -288,39 +288,44 @@ class ApplicationController < ActionController::Base
   def set_locale
     response.header['Vary'] = 'Accept-Language'
 
-    if @user
-      if !@user.languages.empty?
-        request.user_preferred_languages = @user.languages
-        response.header['Vary'] = '*'
-      elsif !request.user_preferred_languages.empty?
-        @user.languages = request.user_preferred_languages
-        @user.save
-      end
+    if @user && !@user.languages.empty?
+      request.user_preferred_languages = @user.languages
+      response.header['Vary'] = '*'
+    end
+
+    I18n.locale = select_locale
+
+    if @user && @user.languages.empty? && !request.user_preferred_languages.empty?
+      @user.languages = request.user_preferred_languages
+      @user.save
     end
 
-    if request.compatible_language_from(I18n.available_locales).nil?
+    response.headers['Content-Language'] = I18n.locale.to_s
+  end
+
+  def select_locale(locales = I18n.available_locales)
+    if params[:locale]
+      request.user_preferred_languages = [ params[:locale] ]
+    end
+
+    if request.compatible_language_from(locales).nil?
       request.user_preferred_languages = request.user_preferred_languages.collect do |pl|
         pls = [ pl ]
 
         while pl.match(/^(.*)-[^-]+$/)
-          pls.push($1) if I18n.available_locales.include?($1.to_sym)
+          pls.push($1) if locales.include?($1) or locales.include?($1.to_sym)
           pl = $1
         end
 
         pls
       end.flatten
-
-      if @user and not request.compatible_language_from(I18n.available_locales).nil?
-        @user.languages = request.user_preferred_languages
-        @user.save        
-      end
     end
 
-    I18n.locale = params[:locale] || request.compatible_language_from(I18n.available_locales) || I18n.default_locale
-
-    response.headers['Content-Language'] = I18n.locale.to_s
+    request.compatible_language_from(locales) || I18n.default_locale
   end
 
+  helper_method :select_locale
+
   def api_call_handle_error
     begin
       yield
index fe9d569aa74a946435b2654d8f3630abbc19c3a5..e2f783731b7ff50f6107e5af77ee7661963724b8 100644 (file)
@@ -19,7 +19,7 @@
       hash = '#map=' + (coord.zoom || 17) + '/' + coord.lon + '/' + coord.lat
     }
 
-    $('#id-embed').attr('src', '<%= id_url %>' + hash);
+    $('#id-embed').attr('src', '<%= id_url :locale => params[:locale] %>' + hash);
   </script>
 <% else %>
   <script type="text/javascript">alert("<%= t 'site.edit.id_not_configured' %>")</script>
index 65ca9210e11734d4209c9d4fe9b12158102bf366..0c0a859727160112fbd82edf46eb3497d66943c2 100644 (file)
@@ -10,7 +10,7 @@
 <script type="text/javascript">alert("<%= t 'site.edit.potlatch2_not_configured' %>")</script>
 <% end %>
 
-<% locale = request.compatible_language_from(Potlatch2::LOCALES.keys) || "en" %>
+<% locale = select_locale(Potlatch2::LOCALES.keys) %>
 
 <script type="text/javascript" defer="defer">
   var changesaved=true;
index 8565cb9ea9f84c87fe0bea5f359940443cc3daa5..96262f1e2c4e0b1480c09bf39bb8669f06f20d18 100644 (file)
@@ -16,7 +16,7 @@
       'Please upgrade your browser or use Potlatch 2 to edit the map.';
     document.getElementById('id-container').className = 'unsupported';
   } else {
-    <% locale = request.compatible_language_from(ID::LOCALES) || "en" %>
+    <% locale = select_locale(ID::LOCALES) %>
 
     var id = iD()
       .embed(true)