Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
streetrunners opened this issue Jan 15, 2019 · 6 comments

Comments

@streetrunners
Copy link

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

@vlsi
Copy link
Collaborator

vlsi commented Jan 15, 2019

I guess you would have to make a patch for that around

def coordinate_documents(docs)
regex = document_url_regex
approved = {}
docs.each do |doc|
lang = doc.data['lang'] || @default_lang
url = doc.url.gsub(regex, '/')
doc.data['permalink'] = url
next if @file_langs[url] == @active_lang
next if @file_langs[url] == @default_lang && lang != @active_lang
approved[url] = doc
@file_langs[url] = lang
end
approved.values

@streetrunners
Copy link
Author

streetrunners commented Jan 15, 2019

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.

@seblaz
Copy link

seblaz commented Apr 6, 2019

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

@MPJHorner
Copy link

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.

@antoniovazquezblanco
Copy link
Contributor

Is this a duplicate of #33 ?

@george-gca
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants