Skip to content

Commit 738b85d

Browse files
committed
update gitignore
0 parents  commit 738b85d

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

.ruby-gemset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xliff

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.1

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gem 'byebug'
2+
gem 'nokogiri'
3+
gem 'sanitize'

csv2xliff.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'nokogiri'
4+
require 'csv'
5+
require 'byebug'
6+
require 'fileutils'
7+
8+
def query2hash(string)
9+
return {} if string.nil?
10+
string.split('&').inject({}) do |hash,keyval|
11+
tmp =keyval.split('=')
12+
hash[tmp[0]]=tmp[1]
13+
hash
14+
end
15+
end
16+
17+
# ruby csv2xliff.rb traductions_grundey_target.csv -T lolita
18+
if $stdin.tty?
19+
csv_filename= ARGV[0]
20+
unless csv_filename[-4..-1] == ".csv"
21+
puts "pas de fichiers csv en premier paramètre"
22+
exit 0
23+
end
24+
if ARGV[1] == '--TargetFolder' || ARGV[1] == '-T'
25+
folder= ARGV[2]
26+
else
27+
puts "pas de dossier indiqué après --TargetFolder ou -T"
28+
exit 0
29+
end
30+
end
31+
32+
if folder[-1..-1]=="/"
33+
folder= folder[0..-2]
34+
end
35+
36+
FileUtils.mkdir_p(folder)
37+
#Read csv with header: Filename file_params trans-unit_params same Source sc target tc
38+
filescollection = Hash.new(Array.new)
39+
40+
CSV.foreach( csv_filename ,{:headers => :string,:col_sep => ";",:encoding => "bom|utf-8"}) do |row|
41+
42+
file_params=query2hash(row["File_params"])
43+
tu_params=query2hash(row["Trans-unit_params"])
44+
filescollection[row["Filename"]] += [ [file_params,tu_params,row["Source"],row["Target"]] ]
45+
end
46+
47+
filescollection.each do |file,content|
48+
debugger if file =~ /145/
49+
file_params= content.first.first
50+
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') { |xml|
51+
xml.xliff(:version => "1.2",:xmlns => "urn:oasis:names:tc:xliff:document:1.2") {
52+
xml.file(:original => file_params['original'],:"source-language" => file_params['source'],:"target-language" => file_params['target'],:datatype => file_params['datatype']) {
53+
xml.body{
54+
content.each do |data|
55+
tu= data[1]
56+
xml.send(:"trans-unit",:resname => tu['resname'], :restype => tu['restype'],:datatype => tu['datatype'],:id => tu['id']) {
57+
xml.source{
58+
xml.cdata(data[2])
59+
}
60+
xml.target {
61+
xml.cdata(data[3])
62+
}
63+
}
64+
end
65+
}
66+
}
67+
}
68+
}
69+
70+
#save the file with builder.to_xml
71+
File.open("#{folder}/#{file}",'w+') do |f|
72+
f.puts builder.to_xml
73+
end
74+
end

xliff2csv.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'nokogiri'
4+
require 'csv'
5+
require 'byebug'
6+
require 'sanitize'
7+
8+
# ruby xliff2csv.rb -F grundey_target
9+
if $stdin.tty?
10+
if ARGV[0] == '--SourceFolder' || ARGV[0] == '-S'
11+
folder= ARGV[1]
12+
else
13+
puts "pas de dossier indiqué après --SourceFolder ou -S"
14+
exit 0
15+
end
16+
end
17+
18+
if folder[-1..-1]=="/"
19+
folder= folder[0..-2]
20+
end
21+
22+
files = Dir.glob(folder+'/*.xliff')
23+
csv_filename = folder.split("/").last
24+
25+
class Hash
26+
def to_string
27+
size = self.size
28+
count= 0
29+
self.inject("") do |accu,(k,v)|
30+
count +=1
31+
accu +="#{k.to_s}=#{v}"
32+
accu +="&" unless count == size
33+
accu
34+
end
35+
end
36+
end
37+
38+
CSV.open( "traductions_#{csv_filename}.csv","w+",{:col_sep => ";"}) do |csv|
39+
csv << ["Filename","File_params","Trans-unit_params","same","Source","sc","Target","tc"]
40+
files.each do |file|
41+
f = File.open(file)
42+
doc = Nokogiri::XML(f,nil,'UTF-8') do |config|
43+
config.options = Nokogiri::XML::ParseOptions::STRICT | Nokogiri::XML::ParseOptions::NONET
44+
end
45+
46+
filename = file.split("/").last
47+
params = {}
48+
file_params = doc.xpath("//x:file", "x" => "urn:oasis:names:tc:xliff:document:1.2").first
49+
params[:original] = file_params.attr("original")
50+
params[:source] = file_params.attr("source-language")
51+
params[:target] = file_params.attr("target-language")
52+
params[:datatype] = file_params.attr("datatype")
53+
trans_units_params = doc.xpath("//x:trans-unit", "x" => "urn:oasis:names:tc:xliff:document:1.2")
54+
sources = doc.xpath("//x:source", "x" => "urn:oasis:names:tc:xliff:document:1.2")
55+
targets = doc.xpath("//x:target", "x" => "urn:oasis:names:tc:xliff:document:1.2")
56+
sources.each_with_index do |source,index|
57+
trans_params= trans_units_params[index]
58+
trans_unit = {}
59+
trans_unit[:id] = trans_params.attr('id')
60+
trans_unit[:resname] = trans_params.attr('resname')
61+
trans_unit[:restype] = trans_params.attr('restype')
62+
trans_unit[:datatype] = trans_params.attr('datatype')
63+
source = source.text
64+
target = targets[index].text
65+
66+
same = ((source == target) ? 1 :0)
67+
csv << [filename,params.to_string,trans_unit.to_string,same,source,Sanitize.clean(source).split.size,target,Sanitize.clean(target).split.size]
68+
end
69+
f.close
70+
end
71+
end

0 commit comments

Comments
 (0)