.PO ファイルを解析できる Java ライブラリを知っている人はいますか? ID と値のマップを作成して、それらをデータベースにロードできるようにしたいだけです。
6 に答える
インターネットを検索しましたが、既存のライブラリも見つかりませんでした。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
}
}
Java gettext ユーティリティ マニュアルによると、プログラムを使用して PO ファイルを ResourceBundle クラスに変換し、msgfmt --java2
java.util.ResourceBundle または gnu.gettext.GettextResource を使用して読み取ることができます。これが最も効率的な方法だと思います。Gettext-commonsは、次のように配置されているため、msgfmt を呼び出すための中間プロセスの作成を含め、まったく同じことを行います。
Gettext Commons は、GNU gettext ユーティリティを利用するJava ライブラリです。
それでも正確に Java ライブラリが必要な場合は、この形式を解析するための独自のライブラリを作成する、つまり msgfmt ソース コードを C 言語から Java 言語に書き直すしかないと思います。ただし、プロセスの作成+ Cプログラムの実行よりも高速になるかどうかはわかりません。
gettext-commonsは、しばらく前に調査を行っているときに見つけた唯一のものです。
githubのtenneraプロジェクトには、GNU Gettext PO/POT用のANTLRベースのパーサーが含まれています。RedhatがWebベースの翻訳ソフトウェアに使用していると思います。
.MO パーサー (Java ではなく Scala)、解析して Map : http://scalamagic.blogspot.com/2013/03/simple-gettext-parser.html、ソース: http://pastebin.com/csWx5Sbb
po ファイルを読み書きする Java クラスをいくつか見つけました: https://launchpad.net/po-parser