Skip to content

Commit

Permalink
Merge pull request #176 from transifex/devel
Browse files Browse the repository at this point in the history
v1.6.6
  • Loading branch information
codegaze authored Mar 28, 2023
2 parents f0a16fe + 1b35c49 commit 6ade758
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 5 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,18 @@ add multiple resources with a relatively simple shell script. For example:

If you have content already setup in Transifex, you may want to setup local
resources in order to pull the language files on your system. In order to do
that, you can run:
that, you can run the following command with Linux or Mac OS:

```sh
tx add remote \
--file-filter 'translations/<project_slug>.<resource_slug>/<lang>.<ext>'
https://www.transifex.com/myorganization/myproject/dashboard/
```

> The use of tx add remote appends the content in the .tx/config file and does not overwrite it. However, if the project and resource exist in the .tx/config file, then it will overwrite the previous information for the specific project & resource.
For Windows OS, please use double quotes instead of single quotes in the following example.

This will create entries in your configuration file for each resource in your
remote project. ie the configuration file may look like this:

Expand Down Expand Up @@ -509,6 +513,9 @@ This means that the _remote_ `pt_PT` language code maps to the _local_ `pt-pt`
language code and the _remote_ `pt_BR` language code maps to the _local_
`pt-br` language code.
The REMOTE_CODE is the language code supported by Transifex. And the LOCAL_CODE is your
language code.
The `-l` flag works with both _local_ and _remote_ language codes.
**Skipping pushing older files:**
Expand Down Expand Up @@ -734,6 +741,12 @@ default to taking the filesystem timestamp into account.
when using `-a/--all` flag and you don't want to change the existing files
but only download other language files.
`--mode/-m`: The translation mode of the downloaded file. This can be one of the
following: `'default', 'reviewed'`, `'proofread'`, `'translator'`, `'untranslated'`,
`'onlytranslated'`, `'onlyreviewed'`, `'onlyproofread'`, `'sourceastranslation'` **(default
mode is: **`'default'`). Use like` 'tx pull -m proofread'` to download only proofread
translations.
- `--branch`: Using this flag, you can access copies of the regular remote
resource that are tied to the provided branch. So if `tx pull proj.res`
pulls from the `https://www.transifex.com/org/proj/res` resource, then `tx
Expand Down
6 changes: 6 additions & 0 deletions cmd/tx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ func Main() {
Aliases: []string{"d"},
Usage: "Whether skip existing files",
},
&cli.BoolFlag{
Name: "keep-new-files",
Usage: "Used with --disable-ovewrite to create new files " +
"if file already exists with a '.new' extension.",
},
&cli.BoolFlag{
Name: "skip",
Usage: "Whether to skip on errors",
Expand Down Expand Up @@ -537,6 +542,7 @@ func Main() {
Source: c.Bool("source"),
Translations: c.Bool("translations"),
DisableOverwrite: c.Bool("disable-overwrite"),
KeepNewFiles: c.Bool("keep-new-files"),
All: c.Bool("all"),
ResourceIds: resourceIds,
UseGitTimestamps: c.Bool("use-git-timestamps"),
Expand Down
17 changes: 13 additions & 4 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type PullCommandArguments struct {
Translations bool
All bool
DisableOverwrite bool
KeepNewFiles bool
ResourceIds []string
UseGitTimestamps bool
Branch string
Expand Down Expand Up @@ -378,8 +379,12 @@ func (task *FilePullTask) Run(send func(string), abort func()) {

_, err := os.Stat(sourceFile)
if err == nil && args.DisableOverwrite {
sendMessage("Disable Overwrite is enabled, skipping", false)
return
if !args.KeepNewFiles {
sendMessage("Disable overwrite enabled, skipping", false)
return
} else {
sourceFile = sourceFile + ".new"
}
}

if !args.Force {
Expand Down Expand Up @@ -447,8 +452,12 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
if filePath != "" {
// Remote language file exists and so does local
if args.DisableOverwrite {
sendMessage("Disable overwrite enabled, skipping", false)
return
if !args.KeepNewFiles {
sendMessage("Disable overwrite enabled, skipping", false)
return
} else {
filePath = filePath + ".new"
}
}
} else {
// Remote language file exists but local does not
Expand Down
144 changes: 144 additions & 0 deletions internal/txlib/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package txlib

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -591,6 +592,149 @@ func TestDownloadPseudoTranslations(t *testing.T) {
testSimpleGet(t, mockData, sourceDownloadUrl)
}

func TestKeepNewFilesSource(t *testing.T) {
afterTest := beforeTest(t, nil, nil)
defer afterTest()

cfg := getStandardConfig()
assertFileContent(t, "aaa.json", "")

ts := getNewTestServer("New source")
defer ts.Close()

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

api := jsonapi.GetTestConnection(mockData)
err := PullCommand(
cfg,
&api,
&PullCommandArguments{
FileType: "default",
Mode: "default",
Force: true,
Source: true,
ResourceIds: nil,
MinimumPercentage: -1,
Workers: 1,
KeepNewFiles: true,
DisableOverwrite: true,
},
)
if err != nil {
t.Errorf("%s", err)
}

testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlSourceLanguage)
testSimpleSourceDownload(t, mockData, "false")
testSimpleGet(t, mockData, sourceDownloadUrl)

assertFileContent(t, "aaa.json.new", "New source")
assertFileContent(t, "aaa.json", "")
}

func TestKeepNewFilesSourceOnlyWithDisableOverride(t *testing.T) {
afterTest := beforeTest(t, nil, nil)
defer afterTest()

cfg := getStandardConfig()
assertFileContent(t, "aaa.json", "")

ts := getNewTestServer("New source")
defer ts.Close()

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

api := jsonapi.GetTestConnection(mockData)
err := PullCommand(
cfg,
&api,
&PullCommandArguments{
FileType: "default",
Mode: "default",
Force: true,
Source: true,
ResourceIds: nil,
MinimumPercentage: -1,
Workers: 1,
KeepNewFiles: true,
},
)
if err != nil {
t.Errorf("%s", err)
}

testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlSourceLanguage)
testSimpleSourceDownload(t, mockData, "false")
testSimpleGet(t, mockData, sourceDownloadUrl)

assertFileContent(t, "aaa.json", "New source")
_, err = ioutil.ReadFile("aaa.json.new")
if err == nil {
t.Error("File not exist because DisableOverwrite is not true")
}
}

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

cfg := getStandardConfig()

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

mockData := jsonapi.MockData{
resourceUrl: getResourceEndpoint(),
projectUrl: getProjectEndpoint(),
statsUrlAllLanguages: getStatsEndpointAllLanguages(),
translationDownloadsUrl: getTranslationDownloadsEndpoint(),
translationDownloadUrl: getDownloadEndpoint(ts.URL),
}

api := jsonapi.GetTestConnection(mockData)

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

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

testSimpleGet(t, mockData, resourceUrl)
testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, statsUrlAllLanguages)
testSimpleTranslationDownload(t, mockData, "false")
testSimpleGet(t, mockData, translationDownloadUrl)
assertFileContent(t, "aaa-el.json", `{"hello": "world"}`)
assertFileContent(t, "aaa-el.json.new", "This is the content")
}

func assertFileContent(t *testing.T, expectedPath, expectedContent string) {
data, err := os.ReadFile(expectedPath)
if err != nil {
Expand Down

0 comments on commit 6ade758

Please sign in to comment.