getpelican / pelican

Static site generator that supports Markdown and reST syntax. Powered by Python.
https://getpelican.com
GNU Affero General Public License v3.0
12.47k stars 1.81k forks source link

Multilingual index.html / site #942

Closed OE-Tester closed 10 years ago

OE-Tester commented 11 years ago

Hi, I found several issues regarding article translation and also I18N for templates (#92) - I was able to generate translations of articles, bit there seems no way for generation of a multilingual index.html. What is the best practice to have a real multilingual site? I guess I somehow need to modify templates to use LANG specific blocks / code - how to do this best?

Thanks for your attention!

justinmayer commented 11 years ago

What does having a "real multilingual site" mean to you? It's difficult to offer any suggestions without specific details regarding what you want to accomplish.

OE-Tester commented 11 years ago

With that somewhat unclear term I meant full L10N and I18N as you can find in many CMS, however atm I would be happy with a way to have different versions of a site for different languages showing only the content for one language, the I18N Issue #92 discusses this already but I wanted to know which solution is best with current pelican to achieve that?

It looks like right now it is best to create a separate site for each language that use their own translated theme, e.g. under /en and /de - then /index.html contains some javascript that redirects to /en or /de based on browser preferred language (or whatever you like).

But I wanted to know if there is a better way, especially one that avoids usage of a separate theme for each language? Also with this approach I loose the "view article in different language" generated links. But playing around with making one theme work for several language versions seems to need some changes in the generator logic, so I did not dig deeper.

Thanks for your attention!

justinmayer commented 11 years ago

Your explanation certainly provides a better understanding as to what you're looking for, and it's not an uncommon use case. At this time, I'm not aware of a better solution other than the one you've put forth. If you are interested in working on a better way of accomplishing that from with Pelican, I'm sure others in the community would be happy to see such a feature land. (^_^)

jcdubacq commented 10 years ago

I have been doing that for another CMS (dotclear). There are lots of little details : for example, tags should be translated, categories also.

My current plan would be to have two themes differing only by strings (as such, i18n-izable themes would be a bonus). I build one site per language, linking all sources or pdf or images to the same directories (using symbolic links). Links in the navbar allow to switch to a different site. My reasoning is that very few people would be comfortable with reading in different languages one content. Untranslated articles (for example, French articles not yet translated to English) could be dropped in the English site, waiting for a translation (only tags would have to be translated right away, as well as the Category name, if not given by the directory). I use a very small bit of php to set cookies according to preferred language, but pelican could be hacked to insert a .en somewhere for each article (so that no PHP is needed, only content-negociation). So one site for each navigation language (tags, theme, category names), and possibly several languages for one article in any site (copy is easy). This is a bit more hand work than with full automation, but as said, full automation makes the logic more complex, and I prefer simple logic sometimes.

justinmayer commented 10 years ago

Thank you for the input, Jean-Christophe. Is this something you would be willing to work on and help us improve?

w-vi commented 10 years ago

I'm trying to figure this out too, something elegant, without changing the code significantly. So far I'm using the {% if article.translations and article.translations[0].lang == 'X' %} lang Y {% else %} lang X {% endif %} construct to put some language specific bits in the theme, this is enough for me for the moment but having true multi-language support would be great.

smartass101 commented 10 years ago

Hi, I experimented a bit with auto-translating page titles in the menu and here's a working concept:

Necessary part in pelicanconf.py:

TRANSLATE_MENU = True

THEME = "themes/multilingual_menu"

def get_current_lang(gen):
    seq = list(gen)
    return DEFAULT_LANG if len(seq) == 0 else seq[0].lang

def get_best_translation(translations, current_lang, default_p):
    candidate = [ t for t in translations if t.lang == current_lang ]
    return default_p if len(candidate) == 0 else candidate[0]

JINJA_FILTERS = {
    "get_current_lang": get_current_lang,
    "get_best_translation": get_best_translation,
    }

and this little change in base.html where the menu is generated (this is from simple/base.html):

{% if DISPLAY_PAGES_ON_MENU %}
          {% if TRANSLATE_MENU %}
            {% set current_lang = [page, article]|select("defined")|get_current_lang %}
          {% endif %}
          {% for p in PAGES %}
            {% if TRANSLATE_MENU and p.lang != current_lang and p.translations %}
              {% set p = p.translations|get_best_translation(current_lang, p) %}
            {% endif %}
            <li{% if p == page %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ p.url }}">{{ p.title }}</a></li>
          {% endfor %}

However, for a fully i18n capable pelican theme the jinja2.ext.i18n should be used IMHO. This is just a quick fix, but it shows a a simple approach to determining what can be translated.

smartass101 commented 10 years ago

Perhaps a solution would be to use the new i18n_subsites plugin https://github.com/getpelican/pelican-plugins/pull/139

justinmayer commented 10 years ago

Addressed by @smartass101's i18n_subsites plugin. Issue closed.