untra / polyglot

:abc: Multilingual and i18n support tool for Jekyll Blogs
https://polyglot.untra.io
MIT License
417 stars 60 forks source link

Not relativizing urls #116

Closed kylerove closed 8 months ago

kylerove commented 4 years ago

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!

untra commented 4 years 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 👍

kylerove commented 4 years ago

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?

kylerove commented 4 years ago

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 %}
untra commented 4 years ago

cheers, Sam

kylerove commented 4 years ago

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.

untra commented 4 years ago

@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! 👍

kylerove commented 4 years ago

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.

kylerove commented 4 years ago

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.

kylerove commented 4 years ago

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!

elijh commented 4 years ago

Thank you for this discussion. I found it very helpful.