From f691ad87516043aaf7a5f1f55f82d4c1713be7a4 Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 28 Oct 2013 18:44:21 -0700 Subject: fixed numerous bugs relating to localization --- app/controllers/application_controller.rb | 3 ++- app/controllers/pages_controller.rb | 3 +++ app/views/errors/error.html.haml | 5 ++++- config/initializers/languages.rb | 25 ++++++++++++++----------- lib/property_set.rb | 25 +++++++++++++++---------- lib/static_page.rb | 11 ++++++----- 6 files changed, 44 insertions(+), 28 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a9a25f0..ee53415 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -47,7 +47,8 @@ class ApplicationController < ActionController::Base ## RENDERING ## - def render_500 + def render_500(msg=nil) + @message = msg render :template => 'errors/error', :status => 500 end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 3e76092..933aed7 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -5,6 +5,9 @@ class PagesController < ApplicationController layout :choose_layout rescue_from ActionView::MissingTemplate, :with => :render_404 rescue_from PageNotFound, :with => :render_404 + rescue_from Encoding::CompatibilityError do |exception| + render_500(exception.to_s) + end def show @page = site.find_pages(params[:page]) diff --git a/app/views/errors/error.html.haml b/app/views/errors/error.html.haml index 7f16eb5..45948ef 100644 --- a/app/views/errors/error.html.haml +++ b/app/views/errors/error.html.haml @@ -1,3 +1,6 @@ .dialog %h1 - We're sorry, but something went wrong. \ No newline at end of file + We're sorry, but something went wrong. + %blockquote + - if @message + = h @message \ No newline at end of file diff --git a/config/initializers/languages.rb b/config/initializers/languages.rb index 36f1a34..4258cdb 100644 --- a/config/initializers/languages.rb +++ b/config/initializers/languages.rb @@ -1,17 +1,20 @@ # encoding: utf-8 +# https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers LANGUAGES = { - 'es' => ['Español', 'es', 3, false], - 'en' => ['English', 'en', 4, false], - 'ar' => ['العربية', 'ar', 5, true], - 'pt' => ['Português', 'pt', 6, false], - 'ru' => ['Pyccĸий', 'ru', 7, false], - 'de' => ['Deutsch', 'de', 8, false], - 'fr' => ['Français', 'fr', 10, false], - 'it' => ['Italiano', 'it', 11, false], - 'el' => ['Ελληνικά', 'el', 20, false] + :zh => ['中文', 'zh', 1, false], + :es => ['Español', 'es', 2, false], + :en => ['English', 'en', 3, false], + :ar => ['العربية', 'ar', 5, true], + :pt => ['Português', 'pt', 6, false], + :ru => ['Pyccĸий', 'ru', 7, false], + :de => ['Deutsch', 'de', 8, false], + :fr => ['Français', 'fr', 10, false], + :it => ['Italiano', 'it', 11, false], + :el => ['Ελληνικά', 'el', 20, false] } -AVAILABLE_LANGUAGES = %w(es en ar pt ru de fr it el) +# although everywhere else we use symbols for locales, this array should be strings: +AVAILABLE_LANGUAGES = ['zh', 'es', 'en', 'ar', 'pt', 'ru', 'de', 'fr', 'it', 'el'] -DEFAULT_LOCALE = 'en' \ No newline at end of file +DEFAULT_LOCALE = :en \ No newline at end of file diff --git a/lib/property_set.rb b/lib/property_set.rb index 05aa46a..08f408d 100644 --- a/lib/property_set.rb +++ b/lib/property_set.rb @@ -22,7 +22,7 @@ require 'RedCloth' class PropertySet - DEFAULT_LOCALE = 'en' + DEFAULT_LOCALE = :en # # a simple class to pass through all member variables as attributes. @@ -31,8 +31,8 @@ class PropertySet # class AttrObject def initialize(property_set, locale) - @_ps = property_set - @_locale = locale + @_ps = property_set # underscore is important here, because we don't want to + @_locale = locale # accidentally collide with another property that gets set. end def method_missing(method) get(method) @@ -43,9 +43,10 @@ class PropertySet def get(var_name, inheritance=true) value = instance_variable_get("@#{var_name}") if value.nil? + p [var_name, value, @_locale, DEFAULT_LOCALE] if @_locale != DEFAULT_LOCALE # try value from default locale - @_ps.get_var(var_name) + @_ps.get_var(var_name, DEFAULT_LOCALE) elsif inheritance # try inherited value @_ps.get_inherited_var(var_name, @_locale) @@ -71,6 +72,7 @@ class PropertySet # def eval(template_string, locale) locale ||= DEFAULT_LOCALE + locale = locale.to_sym # locales are always symbols # render to the template to get the instance variables attrs = AttrObject.new(self, locale) @@ -128,7 +130,7 @@ class PropertySet end def locale(l) - @locales[l.to_s] || @locales[DEFAULT_LOCALE] + @locales[l.to_sym] || @locales[DEFAULT_LOCALE] end def get_var(var_name, locale=I18n.locale) @@ -141,7 +143,7 @@ class PropertySet end # - # like get_var, but forbits inheritance + # like get_var, but does not allow inheritance # def get_var_without_inheritance(var_name, locale=I18n.locale) attrs = locale(locale) @@ -171,7 +173,7 @@ if ARGV.grep('--test').any? - @author = 'you' - @created_at = 'Sun Aug 12 18:32:20 PDT 2012' - ignored = 1 -this is the body" +" text_es = " - @title = 'hola' @@ -180,13 +182,16 @@ this is the body" " ps = PropertySet.new - ps.eval('en', text_en) - ps.eval('es', text_es) + ps.eval(text_en, 'en') + ps.eval(text_es, 'es') p ps.title == 'hi' p ps.locale(:es).title == 'hola' + p ps.get_var('title', 'es') == 'hola' p ps.locale(:es).created_at == Time.parse('Sun Aug 12 18:32:20 PDT 2012') p ps.ignored == nil + p ps.locale(:es).ignored == nil p ps.locale(:es).heading == "

