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

Language selector and absolute path #401

Open
schoubi opened this issue Nov 18, 2024 · 4 comments
Open

Language selector and absolute path #401

schoubi opened this issue Nov 18, 2024 · 4 comments

Comments

@schoubi
Copy link

schoubi commented Nov 18, 2024

Hi,

I'm not quit sure to understand how to made the language selector working as it should.

i'm using

sphinx-build -b html src/doc /var/www/html/fr -D language=fr
sphinx-build -b html src/doc /var/www/html/en -D language=en

to build my translations.

Then my apache cordialy serve /var/www/html/ as https://bla.com/

My sphinx-immaterial is configure as :

        "languages": [
        {
            "name": "English",
            "link": "en/",  # points to ./en/ subdirectory
            "lang": "en",
        },
        {
            "name": "French",
            "link": "fr/",  # points to ./fr/ subdirectory
            "lang": "fr",
        },
    ]
        }

It's correctly generate a drop-down menu with "English" and "French".

So when i go on the english page, https://bla.com/en/ and then i choose French on the drop down menu , it redirect me to https://bla.com/en/fr/, if i choose English, it redirect me to https://bla.com/en/en/

Same on the french page.

I tried html_baseurl on the sphinx side, or site_url on sphinx-immaterial options, but no luck.

Can you point me something i'm missing ?

@vagas87
Copy link

vagas87 commented Dec 16, 2024

I have the same problem. The Website is not looking for the other languages in the configured subdirectories (presumably in the top-level of the html build, where your index.html is), but for subdirectories within the document structure itself. Let's say we have a side example.html on your server:

https://bla.com/path/to/example.html

I would expect to find the corresponding english side under:

https://bla.com/en/path/to/example.html

Instead, this theme (with your configs) tries to load the following link:

https://bla.com/path/to/en/

I played a bit around with a local build myself and found it only to be working, if you config link as en.html and copy the english side with this name beside the original example.html. Although, when I navigate through the doc, it switches back to the default language with every click.

This is incompatible with how sphinx-build builds different languages with -D language=en, because it deploys the whole doc with the same file names, so selecting a subdirectory with the corresponding language tag (like you did) is pretty much mandatory.

Are we missing something here, e.g. using a different way to build the languages?

@2bndy5
Copy link
Collaborator

2bndy5 commented Dec 16, 2024

Try using relative paths in your html_theme_options["languages"][*]["link"]:

html_theme_options = {
    "languages": [
        {
            "name": "English",
            "link": "../en/",  # points to ./en/ subdirectory
            "lang": "en",
        },
        {
            "name": "French",
            "link": "../fr/",  # points to ./fr/ subdirectory
            "lang": "fr",
        },
    ]
}

Now the language selector should point to the proper directory on your server.

Using the language selector

This theme's language button only directs the user to the site's landing page for the specified language (en/index.html or fr/index.html). It does not retain any paths when switching languages (en/example.html -> fr/index.html)

@vagas87
Copy link

vagas87 commented Dec 17, 2024

Thanks for the advice. I had to make some ajustments to make this work:

  1. The links has to be set like ../en/index.html (otherwise I only see the top directory of the language files and not the index.html)

  2. This obviously only works on the index.html page in each language directory for a structure like this:

    root/en/index.html
    root/fr/index.html

    If you select a language in any subdirectory page (e.g. root/en/sub/example.html), this will fail, because there is no such page to redirect (in this case root/en/fr/index.html)

@2bndy5
Copy link
Collaborator

2bndy5 commented Dec 17, 2024

The links has to be set like ../en/index.html (otherwise I only see the top directory of the language files and not the index.html)

Check your server's settings. I think there's usually an option to automatically resolve a directory's path to the index.html contained within.

  • I did not have to add the index.html bit when hosting (with a local-only server) my test-case for this issue.
  • I did experience the directory listing problem when navigating my browser (Firefox) to the built files directly on disk (not using a server).

If you select a language in any subdirectory page (e.g. root/en/sub/example.html), this will fail, because there is no such page to redirect (in this case root/en/fr/index.html)

This is the exact reason why the current implementation only directs to the landing page. I think the upstream mkdocs theme has improved the JS to check existence of a sub-path when switching languages, but we haven't merged #338 yet due to other reasons.

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

3 participants