在docker hub里的Ghost这个博客平台的官方镜像默认主题是英文,现在将其本地化(汉化)。

环境:

服务器--阿里云ECS

操作系统--Ubuntu 16.04.2 server

docker版本--17.05.0-ce

docker-compose版本--1.13.0

Web服务器--nginx:1.14(官方镜像)

博客平台--ghost:2.1.1(官方镜像)

准备工作:

1、用 docker-compose 对 ghost 容器进行编排,设置宿主机文件夹到容器内的数据卷映射,就可以在宿主机文件夹里对 ghost 主题文件进行修改和本地化。在容器编排文件 docker-compose.yml 里对 ghost 容器进行配置:

version: "3"
services:
  ghost:
    container_name: "ghost"
    image: "ghost:2.1.1"
    volumes:
      - ./ghost/content:/var/lib/ghost/content
      - ./ghost/config.production.json:/var/lib/ghost/config.production.json
    restart: always 

2、用 docker-compose up -d 启动容器后将 ghost 容器里的 /var/lib/ghost/content 文件夹和 /var/lib/ghost/config.production.json 文件复制到宿主机:

cd ./ghost
sudo docker cp ghost:/var/lib/ghost/content .
sudo docker cp ghost:/var/lib/ghost/config.production.json

3、现在在宿主机里可以进入 ghost 的主题文件夹对官方的默认主题 casper 进行修改了:

cd ./ghost/content/themes
ls
casper 
cd casper

本地化步骤:

1、新建本地化文件

在 casper 主题根目录下创建 locales 目录,把本地化定义文件 zh.json 放在 locales 目录里:

cd casper
mkdir locales
touch locales/zh.json

2、激活本地化语言

在浏览器里进入 ghost 的设置页,进入设置语言界面 Ghost's Settings > General > Publication language 选择对应的本地化文件,本地化文件是 zh.json 就键入 zh

3、复制核心模板到主题文件夹

sudo docker cp ghost:/var/lib/ghost/current/core/server/apps/subscribers/lib/views/subscribe.hbs .
sudo docker cp ghost:/var/lib/ghost/current/core/server/helpers/tpl/pagination.hbs ./partials
sudo docker cp ghost:/var/lib/ghost/current/core/server/helpers/tpl/subscribe_form.hbs ./partials

4、现在可以对主题文件里的模板文件 *.hbs 进行本地化了,过程就是对 *.hbs 文件进行搜索,找到要本地化的文本,使用翻译助手表达式 {{t}} 或者 (t) 把需要本地化的文本包裹起来,比如本地化前:

<a href="{{@blog.url}}">Latest Posts</a>

<span class="reading-time">{{reading_time minute="1 min read" minutes="% min read"}}</span>

本地化后:

<a href="{{@blog.url}}">{{t "Latest Posts"}}</a>

<span class="reading-time">{{reading_time minute=(t "1 min read") minutes=(t "% min read")}}</span>

5、最后一步是将每个翻译助手表达式里的文本复制到本地化文件 zh.json 里进行翻译:

本地化文件 zh.json 的内容:
{
    "Ghost":"博客",
    "HOME":"首页",
    "Back": "返回",
    "Newer Posts": "更新的文章",
    "Older Posts": "更旧的文章",
    "Page {page} of {pages}": "第{page}页,共{pages}页",
    "Subscribe": "订阅",
    "Subscribe to {blogtitle}": "订阅{blogtitle}",
    "Subscribed!": "已经订阅!",
    "with the email address": "在此邮箱",
    "Your email address": "你的email地址",
    "You've successfully subscribed to": "你已经成功订阅了",
    "A collection of posts": "文章集合",
    "A collection of 1 post": "共1篇文章",
    "A collection of % posts": "共%篇文章",
    "A collection of": "共",
    "post":"文章",
    "Get the latest posts delivered right to your inbox": "最新文章直达邮箱",
    "Latest Posts": "最新文章",
    "<a href='{url}'>More posts</a> by {name}": "<a href='{url}'>More posts</a> by {name}",
    "No posts": "没有文章",
    " (Page %)": " (第%页)",
    "Read More": "阅读更多",
    "Read <a href='{url}'>more posts</a> by this author": "阅读此作者的<a href='{url}'>更多文章</a>",
    "See all % posts": "查看所有%篇文章",
    "Share this": "分享",
    "Stay up to date! Get all the latest & greatest posts delivered straight to your inbox": "保持更新!所有最新最好的文章全部送进你的邮箱",
    "This post was a collaboration between": "This post was a collaboration between",
    "youremail@example.com": "youremail@example.com",
    "0 post": "0 篇文章",
    "1 post": "1 篇文章",
    "% post": "1 篇文章",
    "% posts": "% 篇文章",
    "See all % posts": "阅读 % 篇文章",
    "1 min read": "1 分钟阅读",
    "% min read": "% 分钟阅读",
    "aliyun": "阿里云",
    "email": "联系邮箱",
    "host": "主机"
}