Skip to content

Commit

Permalink
Use i18n.json file instead of trans
Browse files Browse the repository at this point in the history
  • Loading branch information
salif committed Oct 31, 2024
1 parent 5c080a1 commit 749fd44
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 129 deletions.
88 changes: 43 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ theme = "linkita"

Also, make sure you have `title` and `default_language` set in your `config.toml` file.

4. Copy translations

Open `themes/linkita/config.toml` and copy all translations to your `config.toml` file.
The English translation is under `[languages.en.translations]`.

Otherwise you will get this error:

```
Error: Reason: Function call 'trans' failed
Error: Reason: Failed to retrieve term translation
```

## Updating

```sh
Expand Down Expand Up @@ -134,7 +122,7 @@ extra:
---
```

### `extra.open_graph` keys
### Open Graph options for pages

| key | type | example | comment |
| --- | --- | --- | --- |
Expand Down Expand Up @@ -202,7 +190,7 @@ profile = "your_username"

Do it for each language in your blog, for example for French, the file name is `content/_index.fr.md`.

#### Profiles for authors
### Profiles for authors

You should add `extra.profiles.author_username` table in your `config.toml` file for each author.
Replace `author_username` with author's username.
Expand All @@ -228,25 +216,35 @@ Configuration options used by this theme.
Most of them are optional. Copy-paste the examples into your `config.toml` file
and comment out the options you don't use instead of setting empty values.

| key | type | comment |
| --- | --- | --- |
| `default_language` | string | The default language |
| `author` | string | The default author for pages |
| `title` | string | The site title |
| `description` | string | The site description |
| `generate_feeds` | boolean | Automatically generated feed |
| `feed_filenames` | array of strings | The filenames to use for the feeds |
| `translations` | table | Copy-paste them from the theme's `config.toml` file |
| `extra` | table | |
| key | type |
| --- | --- |
| `default_language` | string |
| `author` | string |
| `title` | string |
| `description` | string |
| `generate_feeds` | boolean |
| `feed_filenames` | array of strings |
| `extra` | table |

Taxonomies with translated names are `tags`, `categories`, and `authors`.

```toml
# The default language
default_language = "en"

# The default author for pages
author = "your_username"
title = "Site Title"
description = "Site Description"

# The site title
title = ""

# The site description
description = ""

# Automatically generated feed
generate_feeds = true

# The filenames to use for the feeds
feed_filenames = ["atom.xml"] # or ["rss.xml"]
```

Expand Down Expand Up @@ -284,9 +282,6 @@ paginate_by = 5

### `extra`

Strings in `extra.page_info` that are not one of the following, will be displayed directly in the UI:
`"date"`, `"date_updated"`, `"reading_time"`, `"word_count"`, `"authors"`.

| key | type | comment |
| --- | --- | --- |
| `extra.math` | boolean | Enable KaTeX math formula support globally |
Expand All @@ -302,6 +297,9 @@ Strings in `extra.page_info` that are not one of the following, will be displaye
| `extra.goatcounter` | table | Enable web analytics |
| `extra.giscus` | table | The giscus comment options |

Strings in `extra.page_info` that are not one of the following, will be displayed directly in the UI:
`"date"`, `"date_updated"`, `"reading_time"`, `"word_count"`, `"authors"`.

```toml
[extra]
math = false
Expand Down Expand Up @@ -394,10 +392,6 @@ url = "$BASE_URL/atom.xml"

### Open Graph (`extra.profiles[username].open_graph`)

See [the Open Graph protocol](https://ogp.me/).
Some options `config.author` to a real author in the `config.toml` file,
set a neutral profile instead.

| key | type | comment |
| --- | --- | --- |
| `extra.profiles[username].open_graph.image` | string | The URL of social image |
Expand All @@ -412,6 +406,10 @@ set a neutral profile instead.
| `extra.profiles[config.author].open_graph.fb_app_id` | string | Set fb:app_id |
| `extra.profiles[config.author].open_graph.fb_admins` | array of strings | Set fb:admins |

See [the Open Graph protocol](https://ogp.me/).
Some options `config.author` to a real author in the `config.toml` file,
set a neutral profile instead.

```toml
[extra.profiles.your_username.open_graph]
# image = ""
Expand All @@ -437,15 +435,15 @@ fb_admins = ["YOUR_USER_ID"]

