Skip to content

Commit 4166467

Browse files
author
Konstantinos Bairaktaris
committed
Add '--replace-edited-strings' flag/config option
1 parent 4e20586 commit 4166467

File tree

6 files changed

+102
-57
lines changed

6 files changed

+102
-57
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ fall back to taking the filesystem timestamp into account.
590590
any time.
591591
- `--silent`: Reduce verbosity of the output.
592592
593+
- `--replace-edited-strings`: If present, source strings that have been edited
594+
(in the editor UI or via the API) will not be protected from this source
595+
file push and will instead be replaced. This can also be set on a
596+
per-resource level in the configuration file.
597+
593598
### Pulling Files from Transifex
594599
595600
`tx pull` is used to pull language files (usually translation language files) from

cmd/tx/main.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ func Main() {
259259
Name: "silent",
260260
Usage: "Whether to reduce verbosity of the output",
261261
},
262+
&cli.BoolFlag{
263+
Name: "replace-edited-strings",
264+
Usage: "Whether to replace source strings that have been edited in the " +
265+
"meantime",
266+
},
262267
},
263268
Action: func(c *cli.Context) error {
264269
cfg, err := config.LoadFromPaths(
@@ -327,19 +332,20 @@ func Main() {
327332
}
328333

329334
args := txlib.PushCommandArguments{
330-
Source: c.Bool("source"),
331-
Translation: c.Bool("translation"),
332-
Force: c.Bool("force"),
333-
Skip: c.Bool("skip"),
334-
Xliff: c.Bool("xliff"),
335-
Languages: languages,
336-
ResourceIds: resourceIds,
337-
UseGitTimestamps: c.Bool("use-git-timestamps"),
338-
Branch: c.String("branch"),
339-
Base: c.String("base"),
340-
All: c.Bool("all"),
341-
Workers: workers,
342-
Silent: c.Bool("silent"),
335+
Source: c.Bool("source"),
336+
Translation: c.Bool("translation"),
337+
Force: c.Bool("force"),
338+
Skip: c.Bool("skip"),
339+
Xliff: c.Bool("xliff"),
340+
Languages: languages,
341+
ResourceIds: resourceIds,
342+
UseGitTimestamps: c.Bool("use-git-timestamps"),
343+
Branch: c.String("branch"),
344+
Base: c.String("base"),
345+
All: c.Bool("all"),
346+
Workers: workers,
347+
Silent: c.Bool("silent"),
348+
ReplaceEditedStrings: c.Bool("replace-edited-strings"),
343349
}
344350

345351
if args.All && len(args.Languages) > 0 {

internal/txlib/config/local.go

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ type LocalConfig struct {
2121
}
2222

2323
type Resource struct {
24-
OrganizationSlug string
25-
ProjectSlug string
26-
ResourceSlug string
27-
FileFilter string
28-
SourceFile string
29-
SourceLanguage string
30-
Type string
31-
LanguageMappings map[string]string
32-
Overrides map[string]string
33-
MinimumPercentage int
34-
ResourceName string
24+
OrganizationSlug string
25+
ProjectSlug string
26+
ResourceSlug string
27+
FileFilter string
28+
SourceFile string
29+
SourceLanguage string
30+
Type string
31+
LanguageMappings map[string]string
32+
Overrides map[string]string
33+
MinimumPercentage int
34+
ResourceName string
35+
ReplaceEditedStrings bool
3536
}
3637

3738
func loadLocalConfig() (*LocalConfig, error) {
@@ -121,18 +122,29 @@ func loadLocalConfigFromBytes(data []byte) (*LocalConfig, error) {
121122
return nil, err
122123
}
123124

125+
replaceEditedStrings := false
126+
if section.HasKey("replace_edited_strings") {
127+
replaceEditedStrings, err = section.Key("replace_edited_strings").Bool()
128+
if err != nil {
129+
return nil, fmt.Errorf(
130+
"'replace_edited_strings' needs to be 'true' or 'false': %s", err,
131+
)
132+
}
133+
}
134+
124135
resource := Resource{
125-
OrganizationSlug: organizationSlug,
126-
ProjectSlug: projectSlug,
127-
ResourceSlug: resourceSlug,
128-
FileFilter: section.Key("file_filter").String(),
129-
SourceFile: section.Key("source_file").String(),
130-
SourceLanguage: section.Key("source_lang").String(),
131-
Type: section.Key("type").String(),
132-
LanguageMappings: make(map[string]string),
133-
Overrides: make(map[string]string),
134-
MinimumPercentage: -1,
135-
ResourceName: section.Key("resource_name").String(),
136+
OrganizationSlug: organizationSlug,
137+
ProjectSlug: projectSlug,
138+
ResourceSlug: resourceSlug,
139+
FileFilter: section.Key("file_filter").String(),
140+
SourceFile: section.Key("source_file").String(),
141+
SourceLanguage: section.Key("source_lang").String(),
142+
Type: section.Key("type").String(),
143+
LanguageMappings: make(map[string]string),
144+
Overrides: make(map[string]string),
145+
MinimumPercentage: -1,
146+
ResourceName: section.Key("resource_name").String(),
147+
ReplaceEditedStrings: replaceEditedStrings,
136148
}
137149

138150
// Get first the perc in string to check if exists because .Key returns
@@ -282,6 +294,10 @@ func (localCfg LocalConfig) saveToWriter(file io.Writer) error {
282294
return err
283295
}
284296
}
297+
298+
section.NewKey(
299+
"replace_edited_strings", strconv.FormatBool(resource.ReplaceEditedStrings),
300+
)
285301
}
286302

287303
_, err = cfg.WriteTo(file)
@@ -366,6 +382,10 @@ func localConfigsEqual(left, right *LocalConfig) bool {
366382
return false
367383
}
368384
}
385+
386+
if leftResource.ReplaceEditedStrings != rightResource.ReplaceEditedStrings {
387+
return false
388+
}
369389
}
370390

