Skip to content

Commit

Permalink
feat: Add translation key prefixes for upload and download (#687)
Browse files Browse the repository at this point in the history
* add new download parameters

* add support to use the file path as prefix

* fix test

* update descriptions for new parameters

* update descriptions and test

* update the doc file

* fix php test

* add new param for upload

* accept file path as prefix

* add changes after rebase

* don't truncate file path to throw phrase error and stay transparent

* change the name of the parameter

* Fix placeholders in file path

---------

Co-authored-by: Sönke Behrendt <[email protected]>
  • Loading branch information
malvarezphrase and theSoenke authored Oct 2, 2024
1 parent 25e90f4 commit 9c9c959
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 4 deletions.
21 changes: 21 additions & 0 deletions clients/cli/cmd/internal/placeholders/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package placeholders

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/antihax/optional"
"github.com/phrase/phrase-cli/cmd/internal/stringz"
)

Expand Down Expand Up @@ -92,3 +94,22 @@ func Resolve(s, pattern string) (map[string]string, error) {

return values, nil
}

func ResolveTranslationKeyPrefix(translationKeyPrefix optional.String, filePath string) (optional.String, error) {
if strings.Contains(translationKeyPrefix.Value(), "<file_path>") {
currentDir, err := os.Getwd()
if err != nil {
return optional.EmptyString(), err
}

filePath, err := filepath.Rel(currentDir, filePath)
if err != nil {
return optional.EmptyString(), err
}

resolvedKeyPrefix := strings.Replace(translationKeyPrefix.Value(), "<file_path>", filePath, 1)
return optional.NewString(resolvedKeyPrefix), nil
}

return translationKeyPrefix, nil
}
6 changes: 6 additions & 0 deletions clients/cli/cmd/internal/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFil

if target.Params != nil {
localVarOptionals = target.Params.LocaleDownloadOpts
translationKeyPrefix, err := placeholders.ResolveTranslationKeyPrefix(target.Params.TranslationKeyPrefix, localeFile.Path)
if err != nil {
return err
}
localVarOptionals.TranslationKeyPrefix = translationKeyPrefix
}

