This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathimport-locales.go
146 lines (125 loc) · 3.95 KB
/
import-locales.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/st3fan/xliff"
)
func localizedDestination(dest string, language string) string {
if language == "tl" {
language = "fil"
}
dir, file := filepath.Split(dest)
return filepath.Join(dir, fmt.Sprintf("%s.lproj", language), file)
}
func shouldSkipTransUnit(transUnit xliff.TransUnit, skipTransUnits []string) bool {
for _, s := range skipTransUnits {
if s == transUnit.ID {
return true
}
}
return false
}
func escapeString(s string) string {
return strings.Replace(s, `"`, `\"`, -1)
}
func writeStrings(file xliff.File, path string, skipTransUnits []string, allowIncomplete bool) error {
w, err := os.Create(path)
if err != nil {
return err
}
defer w.Close()
for _, transUnit := range file.Body.TransUnits {
if shouldSkipTransUnit(transUnit, skipTransUnits) {
continue
}
// If we allow incomplete strings, then do not write this
// string out at all. The app will default to the english base
// string if the string is not present for a locale.
if allowIncomplete && transUnit.Target == "" {
continue
}
if transUnit.Note != "" {
fmt.Fprintf(w, "/* %s */\n", transUnit.Note)
} else {
fmt.Fprintf(w, "/* (No Comment) */\n")
}
fmt.Fprintf(w, "\"%s\" = \"%s\";\n\n", transUnit.ID, escapeString(transUnit.Target))
}
return nil
}
func removeMissingTransUnitTargetErrors(errors []xliff.ValidationError) []xliff.ValidationError {
var result []xliff.ValidationError
for _, error := range errors {
if error.Code != xliff.MissingTransUnitTarget {
result = append(result, error)
}
}
return result
}
func main() {
var allowIncomplete = flag.Bool("allowIncomplete", false, "Allow incomplete locales to be imported")
flag.Parse()
fileMappings := map[string]string{
"Blockzilla/Info.plist": "Blockzilla/InfoPlist.strings",
"Blockzilla/en.lproj/Localizable.strings": "Blockzilla/Localizable.strings",
"Blockzilla/Intro.strings": "Blockzilla/Intro.strings",
}
skipTransUnits := []string{
"CFBundleDisplayName",
"CFBundleName",
"CFBundleShortVersionString",
}
Loop:
for _, path := range flag.Args() {
fmt.Println("Processing ", path)
doc, err := xliff.FromFile(path)
if err != nil {
fmt.Printf("Skipping: %s: %v\n", path, err)
continue Loop
}
// Dry run to make sure this locale is part of the project already
for _, file := range doc.Files {
if unlocalizedDestination, ok := fileMappings[file.Original]; ok {
destination := localizedDestination(unlocalizedDestination, file.TargetLanguage)
if _, err := os.Stat(destination); os.IsNotExist(err) {
fmt.Printf("Skipping: %s: not imported into the project first\n", path)
continue Loop
}
}
}
if !doc.IsComplete() && !*allowIncomplete {
fmt.Printf("Skipping: %s: not completely localized\n", path)
continue Loop
}
// Validate the document. If we allow incomplete locales then
// we ignore MissingTransUnitTarget errors. Other errors will
// will result in a complete rejection of the file.
errors := doc.Validate()
if len(errors) != 0 {
if *allowIncomplete {
errors = removeMissingTransUnitTargetErrors(errors)
}
if len(errors) != 0 {
for _, err := range errors {
fmt.Printf("Skipping: %s: because of validation error: %s\n", path, err)
}
continue Loop
}
}
// Everything is good to go, actually import strings
for _, file := range doc.Files {
if unlocalizedDestination, ok := fileMappings[file.Original]; ok {
destination := localizedDestination(unlocalizedDestination, file.TargetLanguage)
if err := writeStrings(file, destination, skipTransUnits, *allowIncomplete); err != nil {
fmt.Printf("Error: Failed to write strings for %s: %v\n", path, err)
}
}
}
}
}