diff --git a/Gemfile b/Gemfile index 91a5baa..07791d5 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem "chartkick" gem 'usagewatch_ext' gem 'ckeditor' gem 'unicorn' +gem 'zhconv' #built in modules eval(File.read(File.dirname(__FILE__) + '/built_in_extensions.rb')) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5117dbd..bdc95ed 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -12,6 +12,15 @@ class ApplicationController < ActionController::Base def set_locale in_use_locales = current_site.in_use_locales + if params[:locale].eql?("zh_cn") or (!params[:locale] and session[:zh_cn]) + redirect_to root_path if !current_site.enable_zh_cn + params[:locale] = "zh_tw" + I18n.locale = session[:locale] + session[:zh_cn] = true + else + session[:zh_cn] = false + end + if params[:locale] session[:locale] = in_use_locales.include?(params[:locale].to_sym) ? params[:locale] : nil end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 77ff834..2f33b6c 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,5 +1,6 @@ # pass the layout=false for not rendering the layouts and also can specify the methods to in the backend controller. # data-layout-content="arrayname" in layouts can be used to render data in the array +require 'zhconv' class PagesController < ApplicationController before_action :get_key @@ -31,7 +32,13 @@ class PagesController < ApplicationController impressionist(page) OrbitHelper.set_params params OrbitHelper.set_site_locale locale - render :html => render_final_page("home",page,true).html_safe + final_page = render_final_page("home",page,true) + if session[:zh_cn] + final_page = ZhConv.convert("zh-cn", final_page) + final_page.gsub!('/zh_tw/','/zh_cn/') + final_page.sub!('繁体中文','繁体中文') + end + render :html => final_page.html_safe end def show @@ -82,7 +89,13 @@ class PagesController < ApplicationController end impressionist(page) - render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe + final_page = render_final_page("#{module_app}/#{params[:target_action]}",page,layout) + if session[:zh_cn] + final_page = ZhConv.convert("zh-cn", final_page) + final_page.gsub!('/zh_tw/','/zh_cn/') + final_page.sub!('繁体中文','繁体中文') + end + render :html => final_page.html_safe else render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 5ee7b88..da1ab52 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -310,11 +310,12 @@ module ApplicationHelper def switch_language(loc) url = request.original_fullpath - if loc != locale - url = url.gsub("/#{locale.to_s}/","/#{loc.to_s}/") - url = url.gsub("locale=#{locale.to_s}","locale=#{loc.to_s}") if url == request.original_fullpath - url = url + "#{url.include?("?") ? "&" : "?"}locale=#{loc.to_s}" if url == request.original_fullpath - end + + locale = url.include?("zh_cn") ? :zh_cn : I18n.locale + + url = url.gsub("/#{locale.to_s}/","/#{loc.to_s}/") + url = url.gsub("locale=#{locale.to_s}","locale=#{loc.to_s}") if url == request.original_fullpath + url = url + "#{url.include?("?") ? "&" : "?"}locale=#{loc.to_s}" if url == request.original_fullpath and !url.include?(locale.to_s) url end diff --git a/app/models/site.rb b/app/models/site.rb index 762893b..6c56469 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -19,6 +19,7 @@ class Site field :mobile_api_openness_on, :type => Boolean, :default => false field :desktop_closed, :type => Boolean, :default => false field :enable_language_detection, :type => Boolean, :default => false + field :enable_zh_cn, :type => Boolean, :default => true field :default_locale, :default => "en" field :mobile_on, :type => Boolean, :default => false field :announcement_category, :type => Array, :default=>[] diff --git a/app/views/admin/sites/preference.html.erb b/app/views/admin/sites/preference.html.erb index 3f5a2c2..8cd10a2 100644 --- a/app/views/admin/sites/preference.html.erb +++ b/app/views/admin/sites/preference.html.erb @@ -156,6 +156,14 @@ +