if localVarOptionals.FileFormat.Value() == "" {
Expand All @@ -170,6 +175,7 @@ func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFil
debugFprintln("Tags", localVarOptionals.Tags)
debugFprintln("Branch", localVarOptionals.Branch)
debugFprintln("FormatOptions", localVarOptionals.FormatOptions)
debugFprintln("TranslationKeyPrefix", localVarOptionals.TranslationKeyPrefix)

if async {
return target.downloadAsynchronously(client, localeFile, localVarOptionals)
Expand Down
7 changes: 7 additions & 0 deletions clients/cli/cmd/internal/push_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/antihax/optional"
"github.com/phrase/phrase-cli/cmd/internal/paths"
"github.com/phrase/phrase-cli/cmd/internal/placeholders"
"github.com/phrase/phrase-go/v3"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -199,6 +200,12 @@ func (source *Source) uploadFile(client *phrase.APIClient, localeFile *LocaleFil
params.Branch = optional.NewString(branch)
}

translationKeyPrefix, err := placeholders.ResolveTranslationKeyPrefix(params.UploadCreateOpts.TranslationKeyPrefix, localeFile.Path)
if err != nil {
return nil, err
}
params.UploadCreateOpts.TranslationKeyPrefix = translationKeyPrefix

upload, _, err := client.UploadsApi.UploadCreate(Auth, source.ProjectID, file, params.FileFormat.Value(), params.LocaleId.Value(), &params.UploadCreateOpts)

return &upload, err
Expand Down
4 changes: 2 additions & 2 deletions clients/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/phrase/phrase-go/v3 v3.3.0 h1:kq2eFgKE6mUUZpud1KWsTa1RO4T+ztB2JI3DsCfqHog=
github.com/phrase/phrase-go/v3 v3.3.0/go.mod h1:s0uOYiXLxKAYlaIS6TbKv3efkKFUlY4OB6OL+VgvK90=
github.com/phrase/phrase-go/v3 v3.5.0 h1:zviffIun5A10PNnnSV0ynHjs+VRY7fc9xXVtNOjkm3o=
github.com/phrase/phrase-go/v3 v3.5.0/go.mod h1:s0uOYiXLxKAYlaIS6TbKv3efkKFUlY4OB6OL+VgvK90=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ public void localeDownloadTest() throws ApiException, IOException, InterruptedEx
String fallbackLocaleId = null;
String sourceLocaleId = null;
Object customMetadataFilters = null;
File response = api.localeDownload(projectId, id, xPhraseAppOTP, ifModifiedSince, ifNoneMatch, branch, fileFormat, tags, tag, includeEmptyTranslations, excludeEmptyZeroForms, includeTranslatedKeys, keepNotranslateTags, convertEmoji, formatOptions, encoding, skipUnverifiedTranslations, includeUnverifiedTranslations, useLastReviewedVersion, fallbackLocaleId, sourceLocaleId, customMetadataFilters);
String translationKeyPrefix = null;
Boolean filterByPrefix = null;
File response = api.localeDownload(projectId, id, xPhraseAppOTP, ifModifiedSince, ifNoneMatch,
branch, fileFormat, tags, tag, includeEmptyTranslations, excludeEmptyZeroForms,
includeTranslatedKeys, keepNotranslateTags, convertEmoji, formatOptions, encoding,
skipUnverifiedTranslations, includeUnverifiedTranslations, useLastReviewedVersion,
fallbackLocaleId, sourceLocaleId, translationKeyPrefix, filterByPrefix,
customMetadataFilters);

String fileContents = new String(java.nio.file.Files.readAllBytes(response.toPath()));
Assert.assertEquals("Correct file contents", fileContents, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void uploadCreateTest() throws ApiException, IOException, InterruptedExce
Boolean autotranslate = null;
Boolean markReviewed = null;
Boolean tagOnlyAffectedKeys = null;
String translationKeyPrefix = null;

Map<String, String> nestedFormatOptionsMap = new HashMap<>();
nestedFormatOptionsMap.put("nested_option", "sub_option");
Expand All @@ -118,7 +119,11 @@ public void uploadCreateTest() throws ApiException, IOException, InterruptedExce
formatOptionsMap.put("fallback_language", "en");
formatOptionsMap.put("more_options", nestedFormatOptionsMap);

Upload response = api.uploadCreate(projectId, file, fileFormat, localeId, xPhraseAppOTP, branch, tags, updateTranslations, updateTranslationKeys, updateTranslationsOnSourceMatch, updateDescriptions, convertEmoji, skipUploadTags, skipUnverification, fileEncoding, localeMapping, formatOptionsMap, autotranslate, markReviewed, tagOnlyAffectedKeys);
Upload response = api.uploadCreate(projectId, file, fileFormat, localeId, xPhraseAppOTP, branch,
tags, updateTranslations, updateTranslationKeys, updateTranslationsOnSourceMatch,
updateDescriptions, convertEmoji, skipUploadTags, skipUnverification, fileEncoding,
localeMapping, formatOptionsMap, autotranslate, markReviewed, tagOnlyAffectedKeys,
translationKeyPrefix);

Assert.assertEquals("valid id returned", "id_example", response.getId());
Assert.assertEquals("valid creation date returned", OffsetDateTime.parse("2015-01-28T09:52:53Z"), response.getCreatedAt());
Expand Down
2 changes: 2 additions & 0 deletions clients/php/test/Api/LocalesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public function testLocaleDownload()
null,
null,
null,
null,
null,
$custom_metadata_filters
);

Expand Down
23 changes: 23 additions & 0 deletions doc/compiled.json
Original file line number Diff line number Diff line change
Expand Up @@ -8747,6 +8747,24 @@
"type": "string"
}
},
{
"description": "Download all translation keys, and remove the specified prefix where possible. Warning: this may create duplicate key names if other keys share the same name after the prefix is removed.",
"example": "prefix_",
"name": "translation_key_prefix",
"in": "query",
"schema": {
"type": "string"
}
},
{
"description": "Only download translation keys containing the specified prefix, and remove the prefix from the generated file.",
"example": null,
"name": "filter_by_prefix",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "custom_metadata_filters",
"in": "query",
Expand Down Expand Up @@ -15031,6 +15049,11 @@
"type": "boolean",
"default": false,
"example": null
},
"translation_key_prefix": {
"description": "This prefix will be added to all uploaded translation key names to prevent collisions. Use a meaningful prefix related to your project or file to keep key names organized.",
"type": "string",
"example": "prefix_"
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions paths/locales/download.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ parameters:
in: query
schema:
type: string
- description: "Download all translation keys, and remove the specified prefix where possible. Warning: this may create duplicate key names if other keys share the same name after the prefix is removed."
example: "prefix_"
name: translation_key_prefix
in: query
schema:
type: string
- description: Only download translation keys containing the specified prefix, and remove the prefix from the generated file.
example:
name: filter_by_prefix
in: query
schema:
type: boolean
- name: custom_metadata_filters
in: query
description: |
Expand Down
4 changes: 4 additions & 0 deletions paths/uploads/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,8 @@ requestBody:
type: boolean
default: false
example:
translation_key_prefix:
description: This prefix will be added to all uploaded translation key names to prevent collisions. Use a meaningful prefix related to your project or file to keep key names organized.
type: string
example: prefix_
x-cli-version: '2.12'

0 comments on commit 9c9c959

Please sign in to comment.