是否有用于解析gettext PO文件的Java库?

有谁知道一个Java库,它会让我解析.PO文件? 我只是想创建一个ID和值的Map,以便我可以将它们加载到数据库中。

根据Java gettext实用手册,您可以使用msgfmt --java2程序将PO文件转换为ResourceBundle类,并使用java.util.ResourceBundle或gnu.gettext.GettextResource读取它 – 我认为这是一种最有效的方法。 Gettext-commons完全相同,包括调用msgfmt的中间进程创建,因为它的位置如下:

Gettext Commons是使用 GNU gettext实用程序的 Java库。

如果您仍然想要一个完整的Java库,那么我看到的唯一方法就是编写自己的库来解析这种格式,即将msgfmt源代码从C语言重写为Java语言。 但我不确定它会比创建进程+运行C程序更快。

我搜索了互联网,但也找不到现有的图书馆。 如果你使用Scala,由于它的解析器组合器function,你自己编写解析器非常容易。

调用PoParser.parsePo("po file content") 。 结果是Translation列表。

我已将此代码放入库中(当然可以由任何JVM语言使用,包括Java!): https : //github.com/ngocdaothanh/scaposer

 import scala.util.parsing.combinator.JavaTokenParsers trait Translation case class SingularTranslation( msgctxto: Option[String], msgid: String, msgstr: String) extends Translation case class PluralTranslation( msgctxto: Option[String], msgid: String, msgidPlural: String, msgstrNs: Map[Int, String]) extends Translation // http://www.gnu.org/software/hello/manual/gettext/PO-Files.html object PoParser extends JavaTokenParsers { // Removes the first and last quote (") character of strings // and concats them. private def unquoted(quoteds: List[String]): String = quoteds.foldLeft("") { (acc, quoted) => acc + quoted.substring(1, quoted.length - 1) } // Scala regex is single line by default private def comment = rep(regex("^#.*".r)) private def msgctxt = "msgctxt" ~ rep(stringLiteral) ^^ { case _ ~ quoteds => unquoted(quoteds) } private def msgid = "msgid" ~ rep(stringLiteral) ^^ { case _ ~ quoteds => unquoted(quoteds) } private def msgidPlural = "msgid_plural" ~ rep(stringLiteral) ^^ { case _ ~ quoteds => unquoted(quoteds) } private def msgstr = "msgstr" ~ rep(stringLiteral) ^^ { case _ ~ quoteds => unquoted(quoteds) } private def msgstrN = "msgstr[" ~ wholeNumber ~ "]" ~ rep(stringLiteral) ^^ { case _ ~ number ~ _ ~ quoteds => (number.toInt, unquoted(quoteds)) } private def singular = (opt(comment) ~ opt(msgctxt) ~ opt(comment) ~ msgid ~ opt(comment) ~ msgstr ~ opt(comment)) ^^ { case _ ~ ctxto ~ _ ~ id ~ _ ~ s ~ _ => SingularTranslation(ctxto, id, s) } private def plural = (opt(comment) ~ opt(msgctxt) ~ opt(comment) ~ msgid ~ opt(comment) ~ msgidPlural ~ opt(comment) ~ rep(msgstrN) ~ opt(comment)) ^^ { case _ ~ ctxto ~ _ ~ id ~ _ ~ idp ~ _ ~ tuple2s ~ _ => PluralTranslation(ctxto, id, idp, tuple2s.toMap) } private def exp = rep(singular | plural) def parsePo(po: String): List[Translation] = { val parseRet = parseAll(exp, po) if (parseRet.successful) parseRet.get else Nil } } 

gettext-commons是我在一段时间做一些研究时发现的唯一一个。

github上的tennera项目包含一个基于ANTLR的GNU Gettext PO / POT解析器。 我认为它被Redhat用于基于网络的翻译软件。

.MO解析器(不是Java,而是Scala),解析为Map: http ://scalamagic.blogspot.com/2013/03/simple-gettext-parser.html,来源: http : //pastebin.com/csWx5Sbb

我找到了一些java类来读写po文件: https : //launchpad.net/po-parser