lila/bin/trans-import

63 lines
1.5 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'fileutils'
include FileUtils
class String
def uncapitalize
self[0, 1].downcase + self[1..-1]
#self
end
end
def keyify(text)
text.strip.downcase
.gsub(/_/, ' ')
.gsub(/[^\w\s]/, '')
.gsub(/\b[a-z]/) { |a| a.upcase }
.gsub(/\s/, '')
.uncapitalize
end
def format(text)
text
.strip
.gsub('"', '')
.gsub(/%\w+%/, '%s')
end
lichess_dir = File.expand_path(pwd() + '/../lichess')
lichess_trans_dir = lichess_dir + '/src/Bundle/LichessBundle/Resources/translations'
lila_dir = pwd()
lila_trans_dir = lila_dir + '/conf'
puts 'Import translations'
puts 'from ' + lichess_trans_dir
puts 'to ' + lila_trans_dir
Dir[lila_trans_dir + '/messages.*'].each { File.delete }
Dir[lichess_trans_dir + '/*'].each { |file|
lang = file.gsub(/[\w\/]+\.(\w{2,3})\.yml/, '\1')
lines = File.open(file).readlines.map { |l|
parts = l.split('":').each { |p| p.strip.gsub('"', '') }
key = keyify(parts[0])
val = format(parts[1])
key + '=' + val
}
if lang == 'frp' then lang = 'fp' end
new_file = lila_trans_dir + '/messages.' + lang
text = lines.join("\n")
File.open(new_file, 'w') { |f| f.puts text }
}
fr_file = lichess_trans_dir + '/messages.fr.yml'
lines = File.open(fr_file).readlines.map { |l|
parts = l.split('":').each { |p| p.strip.gsub('"', '') }
key = keyify(parts[0])
val = format(parts[0])
key + '=' + val
}
text = lines.join("\n")
new_file = lila_trans_dir + '/messages'
File.open(new_file, 'w') { |f| f.puts text }