From 45bdd23689f83a90de2df5e089bab5d52af1c4d4 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 20 May 2015 15:56:54 +1000 Subject: [PATCH] FEATURE: support user local switching to RTL correctly FEATURE: support RTL in multisite --- app/assets/stylesheets/desktop_rtl.scss | 2 ++ app/assets/stylesheets/mobile_rtl.scss | 2 ++ app/helpers/application_helper.rb | 10 ++++++++++ app/views/common/_discourse_stylesheet.html.erb | 6 +++++- lib/sass/discourse_sass_compiler.rb | 4 +--- lib/sass/discourse_stylesheets.rb | 3 ++- lib/tasks/assets.rake | 2 +- 7 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 app/assets/stylesheets/desktop_rtl.scss create mode 100644 app/assets/stylesheets/mobile_rtl.scss diff --git a/app/assets/stylesheets/desktop_rtl.scss b/app/assets/stylesheets/desktop_rtl.scss new file mode 100644 index 00000000000..cddb8710acb --- /dev/null +++ b/app/assets/stylesheets/desktop_rtl.scss @@ -0,0 +1,2 @@ +// stub file used to clean up asset precompilation process +@import "desktop"; diff --git a/app/assets/stylesheets/mobile_rtl.scss b/app/assets/stylesheets/mobile_rtl.scss new file mode 100644 index 00000000000..861812af904 --- /dev/null +++ b/app/assets/stylesheets/mobile_rtl.scss @@ -0,0 +1,2 @@ +// stub file used to clean up asset precompilation process +@import "mobile"; diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f49d845b2c4..ca462b38e45 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -106,6 +106,16 @@ module ApplicationHelper current_user.try(:staff?) end + def rtl? + ["ar", "fa_IR", "he"].include?(user_locale) + end + + def user_locale + locale = current_user.locale if current_user && SiteSetting.allow_user_locale + # changing back to default shoves a blank string there + locale.present? ? locale : SiteSetting.default_locale + end + # Creates open graph and twitter card meta data def crawlable_meta_data(opts=nil) diff --git a/app/views/common/_discourse_stylesheet.html.erb b/app/views/common/_discourse_stylesheet.html.erb index 3d1f10818bc..2ccfc92b905 100644 --- a/app/views/common/_discourse_stylesheet.html.erb +++ b/app/views/common/_discourse_stylesheet.html.erb @@ -1,4 +1,8 @@ -<%= DiscourseStylesheets.stylesheet_link_tag(mobile_view? ? :mobile : :desktop) %> +<%- if rtl? %> + <%= DiscourseStylesheets.stylesheet_link_tag(mobile_view? ? :mobile_rtl : :desktop_rtl) %> +<%- else %> + <%= DiscourseStylesheets.stylesheet_link_tag(mobile_view? ? :mobile : :desktop) %> +<%- end %> <%- if staff? %> <%= stylesheet_link_tag "admin"%> diff --git a/lib/sass/discourse_sass_compiler.rb b/lib/sass/discourse_sass_compiler.rb index bde2f84db00..c8f1908245d 100644 --- a/lib/sass/discourse_sass_compiler.rb +++ b/lib/sass/discourse_sass_compiler.rb @@ -63,10 +63,8 @@ class DiscourseSassCompiler } }.merge(debug_opts)).render - # Check if CSS needs to be RTLed after compilation - # and run R2 gem on compiled CSS if true and R2 gem is available css_output = css - if !SiteSetting.allow_user_locale && SiteSetting.default_locale.in?(%w(he ar fa_IR)) + if opts[:rtl] begin require 'r2' css_output = R2.r2(css) if defined?(R2) diff --git a/lib/sass/discourse_stylesheets.rb b/lib/sass/discourse_stylesheets.rb index 668d9b51be0..46452e6b233 100644 --- a/lib/sass/discourse_stylesheets.rb +++ b/lib/sass/discourse_stylesheets.rb @@ -91,8 +91,9 @@ class DiscourseStylesheets end scss = File.read("#{Rails.root}/app/assets/stylesheets/#{@target}.scss") + rtl = @target.to_s =~ /_rtl$/ css = begin - DiscourseSassCompiler.compile(scss, @target) + DiscourseSassCompiler.compile(scss, @target, rtl: rtl) rescue Sass::SyntaxError => e Rails.logger.error "Stylesheet failed to compile for '#{@target}'! Recompiling without plugins and theming." Rails.logger.error e.sass_backtrace_str("#{@target} stylesheet") diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index efba76eff75..c5542307e10 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -78,7 +78,7 @@ task 'assets:precompile:css' => 'environment' do # css will get precompiled during first request instead in that case. if ActiveRecord::Base.connection.table_exists?(ColorScheme.table_name) puts "Compiling css for #{db}" - [:desktop, :mobile].each do |target| + [:desktop, :mobile, :desktop_rtl, :mobile_rtl].each do |target| puts DiscourseStylesheets.compile(target) end end