【问题标题】:Middleman i18n not working中间人 i18n 不工作
【发布时间】:2016-05-08 05:28:11
【问题描述】:

我是使用 ruby​​ 和中间人的新手,我已经创建了我的项目并且一切正常,但是当我转到 /es 路径时,我没有得到任何翻译。我搜索了没有任何结果的信息,并试图在测试配置的文件夹之间移动代码,但什么也没有。

我的文件夹结构是:

|locales
  |---en.yml
  |---es.yml
|source
  |es
    |---index.html.haml
  |layouts
    |---layout.html.haml
  |partials
    |_header.html.haml
    |_navigation.html.haml
  |---index.html.haml

我的 YAML 文件

en.yml

en:
  home: 'Home'

es.yml

es:
  home: 'Inicio'

我的 HAML

%nav
   = link_to t(:home), '/', class: "#{'active' if current_page.url == '/'}"
   = link_to 'Portfolio', '/portfolio', class: "#{'active' if current_page.url == '/portfolio/'}"
   = link_to t(:skills), '/skills', class: "#{'active' if current_page.url == '/skills/'}"
   = link_to t(:about), '/about', class: "#{'active' if current_page.url == '/about/'}"
   = link_to t(:contact), '/contact', class: "#{'active' if current_page.url == '/contact/'}"

我的配置

config.rb

###
# Page options, layouts, aliases and proxies
###

# Per-page layout changes:
#
# With no layout
page '/*.xml', layout: false
page '/*.json', layout: false
page '/*.txt', layout: false

# With alternative layout
# page "/path/to/file.html", layout: :otherlayout

# Proxy pages (http://middlemanapp.com/basics/dynamic-pages/)
# proxy "/this-page-has-no-template.html", "/template-file.html", locals: {
#  which_fake_page: "Rendering a fake page with a local variable" }

# General configuration
set :partials_dir, 'partials'
activate :i18n, :templates_dir => 'partials'
activate :directory_indexes

# Reload the browser automatically whenever files change
configure :development do
  activate :livereload
end

###
# Helpers
###

# Methods defined in the helpers block are available in templates
# helpers do
#   def some_helper
#     "Helping"
#   end
# end

# Build-specific configuration
configure :build do
  # Minify CSS on build
  activate :minify_css

  # Minify Javascript on build
  activate :minify_javascript
end

【问题讨论】:

  • 您介意分享您的代码吗?

标签: ruby-on-rails rails-i18n middleman


【解决方案1】:

我无法写评论,但我认为这可能是原因,你的 es.yml 是错误的,因为它以 en 开头:

 en:
    home: 'Inicio'

不应该吗

 es:
    home: 'Inicio'

【讨论】:

  • 对不起,我放代码时出错了。在es.yml 默认为es:
【解决方案2】:

我知道这个问题已经有几个月的历史了,但我只是遇到了同样的问题,在网上找了几个小时试图找到并回答并设法通过在激活 i18n 后添加这些参数来解决问题:

config.rb

configure :build do
   activate :i18n,
      :mount_at_root => 'en',
      :lang_map => { :'en' => 'en', :'es' => 'es' },
      :path => '/'
end

显然,如果您希望 "es" 成为您的默认设置,请更改 mount_at_root

希望这会有所帮助。

【讨论】:

  • 您好,感谢您的回答!这个问题在我的旧项目中仍然有效。我会查一下! ;)
【解决方案3】:

我通过以下方式实现了具有单独的英语和西班牙语 URL 的本地化路径
在源目录的根目录中添加 index.es.html.erb
并在 config.rb 中设置 activate :i18n, :path => "/:locale/"

在浏览器中,我的语言选择器将用户发送到 //es

英语

http://website.com/

西班牙语

http://website.com/es

文件夹结构

|data
  |---home.json
|locales
  |---en.yml
  |---es.yml
|source
  |---index.html.erb
  |---index.es.html.erb
  |---_slide.erb
|---config.rb

config.rb

configure :build do
  activate :i18n, :path => "/:locale/"
  activate :directory_indexes
  ...
end

slide.erb
使用 t 作为 I18n.t 的快捷方式,我通过数据动态引用翻译后的值。

<%= link_to t([data.link.text]),
    data.link.href,
    :id => data.link.id,
    :class => 'btn btn-primary btn-lg btn-dark'
%>

home.json
"text" 的值与 .yml 文件中的键相关。

{
  "slides": [
    {
      "text": "slides.learnMore",
      ...
    },
    ...
  ]
}

en.yml

en:
  slides:
    learnMore: "LEARN MORE"
    ...

es.yml

es:
  slides:
    learnMore: "APRENDE MÁS"
    ...

【讨论】:

    【解决方案4】:

    将需要每种语言复制的所有.erb.html 移动到文件夹:/source/localizable,如文档中所述:

    您可以使用修饰符更改此文件夹名称:templates_dir:

    # Look in `source/language_specific` instead
    activate :i18n, :templates_dir => "language_specific"
    

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多