我安装了i18n gem和插件来本地化日期,但是当我为网站服务时,我收到以下错误:
Malahovas-MacBook-Pro:test Svetlana$ rake
Configuration file: /Users/Svetlana/Desktop/test/_config.yml
Source: /Users/Svetlana/Desktop/test
Destination: /Users/Svetlana/Desktop/test/_site
Generating...
Liquid Exception: Object must be a Date, DateTime or Time object. "2015-12-01" given. in _includes/footer.html, included in _layouts/post.html
jekyll 2.5.3 | Error: Object must be a Date, DateTime or Time object. "2015-12-01" given.
在footer.html中,我有以下代码:
<time datetime="{{ page.date | date: "%Y-%m-%d" }}">
{{ page.date | localize: "%d %B %Y" }}
</time>
以下是插件i18n_filter.rb
的代码:
require 'i18n'
LOCALE = :ru # set your locale
# Create folder "_locales" and put some locale file from
# https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
module Jekyll
module I18nFilter
# Example:
# {{ post.date | localize: "%d.%m.%Y" }}
# {{ post.date | localize: ":short" }}
def localize(input, format=nil)
load_translations
format = (format =~ /^:(\w+)/) ? $1.to_sym : format
I18n.l input, :format => format
end
def load_translations
unless I18n::backend.instance_variable_get(:@translations)
I18n.backend.load_translations Dir[File.join(File.dirname(__FILE__), '../_locales/*.yml')]
I18n.locale = LOCALE
end
end
end
end
这是一个收藏文件。使用--trace
运行构建后,我得到:
Malahovas-MacBook-Pro:test Svetlana$ jekyll build --trace
Configuration file: /Users/Svetlana/Desktop/test/_config.yml
Source: /Users/Svetlana/Desktop/test
Destination: /Users/Svetlana/Desktop/test/_site
Generating...
Liquid Exception: Object must be a Date, DateTime or Time object. "2015-12-01" given. in _includes/footer.html, included in _layouts/post.html
/Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/tags/include.rb:124:in `rescue in render': Object must be a Date, DateTime or Time object. "2015-12-01" given. (Jekyll::Tags::IncludeTagError)
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/tags/include.rb:116:in `render'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/liquid-2.6.1/lib/liquid/block.rb:109:in `block in render_all'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/liquid-2.6.1/lib/liquid/block.rb:96:in `each'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/liquid-2.6.1/lib/liquid/block.rb:96:in `render_all'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/liquid-2.6.1/lib/liquid/block.rb:82:in `render'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/liquid-2.6.1/lib/liquid/template.rb:128:in `render'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/liquid-2.6.1/lib/liquid/template.rb:138:in `render!'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/renderer.rb:92:in `render_liquid'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/renderer.rb:134:in `place_in_layouts'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/renderer.rb:56:in `run'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/site.rb:292:in `block (2 levels) in render'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/site.rb:291:in `each'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/site.rb:291:in `block in render'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/site.rb:290:in `each'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/site.rb:290:in `render'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/site.rb:51:in `process'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/command.rb:28:in `process_site'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/commands/build.rb:56:in `build'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/commands/build.rb:34:in `process'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/lib/jekyll/commands/build.rb:18:in `block (2 levels) in init_with_program'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/mercenary-0.3.3/lib/mercenary/command.rb:220:in `call'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/mercenary-0.3.3/lib/mercenary/command.rb:220:in `block in execute'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/mercenary-0.3.3/lib/mercenary/command.rb:220:in `each'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/mercenary-0.3.3/lib/mercenary/command.rb:220:in `execute'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/mercenary-0.3.3/lib/mercenary/program.rb:35:in `go'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/mercenary-0.3.3/lib/mercenary.rb:22:in `program'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/gems/jekyll-2.5.3/bin/jekyll:20:in `<top (required)>'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/bin/jekyll:23:in `load'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/bin/jekyll:23:in `<main>'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/bin/ruby_executable_hooks:15:in `eval'
from /Users/Svetlana/.rvm/gems/ruby-2.0.0-p451/bin/ruby_executable_hooks:15:in `<main>'
你知道如何解决这个问题吗?
答案 0 :(得分:1)
I18n.localize
或I18n.l
需要Date,DateTime或Time参数。 page.date
实际上应该是DateTime
或Time
类型,但我假设过滤器未加载,因此被Jekyll忽略(我在底部添加了Liquid-filter加载)。这是一个可能的解决方案:
尝试在调用I18n.localize之前解析输入字符串:
假设项目目录中的区域设置文件ru.yml
位于名为./locales/
的文件夹中。 i18n.rb
位于./_plugins/i18n.rb
require 'date'
require 'i18n'
path = File.expand_path("../../locales/ru.yml", __FILE__)
I18n.load_path = Dir[path]
I18n.locale = :ru
module Jekyll
module I18nFilter
# Example:
# {{ post.date | localize: "%d.%m.%Y" }}
# {{ post.date | localize: ":short" }}
def localize(input, format=nil)
format = (format =~ /^:(\w+)/) ? $1.to_sym : format
if input.is_a?(String)
input = DateTime.parse(input)
end
I18n.l input, :format => format
end
def translate(key)
I18n.t key
end
end
end
# Add as a filter
Liquid::Template.register_filter(Jekyll::I18nFilter)
ru.yml
可以是任何有效的yaml文件,包含语言环境,例如:
ru:
hello: приве́т
如果DateTime
是格式错误的字符串,则转换为input
可能会导致错误,例如空的&#34;&#34;或遗漏约会所需的东西。最小值类似于:&#34; 2015-01-13&#34;。
作为Jekyll的替代方案:查看Nanoc,它似乎更新,更容易扩展。它在Github上使用,我们将它用于localization platform PhraseApp
上的所有静态页面