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 @@ +
+ +
+ <%= f.check_box :enable_zh_cn, :class => "toggle-check", data: {disabled: true} %> +
+
+
diff --git a/app/views/orbit_bar/index.html.erb b/app/views/orbit_bar/index.html.erb index 3c58bad..8af67ee 100644 --- a/app/views/orbit_bar/index.html.erb +++ b/app/views/orbit_bar/index.html.erb @@ -83,12 +83,18 @@ <% end %>
  • - <%= t(locale.to_s) %> + <% locale = session[:zh_cn] ? :zh_cn : I18n.locale %> + <%= t((locale==:zh_tw ? :zh_tw_ : locale.to_s)) %>
  • diff --git a/config/locales/en.yml b/config/locales/en.yml index f6524d4..5b3c941 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -23,6 +23,8 @@ en: alternative: Alternative en: English zh_tw: Chinese + zh_tw_: Traditional Chinese + zh_cn: Simplified Chinese _locale: English access: denied: @@ -412,6 +414,7 @@ en: icon: Icon language: Language lang_detection: Enable User Language Detection + enable_zh_cn: Enable Simplified Chinese lang_enabled: Enable Language lang_default: Default Language lang_detection: Enable User Language Detection diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 3694993..a7804e6 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -2,6 +2,8 @@ zh_tw: _locale: 中文 zh_tw: 中文 + zh_tw_: 繁體中文 + zh_cn: 简体中文 en: 英文 access: denied: @@ -405,6 +407,7 @@ zh_tw: backend_open: 開啟後台給所有使用者 change: 更改 classification: 類別 + enable_zh_cn: 開啟簡體中文 frontend_open: "設定後, 前台將會開放給所有使用者." favicon: 偏好圖示 icon: 圖示 diff --git a/config/routes.rb b/config/routes.rb index 3d88f90..141dd97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,7 +37,7 @@ Orbit::Application.routes.draw do locales = Site.first.in_use_locales rescue I18n.available_locales - scope "(:locale)", locale: Regexp.new(locales.join("|")) do + scope "(:locale)", locale: Regexp.new((locales+[:zh_cn]).join("|")) do get 'users/form' => "users#form_page" get 'users/role_page' => "users#role_page"