Middleman-transpath
You're using i18n for your middleman website ? This extension is for you !
Why ?
Translating path with middleman, or access to the same page but different locale and slug is not integrate by default in middleman-i18n.
For example, you cannot access from :
/a-propos
to /en/about
Install
Add the following line to your Gemfile: gem 'middleman-transpath'
and this to your config.rb: activate :transpath
Solution ?
Yep, easily. Install and configure i18n with your favorite locales, and don't forget to specify your default locale (for example : activate :i18n, :mount_at_root => :fr
in your config.rb, to use French as default locale), check the documentation
Configuration
By default, this is the following configuration:
t.label = {
en: 'πΊπΈ - EN',
fr: 'π«π· - FR'
}
You can add others locales, or rename these, just like this in your config.rb:
activate :transpath do
t.label = {
en: 'EN',
fr: 'FR',
it: 'IT'
}
end
Path configuration
Add a slug on your page frontmatter.
---
slug: about
---
Then, you'll need your locales files (/locales/LANG_SYMBOL.yml). For each locale, you have to set, one by one (not in a list):
- The title translation in titles
- The slug translation in paths
titles:
about: 'A propos' # Page title
paths:
about: 'a-propos' # URL slug
If you want to see an example, go to the wiki section
Subdirectories
If you have subdirectories, like /projects/project-one, it's not a problem, set in your locales files:
paths:
projects: 'projects'
project-one: 'project-one'
and on your page'frontmatter:
slug: project-one
dir:
- projects
You need to add all slugs, even if it's not a page. For example if you have a page like /about/me, and about is not a page, you need to set a translation for about anyway.
Dynamic pages
When you generate dynamic pages using proxy, add :data => { :slug => project.en.slug }
. You'll have to generate it on multiple languages like this:
data.projects.each do |project|
proxy "/projets/#{project.fr.slug}/index.html", "templates/project.html", :locals => { :project => project }, :locale => :fr, :ignore => true, :data => { :slug => project.fr.slug }
proxy "en/projects/#{project.en.slug}/index.html", "templates/project.html", :locals => { :project => project }, :locale => :en, :ignore => true, :data => { :slug => project.en.slug }
end
Then, the frontmatter config is a bit difficult:
---
dynamic:
enable: true
data: project # The data you send using locals in your proxy
name: name # What you want to use for your page title (here: project.name)
slug: slug # What you want to use as slug (here: project.slug)
dir:
- projects # You'll can access through /projects/the-project-slug
---
The data in the data/projects.yml looks like this:
- fr:
name: Premier projet
slug: premier-projet
en:
name: First Project
slug: first-project
AAAAAAH! I need a complete example!
Okok, just go here, to get everyting completely detailled:
Helper to translate the current page
Simply use the following helper to translate your current page in another language. Replace the :fr by the locale symbol of your choise.
<%= link_translate(:fr) %>
You don't want to use your default label? Do the following:
<%= link_translate(:fr, {label: 'FR - test'}) %>
Helper to translate the page title
Add title_translate
in your layout <title>
. It'll use the title you specified in frontmatter, automaticly.
There's a description_translate
helper too, to translate the description.
Contributing
- Fork it ( https://github.com/bastienrobert/middleman-transpath/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
License
See LICENSE, which applies to everything in this repository.