From 06f02ce9fcf7e2fc2083c3efa4aaf94b8e016b97 Mon Sep 17 00:00:00 2001 From: riking Date: Thu, 5 Feb 2015 19:38:51 -0800 Subject: [PATCH] FIX: :sa: Allow closing polls in multi-locale sites --- app/controllers/application_controller.rb | 4 ++-- app/models/user.rb | 8 ++++++++ app/services/post_alerter.rb | 6 +----- plugins/poll/README.md | 2 +- .../assets/javascripts/controllers/poll.js.es6 | 2 +- plugins/poll/plugin.rb | 10 ++++++---- plugins/poll/poll.rb | 17 ++++++----------- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4ca9107d014..3a94fe59bdf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -147,8 +147,8 @@ class ApplicationController < ActionController::Base end def set_locale - I18n.locale = if SiteSetting.allow_user_locale && current_user && current_user.locale.present? - current_user.locale + I18n.locale = if current_user + current_user.effective_locale else SiteSetting.default_locale end diff --git a/app/models/user.rb b/app/models/user.rb index 4a8cd72b099..c0de2fd439e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -138,6 +138,14 @@ class User < ActiveRecord::Base User.where(username_lower: lower).blank? end + def effective_locale + if SiteSetting.allow_user_locale && self.locale.present? + self.locale + else + SiteSetting.default_locale + end + end + EMAIL = %r{([^@]+)@([^\.]+)} def self.new_from_params(params) diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 4d5da91c00e..ac24e0ff549 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -118,11 +118,7 @@ class PostAlerter if collapsed post = first_unread_post(user,post.topic) || post count = unread_count(user, post.topic) - I18n.with_locale(if SiteSetting.allow_user_locale && user.locale.present? - user.locale - else - SiteSetting.default_locale - end) do + I18n.with_locale(user.effective_locale) do opts[:display_username] = I18n.t('embed.replies', count: count) if count > 1 end end diff --git a/plugins/poll/README.md b/plugins/poll/README.md index 5fabf2cc031..805c0818c71 100644 --- a/plugins/poll/README.md +++ b/plugins/poll/README.md @@ -9,7 +9,7 @@ Allows you to add a poll to the first post of a topic. ## Closing the poll -Change the start of the topic title from "Poll: " to "Closed Poll: ". This feature is disabled if the `allow_user_locale` site setting is enabled. +Change the start of the topic title from "Poll: " to "Closed Poll: ". This feature uses the locale of the user who started the topic. _Note: closing a topic will also close the poll._ diff --git a/plugins/poll/assets/javascripts/controllers/poll.js.es6 b/plugins/poll/assets/javascripts/controllers/poll.js.es6 index 5ac1db7bd40..f582c58f686 100644 --- a/plugins/poll/assets/javascripts/controllers/poll.js.es6 +++ b/plugins/poll/assets/javascripts/controllers/poll.js.es6 @@ -5,7 +5,7 @@ export default DiscourseController.extend({ showResults: Em.computed.oneWay('poll.closed'), disableRadio: Em.computed.any('poll.closed', 'loading'), showToggleClosePoll: function() { - return this.get('poll.post.topic.details.can_edit') && !Discourse.SiteSettings.allow_user_locale; + return this.get('poll.post.topic.details.can_edit'); }.property('poll.post.topic.details.can_edit'), actions: { diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index a4a6fb9e30b..a5f0b1fe5af 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -80,10 +80,12 @@ after_initialize do end # Modify topic title. - if topic.title =~ /^(#{I18n.t('poll.prefix').strip})\s?:/i - topic.title = topic.title.gsub(/^(#{I18n.t('poll.prefix').strip})\s?:/i, I18n.t('poll.closed_prefix') + ':') - elsif topic.title =~ /^(#{I18n.t('poll.closed_prefix').strip})\s?:/i - topic.title = topic.title.gsub(/^(#{I18n.t('poll.closed_prefix').strip})\s?:/i, I18n.t('poll.prefix') + ':') + I18n.with_locale(topic.user.effective_locale) do + if topic.title =~ /^(#{I18n.t('poll.prefix').strip})\s?:/i + topic.title = topic.title.gsub(/^(#{I18n.t('poll.prefix').strip})\s?:/i, I18n.t('poll.closed_prefix') + ':') + elsif topic.title =~ /^(#{I18n.t('poll.closed_prefix').strip})\s?:/i + topic.title = topic.title.gsub(/^(#{I18n.t('poll.closed_prefix').strip})\s?:/i, I18n.t('poll.prefix') + ':') + end end topic.acting_user = current_user diff --git a/plugins/poll/poll.rb b/plugins/poll/poll.rb index a833ffb082e..9d663951fca 100644 --- a/plugins/poll/poll.rb +++ b/plugins/poll/poll.rb @@ -21,19 +21,13 @@ module ::PollPlugin return false end - topic.title =~ /^(#{I18n.t('poll.prefix').strip}|#{I18n.t('poll.closed_prefix').strip})\s?:/i + I18n.with_locale(topic.user.effective_locale) do + topic.title =~ /^(#{I18n.t('poll.prefix').strip}|#{I18n.t('poll.closed_prefix').strip})\s?:/i + end end def has_poll_details? - if SiteSetting.allow_user_locale? - # If we allow users to select their locale of choice we cannot detect polls - # by the prefix, so we fall back to checking if the poll details is set in - # places to make sure polls are still accessible by users using a different - # locale than the one used by the topic creator. - not self.details.nil? - else - self.is_poll? - end + self.is_poll? end # Called during validation of poll posts. Discourse already restricts edits to @@ -60,7 +54,8 @@ module ::PollPlugin end def is_closed? - @post.topic.closed? || @post.topic.archived? || (!SiteSetting.allow_user_locale? && (@post.topic.title =~ /^#{I18n.t('poll.closed_prefix')}/i) === 0) + topic = @post.topic + topic.closed? || topic.archived? || (topic.title =~ /^#{I18n.t('poll.closed_prefix', locale: topic.user.effective_locale)}/i) === 0 end def options