Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Add LocalizeLinks and custom permalinks for each lang #53

Merged
merged 2 commits into from
Nov 19, 2015

Conversation

jasonlemay
Copy link
Contributor

Hi folks,

Currently, the plugin returns /my_page and /lang/my_page but I needed to produce different urls depending on the language, like /team and /fr/equipe. I added the functionality to do so by adding a variable in the front matter like this :


---
permalink: /team/
permalink_fr: /equipe/

---

When french (fr) pages (or any languages) will be generated, the site will look for permalink_fr and use it if it exists, otherwise it will fallback to the default permalink. In this case, it would generate /team/index.html and /fr/equipe/index.html.

I also created a new function translate_links, returning the correct link for a specified lang or, if not specified, for the current lang.

To use that, pages need to have a unique namespace variable. This might not be the best solution, but as my first time with Ruby and Jekyll, this is what I came up with.


---
layout:         default
namespace:     team
permalink:      /team/
permalink_fr:   /equipe/

---

<a href="{% tl team %}">This link will return /team if we are in the english version of the website and /fr/equipe if it's the french version</a>

<a href="{% tl team fr %}">This link will always return /fr/equipe</a>

You can also create an easy lang switcher like so :

{% if site.lang == "fr" %}
{% capture link %}{{page.namespace}} en{% endcapture %}
    <a href="{% tl {{ link }} %}">En</a>
{% else if site.lang == "en" %}
{% capture link %}{{page.namespace}} fr{% endcapture %}
    <a href="{% tl {{ link }} %}">Fr</a>
{% endif %}

Waiting for your comments so we can make this live, as this would definitely be a nice feature to have.

@jasonlemay
Copy link
Contributor Author

@kimf @kurtsson Hi guys, do you think you could review this, as it would be a nice upgrade. Thanks!

kurtsson added a commit that referenced this pull request Nov 19, 2015
Add LocalizeLinks and custom permalinks for each lang
@kurtsson kurtsson merged commit 6ad3265 into kurtsson:master Nov 19, 2015
@jasonlemay
Copy link
Contributor Author

Thanks, I will do a PR for the Readme ASAP

@kurtsson
Copy link
Owner

Thanks @jasonlemay, I will push this fix as v. 1.3.0

kurtsson added a commit that referenced this pull request Nov 19, 2015
* Support for localized links and custom permalinks, thanks to [@jasonlemay](#53)
  * Support for excluding posts from translation, thanks to [ctruelson](#51)
@jasonlemay jasonlemay mentioned this pull request Dec 9, 2015
@rraallvv
Copy link

rraallvv commented Aug 7, 2018

I'm not sure if I understand correctly, where do I need to add the translated permalink?
Is it for the english or the french page, or both?

permalink: /team/
permalink_fr: /equipe/ # where do I put this?

@hanniabu
Copy link

@rraallvv you would put that in the frontmatter, but unfortunately it doesn't seem to be working for me. Has anybody else ran into issues that they resolved? Maybe I'm making some stupid mistake, but when I use (in this example) {% tl team %} in a page, I receive an error for the tl usage.

@rraallvv
Copy link

@hanniabu Thanks. I migrated my website to WordPress since I was able to find a cheap VPS.

@jasonlemay jasonlemay deleted the localizeLinks_permalinks branch February 17, 2020 21:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants