C'è una libreria Java per analizzare file gettext PO? [chiuso]
Domanda
Qualcuno sa di una libreria Java che mi lascia analizzare i file .po? Voglio semplicemente per creare una mappa di ID e valori in modo che io possa caricare in un database.
Soluzione
Java gettext utilità Manuale si può convertire PO il file a una classe ResourceBundle utilizzando il programma msgfmt --java2
e leggerlo usando java.util.ResourceBundle o gnu.gettext.GettextResource - suppongo che sia un modo più efficiente. Gettext-commons fare esattamente lo stesso compresa creazione processo intermedio di chiamata msgfmt perché è posizionata come segue:
Gettext Commons è libreria Java che fa uso di GNU gettext utilities .
Se si vuole ancora esattamente una libreria Java allora l'unico modo che vedo è quello di scrivere la propria libreria per l'analisi di questo formato cioè riscrittura msgfmt codice sorgente da C al linguaggio Java. Ma io non sono sicuro che sarà più veloce di creare processo di programma + corsa C.
Altri suggerimenti
Ho cercato su internet e non riusciva a trovare una libreria esistente, sia. Se si utilizza Scala, è abbastanza facile scrivere un parser da soli, grazie alla sua funzione di parser combinatore.
PoParser.parsePo("po file content")
Chiama. Il risultato è una lista di Translation
.
Ho fatto questo codice in una libreria (può essere utilizzato da tutte le lingue JVM, tra cui Java, ovviamente!): 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 è l'unico che ho trovato mentre facendo qualche ricerca qualche tempo fa.
Il href="https://github.com/zanata/tennera" rel="nofollow"> tennera progetto contiene un interprete basato su ANTLR per GNU Gettext PO / POT. Credo che viene utilizzato da RedHat per un software di traduzione basato sul web.
.MO parser (non Java, ma Scala), analizza in mappa: http://scalamagic.blogspot.com/2013/03/simple-gettext-parser.html , fonte: http://pastebin.com/csWx5Sbb
Ho trovato alcune classi Java per leggere e scrivere file PO: https://launchpad.net/po-parser