Skip to content

Commit

Permalink
Merge pull request #33 from lokalise/bugfix/SGO-10_key_translation_pl…
Browse files Browse the repository at this point in the history
…ural_form_object

SGO-10 Plural key translation
  • Loading branch information
andrewLab-lokalise authored Oct 21, 2021
2 parents b847925 + 301df61 commit 5a3f474
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 1 deletion.
109 changes: 109 additions & 0 deletions svc_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,115 @@ func TestKeyService_Create_AutomationsDisabled(t *testing.T) {
}
}

func TestKeyService_Create_PluralTranslationEncoded(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc(
fmt.Sprintf("/projects/%s/keys", testProjectID),
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
testMethod(t, r, "POST")
testHeader(t, r, apiTokenHeader, testApiToken)
data := `{
"keys": [
{
"key_name": "index.welcome",
"description": "Index app welcome",
"platforms": [
"web"
],
"translations": [
{
"language_iso": "en",
"translation": {
"one": "oneText",
"other": "otherText"
}
}
],
"is_plural": true
}
]
}`

req := new(bytes.Buffer)
_ = json.Compact(req, []byte(data))

testBody(t, r, req.String())

_, _ = fmt.Fprint(w, `{
"project_id": "3002780358964f9bab5a92.87762498",
"keys": [
{
"key_id": 331223,
"created_at": "2018-12-31 12:00:00 (Etc/UTC)",
"created_at_timestamp": 1546257600,
"key_name": {
"ios": "index.welcome",
"android": "index.welcome",
"web": "index.welcome",
"other": "index.welcome"
},
"filenames": {
"ios": "",
"android": "",
"web": "",
"other": ""
},
"description": "Index app welcome",
"platforms": [
"web"
],
"tags": [],
"comments": [],
"screenshots": [],
"translations": [
{
"translation_id": 444921,
"key_id": 331223,
"language_iso": "en",
"translation": "Welcome",
"modified_by": 420,
"modified_by_email": "[email protected]",
"modified_at": "2018-12-31 12:00:00 (Etc/UTC)",
"modified_at_timestamp": 1546257600,
"is_reviewed": false,
"reviewed_by": 0,
"words": 0
}
]
}
],
"errors": [
{
"message": "This key name is already taken",
"code": 400,
"key": {
"key_name": "index.hello"
}
}
]
}`)
})

_, _ = client.Keys().Create(testProjectID, []NewKey{
{
KeyName: "index.welcome",
Description: "Index app welcome",
Platforms: []string{"web"},
IsPlural: true,
Translations: []NewTranslation{
{
LanguageISO: "en",
Translation: "{\"one\":\"oneText\",\"other\":\"otherText\"}",
},
},
},
})

}

func TestKeyService_Delete(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down
110 changes: 109 additions & 1 deletion svc_translation.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lokalise

import (
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -53,13 +53,121 @@ type NewTranslation struct {
MergeCustomTranslationStatuses bool `json:"merge_custom_translation_statuses,omitempty"`
}

func (t NewTranslation) MarshalJSON() ([]byte, error) {
type Alias NewTranslation

var translation interface{} = t.Translation

if json.Valid([]byte(t.Translation)) {
_ = json.Unmarshal([]byte(t.Translation), &translation)
}

return json.Marshal(&struct {
LanguageISO string `json:"language_iso"`
Translation interface{} `json:"translation"`
Alias
}{
LanguageISO: t.LanguageISO,
Translation: translation,
Alias: (Alias)(t),
})
}

func (t *NewTranslation) UnmarshalJSON(data []byte) error {
type Alias NewTranslation
aux := &struct {
LanguageISO string `json:"language_iso"`
Translation interface{} `json:"translation"`
*Alias
}{
Alias: (*Alias)(t),
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

switch aux.Translation.(type) {
case map[string]interface{}:
marshal, err := json.Marshal(aux.Translation)
if err != nil {
return err
}
t.Translation = string(marshal)
case string:
t.Translation = fmt.Sprintf("%+v", aux.Translation)
default:
return fmt.Errorf("NewTranslation tranlation type is of unknown type")
}

t.LanguageISO = aux.LanguageISO
t.IsFuzzy = aux.IsFuzzy
t.IsReviewed = aux.IsReviewed
t.CustomTranslationStatusIds = aux.CustomTranslationStatusIds
t.MergeCustomTranslationStatuses = aux.MergeCustomTranslationStatuses

return nil
}

type UpdateTranslation struct {
Translation string `json:"translation"`
IsFuzzy *bool `json:"is_fuzzy,omitempty"`
IsReviewed bool `json:"is_reviewed,omitempty"`
CustomTranslationStatusIDs []string `json:"custom_translation_status_ids,omitempty"`
}

func (t UpdateTranslation) MarshalJSON() ([]byte, error) {
type Alias UpdateTranslation

var translation interface{} = t.Translation

if json.Valid([]byte(t.Translation)) {
_ = json.Unmarshal([]byte(t.Translation), &translation)
}

return json.Marshal(&struct {
Translation interface{} `json:"translation"`
Alias
}{
Translation: translation,
Alias: (Alias)(t),
})
}

func (t *UpdateTranslation) UnmarshalJSON(data []byte) error {
type Alias UpdateTranslation
aux := &struct {
LanguageISO string `json:"language_iso"`
Translation interface{} `json:"translation"`
*Alias
}{
Alias: (*Alias)(t),
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

switch aux.Translation.(type) {
case map[string]interface{}:
marshal, err := json.Marshal(aux.Translation)
if err != nil {
return err
}
t.Translation = string(marshal)
case string:
t.Translation = fmt.Sprintf("%+v", aux.Translation)
default:
return fmt.Errorf("NewTranslation tranlation type is of unknown type")
}

t.IsFuzzy = aux.IsFuzzy
t.IsReviewed = aux.IsReviewed
t.CustomTranslationStatusIDs = aux.CustomTranslationStatusIDs

return nil
}

type TranslationsResponse struct {
Paged
Translations []Translation `json:"translations"`
Expand Down
Loading

0 comments on commit 5a3f474

Please sign in to comment.