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 --- lib/property_set.rb | 25 +++++++++++++++---------- lib/static_page.rb | 11 ++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) (limited to 'lib') 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