Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specialized modifications for our site: added mixed lang mode and the ab... #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions po2yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# Copyright (C) 2009 Thomas Wood
# Copyright (C) 2009 Tom Hughes
#
# Contributors: MNelson30
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
Expand All @@ -29,7 +31,6 @@

require "yaml"


def add_translation(hash, keys, value)
key = keys.shift
if keys.empty?
Expand All @@ -43,18 +44,20 @@ def add_translation(hash, keys, value)
hash
end


# Builds yaml file when a po without a context msg. TODO: option to allow it.
def po2hash(f)
trs = {}
path = []
msgstr = ''
f.each_line { |line|
line.strip!
if line[0..8] == 'msgctxt "'
path = line[9..-2].split(':')
line.gsub!(/\\/,'')
if line[0..6] == 'msgid "'
path = line[7..-2].split(":")
elsif line[0..7] == 'msgstr "'
# TODO convert escaped characters
msgstr = line[8..-2]
puts(msgstr)
end

if !path.empty? and !msgstr.empty?
Expand All @@ -66,14 +69,13 @@ def po2hash(f)
trs
end


def generate_yaml(filename, outfilename)
def generate_yaml(filename, outfilename, langtype)
if File.exists? filename
pofile = File.open(filename, "r")
# The following line expects the files to be named like "de.po" and therefore the language code can be recovered by substracting the '.po' suffix
# TODO refactor in order to extract the language code when the file is not just named like "LANG.po", but like "SOMETHING.LANG.po"
# TODO refactor in order to request the language code in case it can't be extracted from the filename
langcode = File.basename(filename, '.po')
langcode = langtype
tr = {langcode => po2hash(pofile)}
outfile = File.new(outfilename, "w")
outfile.puts(tr.to_yaml)
Expand All @@ -83,21 +85,16 @@ def generate_yaml(filename, outfilename)
end
end


def print_usage()
puts("\nUsage:\n")
puts(" - To create a language's yaml from a given po file\n")
puts(" po2yaml de.po de.yml\n\n")
puts(" po2yaml ja.po ja.yml ja\n\n")
end



if ARGV.size == 2
generate_yaml(ARGV[0], ARGV[1])
# ARGV[2] is for the language type in header
if ARGV.size == 3
generate_yaml(ARGV[0], ARGV[1] ARGV[2])
exit
end

print_usage()



78 changes: 61 additions & 17 deletions yaml2po
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
# - To create a language's .po
# yaml2po -l de -t en.yml de.yml > de.po
#
# - To create mixed language's .po\n")
# yaml2po -l ja -t ja.yml ja.po enginetype keepmsgctxt
# -enginetype = 'psych' / 'syck'
# -keepmsgctxt = true: 1 / false: 0
#
# Copyright (C) 2012 Leandro Regueiro <leandro.regueiro AT gmail DOT com>
# Copyright (C) 2009 Thomas Wood
# Copyright (C) 2009 Tom Hughes
#
# Contributors: MNelson30
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
Expand All @@ -35,20 +41,19 @@ require "yaml"
require "optparse"
require "time"


def process_msgid_msgstr(string)
# Escape the " characters and correctly output multiline strings both in msgid and msgstr
if string.is_a? Hash
new_string = ''
else
new_string = string.gsub("\"", "\\\"")
new_string = new_string.gsub("\n", "\"\n\"")
new_string = new_string.gsub("\t", "\"\t\"")
end
return new_string
end


def iterate(hash, fhash={}, path='', outfile=$stdout)
def iterate(hash, fhash={}, path='', outfile=$stdout, keepmsgctxt)
postr = ''
hash.each {|key, val|
fhash[key] = {} unless fhash.has_key? key
Expand All @@ -59,7 +64,9 @@ def iterate(hash, fhash={}, path='', outfile=$stdout)
puts("Empty key: #{path}#{key}")
else
outfile.puts("")
outfile.puts("msgctxt \"#{path}#{key}\"")
if keepmsgctxt == 1
outfile.puts("msgctxt \"#{path}#{key}\"")
end
new_source = process_msgid_msgstr(val)
outfile.puts("msgid \"#{new_source}\"")
new_translation = process_msgid_msgstr(fhash[key])
Expand All @@ -68,6 +75,28 @@ def iterate(hash, fhash={}, path='', outfile=$stdout)
}
end

# used for converting a yaml file that contains two languages.
def mixediterate(fhash={}, path='', outfile=$stdout, keepmsgctxt)
postr = ''
fhash.each {|key, val|
fhash[key] = {} unless fhash.has_key? key
if val.is_a? Hash
fhash[key] = {} unless fhash[key].is_a? Hash
mixediterateiterate(val, fhash[key], path+key+':', outfile)
elsif val.nil?
puts("Empty key: #{path}#{key}")
else
outfile.puts("")
if keepmsgctxt == 1
outfile.puts("msgctxt \"#{path}#{key}\"")
end
new_source = process_msgid_msgstr(key)
outfile.puts("msgid \"#{new_source}\"")
new_translation = process_msgid_msgstr(val)
outfile.puts("msgstr \"#{new_translation}\"")
end
}
end

def print_header(lang='LANGUAGE', outfile=$stdout)
outfile.puts('# SOME DESCRIPTIVE TITLE.')
Expand Down Expand Up @@ -102,7 +131,6 @@ def print_header(lang='LANGUAGE', outfile=$stdout)
outfile.puts('"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"')
end


def generate_pot(engfilename, outfilename)
if File.exists? engfilename
engfile = YAML::load_file(engfilename)
Expand All @@ -115,16 +143,30 @@ def generate_pot(engfilename, outfilename)
end
end

# could be refactored a bit more. Just a repeat of what you already have.
def mixedlang2po(lang,langfilename, outfilename, enginetype, keepmsgctxt)
if File.exists? langfilename
YAML::ENGINE.yamler = enginetype
langfile = YAML::load_file(langfilename)
outfile = File.new(outfilename, "w")
print_header(lang, outfile)
mixediterate(langfile, '', outfile, keepmsgctxt)
outfile.close()
else
$stderr.puts("\nError: Specified YAML file \"#{langfilename}\" does not exist.\n\n")
end
end

def lang2po(lang, engfilename, langfilename, outfilename)
def lang2po(lang, engfilename, langfilename, outfilename, enginetype, keepmsgctxt)
if File.exists? engfilename
if File.exists? langfilename
YAML::ENGINE.yamler = enginetype
# Generate the PO file only once checked that both files exist
engfile = YAML::load_file(engfilename)
langfile = YAML::load_file(langfilename)
outfile = File.new(outfilename, "w")
print_header(lang, outfile)
iterate(engfile['en'], langfile[lang], '', outfile)
iterate(engfile['en'], langfile[lang], '', outfile, keepmsgctxt)
outfile.close()
else
$stderr.puts("\nError: Specified YAML file \"#{langfilename}\" does not exist.\n\n")
Expand All @@ -134,18 +176,20 @@ def lang2po(lang, engfilename, langfilename, outfilename)
end
end


def print_usage()
puts("\nUsage:\n")
puts(" - To create a 'master' .pot\n")
puts(" yaml2po -P en.yml file.pot\n")
puts("\n")
puts(" - To create a language's .po\n")
puts(" yaml2po -l de -t en.yml de.yml de.po\n\n")
puts(" yaml2po -l de -t en.yml de.yml de.po enginetype keepmsgctxt\n\n")
puts("\n")
puts(" - To create a language's .po\n")
puts(" yaml2po -l ja -t ja.yml ja.po enginetype keepmsgctxt\n\n")
puts(" # enginetype = 'psych' / 'syck' \n")
puts(" # keepmsgctxt = true: 1 / false: 0 \n\n")
end



opt = ARGV[0]

if opt == '-P'
Expand All @@ -156,13 +200,13 @@ if opt == '-P'
end
elsif opt == '-l'
# Goes this way when generating a PO file with the translation for a given language
if (ARGV.size == 6)
lang2po(ARGV[1], ARGV[3], ARGV[4], ARGV[5])
if (ARGV.size == 8)
lang2po(ARGV[1], ARGV[3], ARGV[4], ARGV[5], ARGV[6], ARGV[7])
exit
end
# Goes this way when generating a PO file in mixed language mode.
if (ARGV.size == 7)
mixedlang2po(ARGV[2], ARGV[3], ARGV[4], ARGV[5], ARGV[6])
end
end

print_usage()