Closed kylerove closed 8 months ago
Heya @kylerove 👋 thanks for reaching out. You can find the source of https://polyglot.untra.io in the site directory with plenty of examples.
My suggestion is to make links to other pages using the <href>
tag and liquid syntax like so:
<a class="nav-link" href="{{ site.baseurl }}/about-us">
for example, this is how I list the pages on the https://polyglot.untra.io site:
give that a try, and let me know if you need any further assistance 👍
Seems that was it. I had "/" as my baseurl. To be consistent with what you showed above, I have moved to keep it empty. Just in case the site ever moves to a subdirectory, though, I am keeping the site.baseurl tag in place in links. Thanks for the quick response! Appreciate it!
I was following the section https://github.com/untra/polyglot#relativized-local-urls that shows several examples. I hadn't thought to look into the site directory itself to see what you were doing. Seems the code is sensitive to presence of the site.baseurl
tag?
So, while all my links are working, it is now adding in the current lang into the language switching dropdown menu link for the default language. English is the default. When I am on a Spanish page, the link to English shows up as /es/code-of-conduct/
instead of expected /code-of-conduct/
. My liquify loop is very similar to your site example. I have added an array in my _config.yml
to associate a caption/label for each language code.
{% for lang in site.languages %}
{% if lang == site.active_lang %}
{% continue %}
{% endif %}
{% assign this_lang = site.languages-captions | where: "language",lang | first %}
{% if lang == site.default_lang %}
<a class="dropdown-item" href="{{ site.url }}{{ site.baseurl }}{{ page.url }}">{{ this_lang.caption }}</a>
{% else %}
<a class="dropdown-item" href="{{ site.url }}{{ site.baseurl }}/{{ lang }}{{ page.url }}">{{ this_lang.caption }}</a>
{% endif %}
{% endfor %}
{{ site.url }}
for the start of the href. All of these should all be relative urls, and the site.baseurl
should default to /
when not specified: https://jekyllrb.com/docs/configuration/options/
relative urls passed into an href will be links for the same site (eg <a href="/about">
on https://polyglot.untra.io links to https://polyglot.untra.io/about
). If you are not specifying a url in your config, this can confuse things. I specify mine, whats yours?permalink
in the frontmatter for the code-of-conduct
page? and that permalink is the same in the english and spanish versions?cheers, Sam
Appreciate the look. See https://github.com/kylerove/pediatricurologybook for the repo. I've tried with and without {{ site.url }}
, realizing it isn't strictly necessary.
The menu code lives in _includes/header.html
.
@kylerove replace this segment: https://github.com/kylerove/pediatricurologybook/blob/master/_includes/header.html#L39-L43 with this (its subtle, just whitespace):
{% if lang == site.default_lang %}
<a class="dropdown-item" href=" {{ site.url }}{{ site.baseurl }}{{ page.url }}">{{ this_lang.caption }}</a>
{% else %}
<a class="dropdown-item" href="{{ site.url }}{{ site.baseurl }}/{{ lang }}{{ page.url }}">{{ this_lang.caption }}</a>
{% endif %}
explanation:
whitespace in the href
wont get picked up by jekyll-polyglot for relativization, but browsers are smart and will handle relative urls with unintended whitespace gracefully. Tbh I'm also not proud of this solution: this is what my language switcher is doing, and I keep forgetting how I got it to work and remembering this subtle hack in html preprocessing.
I hope this works for you! 👍
So after reading your comment, I was like "of course!" I had read that in the documentation. After implementing it, though, same result. I tried without {{ site.url }}
and all sorts of variations with the space and still, it relativizes to the current language.
I'm at a loss as to why this works on your site and not mine.
What if your plugin created its own tag for the current page.url in the default language with some extra code/signature detectable by the relativization code that it could ignore. The sole purpose would be for the language switching menu.
I copied the code from your site sidebar to give me a list of languages and links to current page, and I'm still seeing the same behavior. It is still relativizing the default lang link (en) to the current language.
After battling this all day and even digging into your source and looking at the regex, I confirmed that it does skip hrefs with a space at the beginning, but somehow even with a space in my header.html
file that generates my language menu, it was adding in the language code to the url for my site.default_lang
. This led me to realize that something else was stripping the space. I temporarily removed all plugins except jekyll-polyglot
and jekyll-minifier
. It worked as expected and my url back to site.default_lang
was correct!
After adding plugins back one by one, I found the *%#! culprit: jekyll-spaceship.
I don't have the energy to file a bug report because it is so damn obscure. Sufficed to say, I can get by without the plugin for now. Maybe another day, if need the plugin, I can file a report. Brief scan of existing issues didn't show anyone else with this problem.
Appreciate all your help!
Thank you for this discussion. I found it very helpful.
It seems that polyglot is not relativizing urls as I would expect. I have lang and permalinks specified in the front matter of pages. If I specify links like
[about us](/about-us/)
It will not insert the lang when I'm on the spanish version of the site. (I would expect it to link to/es/about-us/
).Similarly, if I specify the link in html and attempt to include the liquid tag to reference the page (
<a class="nav-link" href="{{ "/about-us/" | relative_url }}">
), it will not insert the lang on an es page. I tried various permutations (without the relative_url) and same result.The only way I could get the lang inserted into links was if I omit the leading slash. The problem then becomes that none of the links outside the root directory are relativized to the current subdirectory (
/es/code-of-conduct/about-us/
instead of/es/about-us/
.How can I troubleshoot this? I tried disabling other plugins, but the outcome was the same. Doesn't appear to be a conflict as far as I can tell.
Otherwise, I this plugin will exactly solve the issue of creating translations for an online book we are writing!