GetText POファイルを解析するためのJavaライブラリはありますか? [閉まっている
質問
.poファイルを解析できるJavaライブラリを知っている人はいますか? IDと値のマップを作成して、データベースにロードできるようにするだけです。
解決
によると Java getTextユーティリティマニュアル POファイルを使用してResourceBundleクラスに変換できます msgfmt --java2
java.util.resourcebundleまたはgnu.getText.getTextresourceを使用してプログラムして読む - 最も効率的な方法だと思います。 GetText-Commons MSGFMTが次のように配置されているため、中間プロセス作成を含めてまったく同じことをしてください。
GetText CommonsはJava Libraryです 使用します GNUの GetTextユーティリティ.
まだJavaライブラリが必要な場合は、私が見る唯一の方法は、この形式を解析するために独自のライブラリを作成することです。しかし、Process + Run 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プロジェクト GNU GetText PO/POT用のANTLRベースのパーサーが含まれています。 RedhatはWebベースの翻訳ソフトウェアに使用していると思います。
.moパーサー(JavaではなくScala)、マップへの解析: http://scalamagic.blogspot.com/2013/03/simple-getText-parser.html 、 ソース: http://pastebin.com/cswx5sbb
POファイルを読み書きするためのJavaクラスをいくつか見つけました。 https://launchpad.net/po-parser