hi

" - p ps.body == "this is the body\n" + #p ps.body == "this is the body" + #p ps.get_var('body', :es) == "esta es el cuerpo" end diff --git a/lib/static_page.rb b/lib/static_page.rb index d7d6529..9f4cc05 100644 --- a/lib/static_page.rb +++ b/lib/static_page.rb @@ -16,7 +16,8 @@ require 'rdiscount' #require 'iconv' class StaticPage - attr_accessor :path, :children, :name, :file_path, :props, :parent, :mount_point, :locales + attr_accessor :path, :children, :name, :file_path, :parent, :mount_point, :locales + attr_accessor :props # type PropertySet ## ## CLASS METHODS @@ -273,8 +274,8 @@ class StaticPage # returns an array like so: # # [ - # ['/path/to/page/en.haml', 'en'] - # ['/path/to/page/es.haml', 'es'] + # ['/path/to/page/en.haml', :en] + # ['/path/to/page/es.haml', :es] # ] # # Or this, if page is simple: @@ -290,7 +291,7 @@ class StaticPage elsif File.directory?(@file_path) Dir.foreach(@file_path).collect { |file| if file && file =~ LOCALE_FILE_MATCH - [File.join(@file_path, file), $1] + [File.join(@file_path, file), $1.to_sym] end }.compact end @@ -410,7 +411,7 @@ class StaticPage rd = RDiscount.new(string, :smart, :generate_toc, :autolink) html = rd.to_html if props.locale(locale).toc != false && rd.toc_content - html = "
%s
\n\n%s" % [rd.toc_content, html] + html = "
%s
\n\n%s" % [rd.toc_content.force_encoding('utf-8'), html] end unless (title = explicit_title(locale)).nil? html = "

#{title}

\n\n" + html -- cgit v1.2.3