是否有用于解析getText PO文件的Java库? [关闭
题
有人知道可以让我解析.po文件的Java库吗?我只想创建ID和值的地图,以便可以将它们加载到数据库中。
解决方案
根据 Java GetText实用程序手册 您可以使用PO文件将PO文件转换为ResourceBundle类 msgfmt --java2
使用java.util.resourcebundle或gnu.gettext.gettextresource进行编程并阅读它 - 我认为这是一种最有效的方法。 getText-commons 进行完全相同的操作,包括中间过程创建以调用MSGFMT,因为它的定位如下:
GetText Commons是Java库 使用 gnu getText实用程序.
如果您仍然想要一个Java库,那么我看到的唯一方法是编写自己的库来解析此格式,即从C到Java语言重写MSGFMT源代码。但是我不确定它会比创建流程 +运行C程序更快。
其他提示
我搜索了互联网,也找不到现有库。如果您使用Scala,那么由于其解析器组合功能,您可以自己写解析器非常容易。
称呼 PoParser.parsePo("po file content")
. 。结果是 Translation
.
我已经将此代码放入库中(当然可以由包括Java在内的任何JVM语言使用!):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用于基于Web的翻译软件。
我找到了一些Java课程来读写PO文件: https://launchpad.net/po-parser