371391
return true

internal/txlib/push.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ import (
1717
)
1818

1919
type PushCommandArguments struct {
20-
Source bool
21-
Translation bool
22-
Force bool
23-
Skip bool
24-
Xliff bool
25-
Languages []string
26-
ResourceIds []string
27-
UseGitTimestamps bool
28-
Branch string
29-
Base string
30-
All bool
31-
Workers int
32-
Silent bool
20+
Source bool
21+
Translation bool
22+
Force bool
23+
Skip bool
24+
Xliff bool
25+
Languages []string
26+
ResourceIds []string
27+
UseGitTimestamps bool
28+
Branch string
29+
Base string
30+
All bool
31+
Workers int
32+
Silent bool
33+
ReplaceEditedStrings bool
3334
}
3435

3536
func PushCommand(
@@ -445,6 +446,7 @@ func (task *ResourcePushTask) Run(send func(string), abort func()) {
445446
remoteStats[sourceLanguage.Id],
446447
args,
447448
resourceIsNew,
449+
args.ReplaceEditedStrings || cfgResource.ReplaceEditedStrings,
448450
}
449451
}
450452
if args.Translation { // -t flag is set
@@ -565,12 +567,13 @@ func (task *LanguagePushTask) Run(send func(string), abort func()) {
565567
}
566568

567569
type SourceFilePushTask struct {
568-
api *jsonapi.Connection
569-
resource *jsonapi.Resource
570-
sourceFile string
571-
remoteStats *jsonapi.Resource
572-
args PushCommandArguments
573-
resourceIsNew bool
570+
api *jsonapi.Connection
571+
resource *jsonapi.Resource
572+
sourceFile string
573+
remoteStats *jsonapi.Resource
574+
args PushCommandArguments
575+
resourceIsNew bool
576+
replaceEditedStrings bool
574577
}
575578

576579
func (task *SourceFilePushTask) Run(send func(string), abort func()) {
@@ -580,6 +583,7 @@ func (task *SourceFilePushTask) Run(send func(string), abort func()) {
580583
remoteStats := task.remoteStats
581584
args := task.args
582585
resourceIsNew := task.resourceIsNew
586+
replaceEditedStrings := task.replaceEditedStrings
583587

584588
parts := strings.Split(resource.Id, ":")
585589
sendMessage := func(body string, force bool) {
@@ -624,7 +628,7 @@ func (task *SourceFilePushTask) Run(send func(string), abort func()) {
624628
err = handleThrottling(
625629
func() error {
626630
var err error
627-
sourceUpload, err = txapi.UploadSource(api, resource, file)
631+
sourceUpload, err = txapi.UploadSource(api, resource, file, replaceEditedStrings)
628632
return err
629633
},
630634
"Uploading file",

pkg/jsonapi/resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"mime/multipart"
9+
"strconv"
910
)
1011

1112
type Resource struct {
@@ -191,6 +192,11 @@ func (r *Resource) SaveAsMultipart(fields []string) error {
191192
if err != nil {
192193
return err
193194
}
195+
case bool:
196+
err := writer.WriteField(field, strconv.FormatBool(data))
197+
if err != nil {
198+
return err
199+
}
194200
case []byte:
195201
w, err := writer.CreateFormFile(field,
196202
fmt.Sprintf("%s.txt", field))

pkg/txapi/resource_strings_async_uploads.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func (err *ResourceStringAsyncUploadAttributes) Error() string {
3636
}
3737

3838
func UploadSource(
39-
api *jsonapi.Connection, resource *jsonapi.Resource, file io.Reader,
39+
api *jsonapi.Connection,
40+
resource *jsonapi.Resource,
41+
file io.Reader,
42+
replaceEditedStrings bool,
4043
) (*jsonapi.Resource, error) {
4144
data, err := io.ReadAll(file)
4245
if err != nil {
@@ -49,7 +52,8 @@ func UploadSource(
4952
// Setting attributes directly here because POST and GET attributes are
5053
// different
5154
Attributes: map[string]interface{}{
52-
"content": data,
55+
"content": data,
56+
"replace_edited_strings": replaceEditedStrings,
5357
},
5458
}
5559
upload.SetRelated("resource", resource)

0 commit comments

Comments
 (0)