polyglot icon indicating copy to clipboard operation
polyglot copied to clipboard

Is there a way to link posts in different languages with no similar filename or permalink?

Open streetrunners opened this issue 6 years ago • 6 comments

I would like to link two posts written in different languages like this:

2018-10-09-post-a.md

---
lang: en
ref: my-ref
---
foo

2019-01-01-totally-different-filename.md

---
lang: es
ref: my-ref
---
bar

I don't want to hardcode permalinks in the front matter of each post, I'd rather let Jekyll create them from what I specify in _config.yml. I don't want to name the files the same as I'd like each to have it's own translated title. Is there a way to link them just with this ref variable?

Thanks

streetrunners avatar Jan 15 '19 07:01 streetrunners

I guess you would have to make a patch for that around https://github.com/untra/polyglot/blob/d1a5c8599be53ed859e3f5e3fa43cd094c6936a6/lib/jekyll/polyglot/patches/jekyll/site.rb#L93-L105

vlsi avatar Jan 15 '19 08:01 vlsi

I've somehow managed to make it work with the lib defaults! All my spanish posts are stored in the root _posts folder and all the english ones are stored inside the _posts/en/ folder. I didn't even set a lang variable in the posts' front matter (although I already had a locale one).

So, all in all, what I did was:

  • Install the library
  • Add a lang: es and lang: en variables in both index.html and en/index.html

You can see it working here: https://streetrunners.gitlab.io/

Please check how when accessing a post in the default language and then switching to English from the menu, everything works. Also, the homepage paginates only the posts in the language the user is in.

My only problem is that I cannot access /en/index.html directly, as it looks like it's not processed by Jekyll. So accessing the homepage and then trying to change the language from there doesn't work.

streetrunners avatar Jan 15 '19 08:01 streetrunners

Hi, First of all, thank you for this plugin, it's really great! I've encountered a similar problem, I wanted to generate different pages (translations) from the same file but although the translations worked great, it wasn't possible to translate the permalinks. I've generated the following code to solve it:

module Jekyll
    class Page
    
        #======================================
        # permalink
        #======================================
        def permalink
            return nil if data.nil? 
            # Give precedence to permalink_i18n
            if data['permalink_i18n'] && data['permalink_i18n'][@site.active_lang]
                return data['permalink_i18n'][@site.active_lang]
            end
            # Fall back to permalink
            return data['permalink']
        end
    end
end

And it works if you put this in front matter:

permalink_i18n:
    es: "/permalink_en_español/"
    en: "/permalink_in_english/"

I've learn ruby this week so I don't have the expertise to make a pull request, but maybe you guys could view it and add as a new Feature?

Regards, Sebastian

seblaz avatar Apr 06 '19 16:04 seblaz

permalink_i18n:
    es: "/permalink_en_español/"
    en: "/permalink_in_english/"

Hi @seblaz I love this, however - probably need to adjust it so it automatically adjust the links to the correct lang (as defined here) as this doesn't correctly update the link. Also would need someway to pointing links to a page reference otherwise it'll be complicated to manage all your links in this way.

MPJHorner avatar Dec 10 '19 12:12 MPJHorner

Is this a duplicate of https://github.com/untra/polyglot/issues/33 ?

antoniovazquezblanco avatar Oct 04 '23 12:10 antoniovazquezblanco

This is what page_id is for, isn't it?

george-gca avatar Feb 25 '24 22:02 george-gca