### The top menu (`extra.menu`)

`$BASE_URL` will be automatically translated into the language specific base url.

| key | type |
| --- | --- |
| `extra.menu[].url` | string |
| `extra.menu[].name` | string |
| `extra.menu[].names` | table |
| `extra.menu[].names[lang]` | string |

`$BASE_URL` will be automatically translated into the language specific base url.

```toml
[[extra.menu]]
url = "$BASE_URL/projects/"
Expand Down Expand Up @@ -474,10 +472,6 @@ en = "About"

### The page footer (`extra.footer`)

Currently `privacy_policy_url`, `terms_of_service_url`, and `search_page_url` are only used in `<head>`.

`$BASE_URL` is supported in the `_url` options.

| key | type |
| --- | --- |
| `extra.footer.author_name` | string |
Expand All @@ -488,6 +482,10 @@ Currently `privacy_policy_url`, `terms_of_service_url`, and `search_page_url` ar
| `extra.footer.terms_of_service_url` | string |
| `extra.footer.search_page_url` | string |

Currently `privacy_policy_url`, `terms_of_service_url`, and `search_page_url` are only used in `<head>`.

`$BASE_URL` is supported in the `_url` options.

```toml
[extra.footer]
since = 2024
Expand All @@ -500,14 +498,14 @@ license_url = "https://creativecommons.org/licenses/by-sa/4.0/deed"

### Locale and Date format (`extra.locales`)

For date format, see [chrono docs](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html).

| key | type | default value |
| --- | --- | --- |
| `extra.locales[lang].locale` | string | |
| `extra.locales[lang].date_format` | string | `%F` |
| `extra.locales[lang].date_format_archive` | string | `%m-%d` |

For date format, see [chrono docs](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html).

```toml
[extra.locales.en]
locale = "en_US"
Expand All @@ -522,13 +520,13 @@ date_format_archive = "%m-%d"

### Web analytics (`extra.goatcounter`)

Set only if you use [GoatCounter](https://www.goatcounter.com/).

| key | type |
| --- | --- |
| `extra.goatcounter.endpoint` | string |
| `extra.goatcounter.src` | string |

Set only if you use [GoatCounter](https://www.goatcounter.com/).

```toml
[extra.goatcounter]
endpoint = "https://MYCODE.goatcounter.com/count"
Expand All @@ -537,8 +535,6 @@ src = "//gc.zgo.at/count.js"

### Comments (`extra.giscus`)

