Jacky's Blog Jacky's Blog
  • 首页
  • 关于
  • 项目
  • 大事记
  • 留言板
  • 友情链接
  • 分类
    • 干货
    • 随笔
    • 项目
    • 公告
    • 纪念
    • 尝鲜
    • 算法
    • 深度学习
首页 › 项目 › Go 版本的 Pofile 解析器

Go 版本的 Pofile 解析器

Jacky
6月 24, 2022项目阅读 947
目录
  1. 前言
  2. 安装方式
  3. 功能特色
  4. 类型
  5. CLI 模式

Go 版本的 Gettext Pofile 解析器

前言

尝鲜派在2021年10月中旬开始支持多语言,最早的时候是通过小而美来和翻译者们沟通,每次发版本前都需要提前联系翻译者,且不方便多人协同翻译。到了2022年2月份的时候开始使用 Weblate 作为在线翻译平台,翻译者们只需要在平台上注册账号,即可协作翻译。但是因为前端语言插件的限制,按照默认的写法,只能每次编译前先从 Weblate 里下载 pofile,再编译上线,无法做到翻译在运行时修改,经过了一番改进,前端实现了 Runtime 从后端加载翻译,那么就需要将原来 pofile 通过 nodejs 的解析改为用 Go 进行解析,在搜索了一番开源社区后发现并没有满足我们要求的库,于是就有了 Pofile。

安装方式

go get github.com/0xJacky/pofile

或者从 Release 页面中下载编译好的二进制文件使用 CLI 模式进行解析。

功能特色

  1. 解析 Pofile 的头部参数到一个结构体,提供 Get 方法用于获取自定义的头部;
  2. 解析 Pofile 的元素,支持翻译者注释(Translator Comment),提取注释(Extracted Comment),Reference(引用),flags(标记),msgctxt, msgid,msgid_plural, msgstr 类型;
  3. 自动忽略标记中含有 fuzzy 的元素;
  4. 解析 pofile 为 map[string]interface{} 可以进一步解析为 JSON;
  5. 提供 CLI 模式用于解析 pofile(s)。

类型

  1. Pofile
type Pofile struct {
	Header Header
	Items  []Item
}
  1. Convert pofile to dict
func (p *Pofile) ToDict() (dict Dict)
  1. Entry
func Parse(path string) (p *Pofile, err error)
  1. Pofile Header Struct
type Header struct {
	ProjectIdVersion        string     `key:"Project-Id-Version"`
	ReportMsgBugsTo         string     `key:"Report-Msgid-Bugs-To"`
	POTCreationDate         *time.Time `key:"POT-Creation-Date"`
	PORevisionDate          *time.Time `key:"PO-Revision-Date"`
	LastTranslator          string     `key:"Last-Translator"`
	Language                string     `key:"Language"`
	LanguageTeam            string     `key:"Language-Team"`
	ContentType             string     `key:"Content-Type"`
	ContentTransferEncoding string     `key:"Content-Transfer-Encoding"`
	PluralForms             string     `key:"Plural-Forms"`
}
  1. Visit custom header field
func (h *Header) Get(key string) interface{}
  1. Profile item struct
type Item struct {
	TranslatorComments []string
	ExtractedComments  []string
	Reference          []string
	Flags              []string
	Msgctxt            string
	MsgId              string
	MsgIdPlural        string
	MsgStr             []string
}

CLI 模式

  1. 将单个 pofile 转换为 JSON
./pofile build --file <path-to-pofile>
  1. 转换目录下所有的 pofile 为一个 JSON
./pofile build --file <path-to-dir>
赞(0)
容器监控方案 cAdvisor + Prometheus + Grafana
上一篇
记一次使用 pprof 检查 Go 内存和协程泄漏
下一篇
再想想
暂无评论
近期评论
  • Jacky发表在《Nginx UI》
  • daiwenzh5发表在《Nginx UI》
  • Jacky发表在《Nginx UI》
  • daiwenzh5发表在《Nginx UI》
  • Jacky发表在《Nginx UI》
  • 0
  • 0
Copyright © 2016-2023 Jacky's Blog. Designed by nicetheme.
粤ICP备16016168号-1
  • 首页
  • 关于
  • 项目
  • 大事记
  • 留言板
  • 友情链接
  • 分类
    • 干货
    • 随笔
    • 项目
    • 公告
    • 纪念
    • 尝鲜
    • 算法
    • 深度学习
# Mac # # Apple # # OS X # # iOS # # macOS #
Jacky
PHP C C++ Python | 舞象之年 | 物联网工程
174
文章
169
评论
267
喜欢