justint / papaya

A clean Zola theme for blogging and projects.
https://justintennant.me/papaya/
MIT License
45 stars 24 forks source link

Add i18n support #2

Closed cs-qyzhang closed 2 years ago

cs-qyzhang commented 2 years ago

Hi, I'd like to add i18n support to this clean theme. I'm a newborn to Rust/Zola/Tera, so my knowledge is limited. I found three ways to do that:

  1. Add a base template for every language, in these templates assign corresponding text to variables, and then extends one of them in base.html, replace every language-specific text with corresponding variable. One problem is variable defined in base template cannot used inside macro, so variables can only be passed into macro by using additional parameters. Another problem is the path of extends keyword must be static, so in order to switch language it seems must change the path of extends keyword instead of changing config.toml.
  2. Define language-specific text in one macro, e.g. i18n::text(key), then use i18n::text(key="recent_projects") to get that text. This method seems better than method 1, but the macro definition is unelegent and will be very huge.
    {% macro text(key) %}
    {% if config.extra.lang == "en" -%}
    {% if key == "recent_projects" -%}
      Recent projects
    {% elif key == "recent_blog_posts" -%}
      Recent blog posts
    {% elif key == "about" -%}
      About
    {% endif -%}
    {% elif config.extra.lang == "zh" -%}
    {% if key == "recent_projects" -%}
      近期项目
    {% elif key == "recent_blog_posts" -%}
      近期博文
    {% elif key == "about" -%}
      关于
    {% endif -%}
    {% endif -%}
    {% endmacro %}
  3. Define langauge-specific text in config.toml. This method is the simplest way, gives user more control of language-specific text, but the config.toml will be large.
justint commented 2 years ago

Hey, thanks for expressing your interest in adding i18n to Papaya!

I'm fairly new to Zola as well, so I checked how other themes have accomplished this, plus what the Zola documentation says.

I've discovered that Zola has a pre-existing trans() Tera function that works exactly as your second idea suggests. The translation keys are defined in the config.toml.

Would this suit your needs? If so, I could refactor my code to utilize the trans() functions and migrate my English keys into the config.toml.