lila/bin/trans-dump
2012-06-16 21:21:10 +02:00

49 lines
1.2 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'fileutils'
include FileUtils
lila_dir = pwd()
lila_trans_dir = lila_dir + '/conf'
lila_keys_file = lila_dir + '/app/i18n/I18nKeys.scala'
code = """// Generated with bin/trans-dump
package lila.i18n
import lila.http.Context
import play.api.templates.Html
import play.api.i18n.Lang
final class I18nKeys(translator: Translator) {
final class Key(val key: String) extends I18nKey {
def apply(args: Any*)(implicit ctx: Context): Html =
translator.html(key, args.toList)(ctx.req)
def str(args: Any*)(implicit ctx: Context): String =
translator.str(key, args.toList)(ctx.req)
def to(lang: Lang)(args: Any*): String =
translator.transTo(key, args.toList)(lang)
}
def untranslated(message: String) = Untranslated(message)
%vals%
def keys = List(%keys%)
}
"""
puts 'Dump translation keys to scala'
file = lila_trans_dir + '/messages'
lines = File.open(file).readlines
keys = lines.map { |l| l.gsub(/(\w+)=.+/, '\1').strip }
vals = keys.map { |k| ' val ' + k + ' = new Key("' + k + '")' }
allCode = code.gsub(/%vals%/, vals.join("\n")).gsub(/%keys%/, keys.join(", "))
File.open(lila_keys_file, 'w') { |f| f.puts allCode }