From f683a831cc361438a35c4df3fc7a07d9eab37109 Mon Sep 17 00:00:00 2001 From: katafrakt Date: Sun, 8 Feb 2015 15:52:26 +0100 Subject: [PATCH] make locale change local to wiki rendering --- lib/wikicloth.rb | 62 +++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/wikicloth.rb b/lib/wikicloth.rb index c0ad932..e096c2c 100644 --- a/lib/wikicloth.rb +++ b/lib/wikicloth.rb @@ -58,43 +58,45 @@ def load(data,p={}) end def render(opt={}) - self.options = { :noedit => false, :locale => I18n.default_locale, :fast => true, :output => :html, :link_handler => self.link_handler, + self.options = { :noedit => false, :fast => true, :output => :html, :link_handler => self.link_handler, :params => self.params, :sections => self.sections }.merge(self.options).merge(opt) self.options[:link_handler].params = options[:params] - I18n.locale = self.options[:locale] - - data = self.sections.collect { |s| s.render(self.options) }.join - data.gsub!(//,"") - data << "\n" if data.last(1) != "\n" - data << "garbage" - - buffer = WikiBuffer.new("",options) - - begin - if self.options[:fast] - until data.empty? - case data - when /\A\w+/ - data = $' - @current_row += $&.length - buffer.add_word($&) - when /\A[^\w]+(\w|)/m - data = $' - $&.each_char { |c| add_current_char(buffer,c) } + locale = self.options[:locale] || I18n.locale + + I18n.with_locale(locale) do + data = self.sections.collect { |s| s.render(self.options) }.join + data.gsub!(//,"") + data << "\n" if data.last(1) != "\n" + data << "garbage" + + buffer = WikiBuffer.new("",options) + + begin + if self.options[:fast] + until data.empty? + case data + when /\A\w+/ + data = $' + @current_row += $&.length + buffer.add_word($&) + when /\A[^\w]+(\w|)/m + data = $' + $&.each_char { |c| add_current_char(buffer,c) } + end end + else + data.each_char { |c| add_current_char(buffer,c) } end - else - data.each_char { |c| add_current_char(buffer,c) } + rescue => err + debug_tree = buffer.buffers.collect { |b| b.debug }.join("-->") + puts I18n.t("unknown error on line", :line => @current_line, :row => @current_row, :tree => debug_tree) + raise err end - rescue => err - debug_tree = buffer.buffers.collect { |b| b.debug }.join("-->") - puts I18n.t("unknown error on line", :line => @current_line, :row => @current_row, :tree => debug_tree) - raise err - end - buffer.eof() - buffer.send("to_#{self.options[:output]}") + buffer.eof() + buffer.send("to_#{self.options[:output]}") + end end def sections