Commit 1c2c79de authored by Peter Serwylo's avatar Peter Serwylo

I18n titles of documents if they are "." separated strings.

For example, if the frontmatter of a document is:

```
---
title: "This is a title"
---
```

Then it will render as "This is a title" as expected. However, if it is:

```
---
title: strings.page_one.heading
---
```

Then it will look for the file in `_data/strings.json` for the following
entry:

```
{
  "page_one": {
    "heading": "This is a heading",
    ...
  },
  ...
}
```

and the resulting heading will be "This is a heading". If the resulting
JSON entry cannot be found, it is presumed that it is not actually
trying to i18n the title and will instead use whatever it found in the
frontmatter (e.g. "not.json" will result in a title of "not.json").

The rationale for this is that the polyglot plugin doesn't support i189n
titles, because they rightly point out that you can [do this kind of
string lookup quite trivially in a liquid
template](https://github.com/untra/polyglot/issues/34#issuecomment-252774718).
However, that doesn't take into account the fact that page titles appear
elsewhere, such as in pagination pages. This would require the same
logic to appear everywhere a document title could be rendered.

Additionally, we already have a process for translating
post/documentation page titles, where the title is translated in weblate
right next to the document content. It would be annoying to say the
least if people translated documents in the "Docs" weblate project (a
.po based project), but then had to go to a different (JSON based)
project in order to translate the titles.
parent 09812fa4
......@@ -5,6 +5,7 @@
"option_x_description": "Option [number]: [description]"
},
"add_repo": {
"title": "How to Add a Repo to F-Droid",
"intro": "You can add a repo to F-Droid by scanning the repo's QR code from another device or by opening a repo link on your phone. Begin by downloading and installing the F-Droid app on your Android phone.",
"open_repo_link": {
"short_summary": "Open the Repo Link",
......@@ -38,6 +39,7 @@
"no_internet": "No internet? No problem. Learn <a href=\"../swap/\">How to Send and Receive Apps Offline.</a>"
},
"swap": {
"title": "How to Send and Receive Apps Offline",
"intro": "No internet? No problem. Download apps from people near you.",
"all_devices_need_fdroid": "All devices need F-Droid downloaded and installed before beginning. All devices should follow the steps below.",
"step_1": "Open F-Droid. Tap 'Nearby' from the menu at the bottom of your screen.",
......@@ -53,6 +55,7 @@
"related_tutorial": "You may also be interested in: <a href=\"../create-repo/\">How to Create a Repo</a>"
},
"create_repo": {
"title": "How to Create a Repo",
"related_tutorial": "You may also be interested in: <a href='../add-repo/'>How to Add a Repo to F-Droid</a>",
"intro": "Can't find what you're looking for? Why not create your own customized collection of apps and files using Repomaker. Download the Repomaker web app on your computer to get started.",
"download_and_install": {
......
#
# If a document title is a "." separated string, which matches an entry in the site.data hash, then it will be translated
# to the matching value.
#
Jekyll::Hooks.register :site, :post_read do |site|
site.documents.each do |document|
parse_title(document)
end
end
def parse_title(document)
site = document.site
title = document.data["title"]
# Title has at least one word, followed by a dot, followed by more words and dots.
if (title =~ /^[\w\-]+\.[\w\-.]+$/) == nil
return
end
title_parts = title.split(/\./)
new_title = recursively_get_value(site.data, title_parts)
if new_title != nil and new_title.length > 0
Jekyll::logger.debug "i18n", "Translating document title #{title} to #{site.active_lang}: \"#{new_title}\""
document.data["title"] = new_title
end
end
#
# Data is a hash with string keys, and each value is either another hash, or a string value, e.g:
#
# { "one" => { "two" => "Two"} }
#
# Keys is an array of strings, which represent successive indexes into the data hash, e.g:
#
# [ "one", "two" ]
#
# Will recursively search the data hash for a matching key, then return that value, or nil if not found.
#
def recursively_get_value(data, keys)
key = keys[0]
if data != nil and data.key? key
if data[key].is_a? String
if keys.length == 1
return data[key]
else
return nil
end
else
return recursively_get_value(data[key], keys[1..keys.length])
end
end
end
\ No newline at end of file
---
layout: tutorial
title: How to Add a Repo to F-Droid
title: tutorials.add_repo.title
---
<!-- Don't have an empty line until after the first <p>, as Jekyll is
looking for "\n\n" to decide what constitutes the "excerpt" for a post. -->
......
---
layout: tutorial
title: How to Create a Repo
title: tutorials.create_repo.title
---
<!-- Don't have an empty line until after the first <p>, as Jekyll is
looking for "\n\n" to decide what constitutes the "excerpt" for a post. -->
......
---
layout: tutorial
title: "How to Send and Receive Apps Offline"
title: tutorials.swap.title
---
<!-- Don't have an empty line until after the first <p>, as Jekyll is
looking for "\n\n" to decide what constitutes the "excerpt" for a post. -->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment