forked from mgumz/xliffer
/
swap_src_target.go
74 lines (59 loc) · 1.89 KB
/
swap_src_target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This file is part of *xliffer*
//
// Copyright (C) 2015, Travelping GmbH <copyright@travelping.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"encoding/xml"
"flag"
"io"
)
// swapSourceTarget is a converter which swaps source and target attributes.
// usefull when one has to work with .xliff files coming from sources not
// savy in using their xliff-editors correctly.
type swapSourceTarget struct {
inFile string
}
func init() {
registeredConverters["swap-source-target"] = new(swapSourceTarget)
}
func (s *swapSourceTarget) Description() string {
return "Swaps source-attribute and target-attribute of a XLIFF"
}
func (s *swapSourceTarget) ParseArgs(base string, args []string) error {
var fs = flag.NewFlagSet(base+" swap-source-target", flag.ExitOnError)
fs.StringVar(&s.inFile, "in", "", "infile")
return fs.Parse(args)
}
func (s *swapSourceTarget) Prepare() error {
return nil
}
func (s *swapSourceTarget) Convert(w io.Writer) error {
var doc, err = xliffFromFile(s.inFile)
if err != nil {
return err
}
for i := range doc.File {
doc.File[i].TargetLang = ""
for j := range doc.File[i].Body.TransUnit {
src := doc.File[i].Body.TransUnit[j].Source.Inner
target := doc.File[i].Body.TransUnit[j].Target.Inner
sname := doc.File[i].Body.TransUnit[j].Source.XMLName
tname := doc.File[i].Body.TransUnit[j].Target.XMLName
doc.File[i].Body.TransUnit[j].Source.Inner = target
doc.File[i].Body.TransUnit[j].Target.Inner = src
doc.File[i].Body.TransUnit[j].Source.XMLName = sname
doc.File[i].Body.TransUnit[j].Target.XMLName = tname
}
}
var out []byte
if out, err = xml.MarshalIndent(doc, "", " "); err != nil {
return err
}
io.WriteString(w, xml.Header)
w.Write(out)
return nil
}