kurtsson / jekyll-multiple-languages-plugin

I18n support for Jekyll and Octopress
MIT License
925 stars 202 forks source link

How to put the default language in a subfolder? #86

Open klaasnotfound opened 7 years ago

klaasnotfound commented 7 years ago

This is a fantastic plugin - thank you so much for creating it!

One thing that I was unable to figure out is how to configure the plugin so that all the languages (including the default language) end up in a subfolder. Suppose I have languages ["en", "de"]. English is the default and put into the root folder. However, for consistency I would like to have the English content in the subfolder en/, just like the German content, which resides in de/.

Is this possible?

efkawu commented 7 years ago

Good question, I have got the same issue.

klaasnotfound commented 7 years ago

Since the plugin doesn't seem to support this, I have hacked together a basic solution. I had to learn some Ruby (and the basics of Jekyll plugins) first, so it's probably not the prettiest approach. But it's working.

To use it, you need to install the plugin manually. If you were using the installed version of the plugin before, be sure to remove the old plugin gems entry from your _config.yml, otherwise you're invoking two versions of the plugin and will see errors. Then replace the jekyll-multiple-languages-plugin.rb in the lib folder with this code.

The modified version does three things:

In a nutshell, when using the modified plugin you should be able to run jekyll build on a multilingual site with custom collections and end up with a properly built _site that has each language in its own subfolder, an index.html that does redirects (provided you have a base.html file) and the ability to use the {% tl ... %} and {% tf ... %} tags with correctly localized permalinks.

Note: The modified plugin is currently incompatible with the original (I had to remove and alter some code that relied on the default language being in the root folder), which is why I didn't crate a pull request. As of now, I don't have the time and/or Ruby/Jekyll expertise to reconcile the two approaches, but I'm hoping some of the more experienced contributors can pick it up from here. A basic flag in the _config.yml (e.g. default_lang_in_root_folder) could indicate whether to use one solution or the other.

Etua commented 7 years ago

Definitely in need of out of the box solution. I'd like to use this feature however I don't want to mess with configuring gems manually.

Paludis commented 7 years ago

Same issue here. For consistency I want all languages in their own subfolder.

aumars commented 7 years ago

@klaasnotfound I panicked when I found out that the plugin couldn't support GitHub Pages, you saved me! You should continue fixing the issue and maybe make a pull request.

klaasnotfound commented 7 years ago

@aumars Yeah, that would be the right thing to do... Unfortunately I currently don't have the time or Ruby skills to work on it.

I forgot to mention that I wrote a small blog post on how to use the modified plugin: http://www.klaasnotfound.com/2017/02/16/proper-multilingual-site-with-github-pages-and-jekyll/

It provides a bit more detail on how to set up a new site and what to watch out for when using GitHub Pages and i18n.

mstfacmly commented 5 years ago

@klaasnotfound While I wasn't able to use the plugin, your solution did provide me with what I needed to get a translated website fully working. Thanks!

mkamensky commented 5 years ago

I solved this by symlinking the destination root directory to the default language. I do this with a hook:

Jekyll::Hooks.register :site, :post_write do |site|
  default = site.config['default_lang']
  next unless site.config['lang'] == default

  puts "Linking default language '#{default}':"
  require 'fileutils'
  FileUtils.cd(site.dest, verbose: true) do
    FileUtils.symlink('.', default, verbose: true)
  end
end
antwal commented 5 years ago

hi i have tested but not working with collections or with paginate-v2 plugin; is possible fix it without use base.html? Thanks

maesitos commented 5 years ago

work around for this issue:

Include two times the default language in _config.yml

languages: ["en", "en", "es"]
defaultLang: en

I recommend to include the canonical url in the HTML header to avoid duplicated content:

<!-- CANONICAL -->
{% if page.namespace %}
  {% if site.lang == site.defaultLang %}
    <link rel="canonical" href="{{ site.url }}/{{site.defaultLang}}{% tl {{ page.namespace }} {{ site.lang }} %}" />
  {% else %}
    <link rel="canonical" href="{{ site.url }}{% tl {{ page.namespace }} {{ site.lang }} %}" />
  {% endif %}
{% else %}
  {% if page.url == "/" %}
    <link rel="canonical" href="{{ site.url }}/{{site.lang}}" />
  {% else %}
    <link rel="canonical" href="{{ site.url }}/{{site.lang}}{{page.url}}" />
  {% endif %}
{% endif %}