Skip to content

Instantly share code, notes, and snippets.

@yu-tang
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yu-tang/1c982199216089483641 to your computer and use it in GitHub Desktop.
Save yu-tang/1c982199216089483641 to your computer and use it in GitHub Desktop.
翻訳Atlas の対訳ファイル(.trc)を TMX ファイルに変換する OmegaT 用スクリプト
/** :name=.trc を TMX に変換 :description=翻訳Atlas の対訳ファイル(.trc)を TMX ファイルに変換する
*
* 翻訳Atlas の対訳ファイル(.trc)を TMX ファイルに変換する OmegaT 用スクリプト
*
* .trc のデータ形式は、下記を参照した。
*
* ATLAS で gettext PO ファイルの翻訳をしたい その2
* http://ta-memo.blogspot.jp/2011/05/atlas-gettext-po-2.html
*
* 使い方:
* 設定行の TRC ファイルのパスを実際のパスに書き換え、スクリプトを実行する。
* 成功すれば、TRC ファイルと同じ場所に同名の TMX ファイルが出力される。
* 出力された TMX ファイルを任意の OmegaT プロジェクトの TM フォルダーに
* 移動すると、参考訳文に表示されるようになる。
*
* 注意点:
* ・言語は今のところ「英→日」固定。
* ・作成者IDを変更したい場合は、「from.creator = 'trc2tmx'」の行を書き換える。
* ・TMX ファイルの配置は tm/mt/ 配下を推奨(tm フォルダーの下に mt サブフォルダー
* を作成し、その中に置く)。そうすると、機械翻訳由来の参考訳文を挿入した場合は
* 既定で赤い背景色が付いて、識別しやすくなる(必要なければ、そうしなくても良い)。
*/
import org.omegat.core.data.PrepareTMXEntry
import org.omegat.core.data.TMXEntry
import org.omegat.util.Language
import org.omegat.util.TMXWriter2
// 設定
def TRC_FILE_NAME = /C:\foo\bar.trc/
// ファイルから、翻訳単位(原文と訳文)を抽出
def trc = new File(TRC_FILE_NAME)
def tmx = getTMXFile(trc)
def map = [:]
final String EN = 'E<<'
final String JA = 'J>>'
String en = ''
String ja = ''
trc.eachLine { line ->
if (line.startsWith(EN)) {
en = line.substring(3)
ja = ''
} else if (line.startsWith(JA)) {
ja = line.substring(3)
if (en && ja) {
map[en] = ja
}
}
}
if (map) {
console.println "${map.size()} trans-units found."
} else {
return 'No translations found.'
}
// TMX を作成
try {
createTMX(tmx, map)
return "Created ${tmx.path}"
} catch(Exception ex) {
return ex
}
File getTMXFile(File trc) {
def path = trc.canonicalPath
if (path.endsWith('.trc')) {
path = path[0..-4] + 'tmx'
} else {
path += ".tmx"
}
new File(path)
}
def createTMX(File file, Map map) {
TMXWriter2 writer = new TMXWriter2(file, new Language('en'), new Language('ja'), true, true, true)
long creationDate = (new Date()).time
def propValues = [] as List<String>
map.each {
PrepareTMXEntry from = new PrepareTMXEntry()
from.creator = 'trc2tmx'
from.creationDate = creationDate
//TMXEntry(PrepareTMXEntry from, boolean defaultTranslation, ExternalLinked linked)
TMXEntry entry = new TMXEntry(from, true, null)
writer.writeEntry(it.key, it.value, entry, propValues)
}
writer.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment