Skip to content

Commit

Permalink
Merge pull request #131 from transifex/add-pseudo
Browse files Browse the repository at this point in the history
Add download pseudo translations flag
  • Loading branch information
codegaze authored Nov 17, 2022
2 parents c4134fc + 20da832 commit 28d9244
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ default to taking the filesystem timestamp into account.
speed. The `--workers` flag sets the number of concurrent downloads possible at
any time.
- `--pseudo`: Generate mock string translations with a ~20% default length increase in characters.
### Removing resources from Transifex
The tx delete command lets you delete a resource that's in your `config` file and on Transifex.
Expand Down
6 changes: 6 additions & 0 deletions cmd/tx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ func Main() {
Name: "silent",
Usage: "Whether to reduce verbosity of the output",
},
&cli.BoolFlag{
Name: "pseudo",
Usage: "Generate mock string translations",
Value: false,
},
},
Action: func(c *cli.Context) error {
cfg, err := config.LoadFromPaths(c.String("root-config"),
Expand Down Expand Up @@ -534,6 +539,7 @@ func Main() {
MinimumPercentage: c.Int("minimum-perc"),
Workers: c.Int("workers"),
Silent: c.Bool("silent"),
Pseudo: c.Bool("pseudo"),
}

if c.Bool("xliff") && c.Bool("json") {
Expand Down
28 changes: 20 additions & 8 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PullCommandArguments struct {
MinimumPercentage int
Workers int
Silent bool
Pseudo bool
}

func PullCommand(
Expand Down Expand Up @@ -403,6 +404,7 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
resource,
args.ContentEncoding,
args.FileType,
args.Pseudo,
)
return err
},
Expand Down Expand Up @@ -497,14 +499,24 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
err = handleThrottling(
func() error {
var err error
download, err = txapi.CreateTranslationsAsyncDownload(
api,
resource,
languageCode,
args.ContentEncoding,
args.FileType,
args.Mode,
)
if args.Pseudo {
download, err = txapi.CreateResourceStringsAsyncDownload(
api,
resource,
args.ContentEncoding,
args.FileType,
args.Pseudo,
)
} else {
download, err = txapi.CreateTranslationsAsyncDownload(
api,
resource,
languageCode,
args.ContentEncoding,
args.FileType,
args.Mode,
)
}
return err
},
"Creating download job",
Expand Down
66 changes: 56 additions & 10 deletions internal/txlib/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestPullCommandResourceExists(t *testing.T) {
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleTranslationDownload(t, mockData)
testSimpleTranslationDownload(t, mockData, "false")
testSimpleGet(t, mockData, translationDownloadUrl)

assertFileContent(t, "aaa-el.json", "This is the content")
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestPullCommandFileExists(t *testing.T) {
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleTranslationDownload(t, mockData)
testSimpleTranslationDownload(t, mockData, "false")
testSimpleGet(t, mockData, translationDownloadUrl)

assertFileContent(t, "aaa-el.json", "This is the content")
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestPullCommandDownloadSource(t *testing.T) {
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlSourceLanguage)
testSimpleSourceDownload(t, mockData)
testSimpleSourceDownload(t, mockData, "false")
testSimpleGet(t, mockData, sourceDownloadUrl)

assertFileContent(t, "aaa.json", "New source")
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestPullCommandProceedOnEqualTranslatedMinPerc(t *testing.T) {
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleTranslationDownload(t, mockData)
testSimpleTranslationDownload(t, mockData, "false")
testSimpleGet(t, mockData, translationDownloadUrl)

assertFileContent(t, "aaa-el.json", "This is the content")
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestPullCommandOverrides(t *testing.T) {
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleTranslationDownload(t, mockData)
testSimpleTranslationDownload(t, mockData, "false")
testSimpleGet(t, mockData, translationDownloadUrl)

assertFileContent(t, "custom_path.json", "This is the content")
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestPullCommandMultipleLangParameters(t *testing.T) {
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleTranslationDownload(t, mockData)
testSimpleTranslationDownload(t, mockData, "false")
testSimpleGet(t, mockData, translationDownloadUrl)

assertFileContent(t, "locale/el/aaa-el.json", "This is the content")
Expand Down Expand Up @@ -547,6 +547,50 @@ func TestShouldSkipDueToStringPercentage(t *testing.T) {
assert.Equal(t, result, true)
}

func TestDownloadPseudoTranslations(t *testing.T) {
afterTest := beforeTest(t, []string{"el"}, nil)
defer afterTest()

cfg := getStandardConfig()
cfg.Local.Resources[0].FileFilter = "locale/<lang>/aaa-<lang>.json"

ts := getNewTestServer("This is the content")
defer ts.Close()

mockData := jsonapi.MockData{
resourceUrl: getResourceEndpoint(),
projectUrl: getProjectEndpoint(),
statsUrlAllLanguages: getStatsEndpointAllLanguages(),
sourceDownloadsUrl: getSourceDownloadsEndpoint(),
sourceDownloadUrl: getDownloadEndpoint(ts.URL),
}

api := jsonapi.GetTestConnection(mockData)

arguments := PullCommandArguments{
FileType: "default",
Mode: "default",
Force: true,
All: true,
ResourceIds: nil,
MinimumPercentage: -1,
Workers: 1,
Pseudo: true,
}

err := PullCommand(cfg, &api, &arguments)
if err != nil {
t.Errorf("%s", err)
}

assertFileContent(t, "locale/el/aaa-el.json", "This is the content")
testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleSourceDownload(t, mockData, "true")
testSimpleGet(t, mockData, sourceDownloadUrl)
}

func assertFileContent(t *testing.T, expectedPath, expectedContent string) {
data, err := os.ReadFile(expectedPath)
if err != nil {
Expand Down Expand Up @@ -586,6 +630,7 @@ func getDownloadEndpoint(url string) *jsonapi.MockEndpoint {
func testSimpleTranslationDownload(
t *testing.T,
mockData jsonapi.MockData,
pseudo string,
) {
testSimplePost(
t,
Expand All @@ -597,20 +642,21 @@ func testSimpleTranslationDownload(
"attributes": {"content_encoding": "",
"file_type": "default",
"mode": "default",
"pseudo": false},
"pseudo": %s},
"relationships": {
"language": {"data": {"type": "languages", "id": "l:el"}},
"resource": {"data": {"type": "resources", "id": "%s"}}
}
}}`,
resourceId,
pseudo, resourceId,
),
)
}

func testSimpleSourceDownload(
t *testing.T,
mockData jsonapi.MockData,
pseudo string,
) {
testSimplePost(
t,
Expand All @@ -621,12 +667,12 @@ func testSimpleSourceDownload(
"type": "resource_strings_async_downloads",
"attributes": {"content_encoding": "",
"file_type": "default",
"pseudo": false},
"pseudo": %s},
"relationships": {
"resource": {"data": {"type": "resources", "id": "%s"}}
}
}}`,
resourceId,
pseudo, resourceId,
),
)
}
3 changes: 2 additions & 1 deletion pkg/txapi/resource_strings_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func CreateResourceStringsAsyncDownload(
resource *jsonapi.Resource,
contentEncoding string,
fileType string,
pseudo bool,
) (*jsonapi.Resource, error) {
download := &jsonapi.Resource{
API: api,
Type: "resource_strings_async_downloads",
Attributes: map[string]interface{}{
"content_encoding": contentEncoding,
"file_type": fileType,
"pseudo": false,
"pseudo": pseudo,
},
}
download.SetRelated("resource", resource)
Expand Down

0 comments on commit 28d9244

Please sign in to comment.