Only available when `extra.comment` in the frontmatter or `extra.comment` in the config is set to `true`. See [giscus.app](https://giscus.app/).

| key | type | default value |
| --- | --- | --- |
| `extra.giscus.repo` | string | |
Expand All @@ -554,6 +550,8 @@ Only available when `extra.comment` in the frontmatter or `extra.comment` in the
| `extra.giscus.lang` | string | `en` |
| `extra.giscus.loading` | string | `lazy` |

Only available when `extra.comment` in the frontmatter or `extra.comment` in the config is set to `true`. See [giscus.app](https://giscus.app/).

```toml
[extra.giscus]
repo = ""
Expand Down
60 changes: 0 additions & 60 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,66 +59,6 @@ name = "authors"
feed = true
paginate_by = 5

# English translations
[languages.en.translations]
language_name_en = "English"
language_name_bg = "Bulgarian"
language_name_eo = "Esperanto"
taxonomy_tags = "tags"
taxonomy_categories = "categories"
taxonomy_authors = "authors"
footer_powered_by = "Powered by"
footer_and = "and"
footer_zola = "Zola"
footer_linkita = "Linkita"
word_prev_page = "← Prev Page"
word_next_page = "Next Page →"
word_toc = "Table of Contents"
word_minutes = "min"
word_words = "words"
word_more = "More >>"
error_404 = "404"

# Bulgarian translations
[languages.bg.translations]
language_name_en = "английски"
language_name_bg = "български"
language_name_eo = "есперанто"
taxonomy_tags = "етикети"
taxonomy_categories = "категории"
taxonomy_authors = "автори"
footer_powered_by = "Осъществено чрез"
footer_and = "и"
footer_zola = "Зола"
footer_linkita = "Линкита"
word_prev_page = "← Предишна страница"
word_next_page = "Следваща страница →"
word_toc = "Съдържание"
word_minutes = "мин"
word_words = "думи"
word_more = "Още >>"
error_404 = "Грешка 404: Страницата не е намерена!"

# Esperanto translations
[languages.eo.translations]
language_name_en = "Angla"
language_name_bg = "Bulgara"
language_name_eo = "Esperanto"
taxonomy_tags = "etikedoj"
taxonomy_categories = "kategorioj"
taxonomy_authors = "aŭtoroj"
footer_powered_by = "Funkciigita de"
footer_and = "kaj"
footer_zola = "Zola"
footer_linkita = "Linkita"
word_prev_page = "← Antaŭa Paĝo"
word_next_page = "Sekva Paĝo →"
word_toc = "Enhavtabelo"
word_minutes = "min"
word_words = "vortoj"
word_more = "Pli >>"
error_404 = "404"

[extra]
math = false
mermaid = false
Expand Down
87 changes: 87 additions & 0 deletions static/i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"language_name_en": {
"en": "English",
"bg": "английски",
"eo": "Angla"
},
"language_name_bg": {
"en": "Bulgarian",
"bg": "български",
"eo": "Bulgara"
},
"language_name_eo": {
"en": "Esperanto",
"bg": "есперанто",
"eo": "Esperanto"
},
"taxonomy_tags": {
"en": "tags",
"bg": "етикети",
"eo": "etikedoj"
},
"taxonomy_categories": {
"en": "categories",
"bg": "категории",
"eo": "kategorioj"
},
"taxonomy_authors": {
"en": "authors",
"bg": "автори",
"eo": "aŭtoroj"
},
"footer_powered_by": {
"en": "Powered by",
"bg": "Осъществено чрез",
"eo": "Funkciigita de"
},
"footer_and": {
"en": "and",
"bg": "и",
"eo": "kaj"
},
"footer_zola": {
"en": "Zola",
"bg": "Зола",
"eo": "Zola"
},
"footer_linkita": {
"en": "Linkita",
"bg": "Линкита",
"eo": "Linkita"
},
"word_prev_page": {
"en": "← Prev Page",
"bg": "← Предишна страница",
"eo": "← Antaŭa Paĝo"
},
"word_next_page": {
"en": "Next Page →",
"bg": "Следваща страница →",
"eo": "Sekva Paĝo →"
},
"word_toc": {
"en": "Table of Contents",
"bg": "Съдържание",
"eo": "Enhavtabelo"
},
"word_minutes": {
"en": "min",
"bg": "мин",
"eo": "min"
},
"word_words": {
"en": "words",
"bg": "думи",
"eo": "vortoj"
},
"word_more": {
"en": "More >>",
"bg": "Още >>",
"eo": "Pli >>"
},
"error_not_found": {
"en": "404: Page not found!",
"bg": "404: Страницата не е намерена!",
"eo": "404: ne trovita!"
}
}
2 changes: 1 addition & 1 deletion templates/404.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "index.html" %}<!---->
{% block main %}<!---->
<h1 class="absolute inset-x-8 bottom-20 top-0 flex items-center justify-center text-6xl">{{
trans(key="error_404", lang=lang) | default(value="404") }}</h1>
macros::translate(key="error_not_found", language=lang, trans_data=trans_data) }}</h1>
{% endblock main %}
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "macros.html" as macros %}
{%- set trans_data = load_data(path="i18n.json", format="json") -%}
<!DOCTYPE html>
<html class="not-ready overflow-y-scroll lg:text-base" lang="{{ lang }}">
{% include "partials/head.html" %}
Expand Down
10 changes: 10 additions & 0 deletions templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@
{{- profile_name -}}
{%- endif -%}
{%- endmacro get_profile_name -%}

{%- macro translate(key, language, trans_data) -%}
{%- if trans_data[key][language] -%}
{{- trans_data[key][language] -}}
{%- elif trans_data[key]["en"] -%}
{{- trans_data[key]["en"] -}}
{%- else -%}
{{- trans_data[key][language] -}}
{%- endif -%}
{%- endmacro translate -%}
Loading

0 comments on commit 749fd44

Please sign in to comment.