I18n string translation and frontmatter

Hi,

I’m still learning to properly use I18n (and Middleman), so please have understanding. I’m using frontmatter in a mypage.html.erb to send page-title to the layout.erb, using title: My Page Title in page’s frontmatter and <title><%= current_page.data.title %></title> in my layout code. This is a correct way, right?

Now, I’m using I18n to localize my page to several languages. If I use the entire-template-localizing approach this is fine, since each page.en.erb / page.de.erb / contains its own localized frontmatter. But if I don’t want to use entire-template-localizing approach and use en.yml / de.yml instead, then how do I deliver page-title to the layout? Is there a way to use t(:symbol) in frontmatter?

The question is more general, because I use frontmatter not only for the title tag but also to set description metadata, menu title for navigation (sometimes must be shorter than the full page title), etc.

Hello again. No answer? :smile: My problem seems to be very general so did I get something wrong or is there some ommision in Middleman design? I’m betting this is my misunderstanding of how to use it.
Thanks in advance!

OK, so the workaround for this is the following:

\locales\de.yml contains:

page1_title: "Seite 1 Titel"
page2_title: "Seite 2 Titel"
…
page1_descr: "Beschreibung der Seite 1"
page2_descr: "Beschreibung der Seite 2"
…

Then the page1.html.erb begins with:

---
(frontmatter items, if any)
---
<% content_for :head_assets do %>
   <%= content_tag :title, t(:page1_title) %>
   <%= tag :meta, :name => 'description', :content => t(:page1_descr) %>
<% end %>

Layout file layout.erb contains:

<html>
   <head>
   <%= yield_content :head_assets %>
  …

To make it more DRY the content_for blok should be extracted from page1/page2/… to a helper, like this (\helpers\custom_helpers.rb):

module CustomHelpers
  def header_assets(title, descr)
    s1 = content_tag :title, t(title)
    s2 = tag :meta, :name => 'description', :content => t(descr)
    return [s1, s2].join("\n")
  end
end

This helper looks ugly because it contains too much presentation layer (HTML through tag helpers), that’s why I call it a workaround, not a solution. But it works, I’ve tested it on a new, empty project (middleman 3.3.2).

Page code looks less ugly after DRYing:

<% content_for :head_assets do %>
   <%= header_assets(:page1_title, :page1_descr) %>
<% end %>

I know it is a bit late…but anyway

I wrote this helper (Middleman v4):

def draw_page_title
['site.title', ("layout.#{current_resource.data.title}" if current_resource.data.title)].reject(&:blank?).map{ |element| I18n.t(element) }.join(': ') 
end

with locales/en.yml:

---
en:
   site: "My site"
   layout:
      index: Home
      about: About
      work: Work
      contact: Contact

and layout.erb:

<title><%= draw_page_title %></title>

I hope this helps someone.