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

Add i18n support #2

Closed
cs-qyzhang opened this issue Sep 27, 2021 · 1 comment · Fixed by #3
Closed

Add i18n support #2

cs-qyzhang opened this issue Sep 27, 2021 · 1 comment · Fixed by #3

Comments

@cs-qyzhang
Copy link
Contributor

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 %}
  1. 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
Copy link
Owner

justint commented Sep 29, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants