Skip to content

Commit 5f0a3ce

Browse files
authored
feat(translations): edit, list pre-translations (#175)
1 parent 89707c6 commit 5f0a3ce

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

crowdin_api/api_resources/translations/enums.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ class CharTransformation(Enum):
1919
EUROPEAN = "european"
2020
ARABIC = "arabic"
2121
CYRILLIC = "cyrillic"
22+
23+
24+
class PreTranslationEditOperation(Enum):
25+
REPLACE = "replace"
26+
TEST = "test"

crowdin_api/api_resources/translations/resource.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from crowdin_api.api_resources.abstract.resources import BaseResource
44
from crowdin_api.api_resources.enums import ExportProjectTranslationFormat
5-
from crowdin_api.api_resources.translations.types import FallbackLanguages
5+
from crowdin_api.api_resources.translations.types import (
6+
FallbackLanguages,
7+
EditPreTranslationScheme,
8+
)
69
from crowdin_api.api_resources.translations.enums import (
710
CharTransformation,
811
PreTranslationApplyMethod,
@@ -48,6 +51,29 @@ def pre_translation_status(
4851
path=f"projects/{projectId}/pre-translations/{preTranslationId}",
4952
)
5053

54+
def list_pre_translations(
55+
self,
56+
projectId: Optional[int] = None,
57+
page: Optional[int] = None,
58+
offset: Optional[int] = None,
59+
limit: Optional[int] = None,
60+
):
61+
"""
62+
List Pre-Translations
63+
64+
Link to documentation:
65+
https://support.crowdin.com/developer/api/v2/#tag/Translations/operation/api.projects.pre-translations.getMany
66+
"""
67+
projectId = projectId or self.get_project_id()
68+
69+
params = self.get_page_params(page=page, offset=offset, limit=limit)
70+
71+
return self.requester.request(
72+
method="get",
73+
path=f"projects/{projectId}/pre-translations",
74+
params=params,
75+
)
76+
5177
def apply_pre_translation(
5278
self,
5379
languageIds: Iterable[str],
@@ -102,6 +128,26 @@ def apply_pre_translation(
102128
},
103129
)
104130

131+
def edit_pre_translation(
132+
self,
133+
preTranslationId: str,
134+
data: Iterable[EditPreTranslationScheme],
135+
projectId: Optional[int] = None,
136+
):
137+
"""
138+
Edit Pre-Translation
139+
140+
Link to documentation:
141+
https://support.crowdin.com/developer/api/v2/#tag/Translations/operation/api.projects.pre-translations.patch
142+
"""
143+
projectId = projectId or self.get_project_id()
144+
145+
return self.requester.request(
146+
method="patch",
147+
path=f"projects/{projectId}/pre-translations/{preTranslationId}",
148+
request_data=data,
149+
)
150+
105151
def build_project_directory_translation(
106152
self,
107153
directoryId: int,

crowdin_api/api_resources/translations/tests/test_translations_resources.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
CharTransformation,
77
PreTranslationApplyMethod,
88
PreTranslationAutoApproveOption,
9+
PreTranslationEditOperation,
910
)
1011
from crowdin_api.api_resources.translations.resource import TranslationsResource
1112
from crowdin_api.requester import APIRequester
@@ -35,6 +36,19 @@ def test_list_project_branches(self, m_request, base_absolut_url):
3536
path="projects/1/pre-translations/2",
3637
)
3738

39+
@mock.patch("crowdin_api.requester.APIRequester.request")
40+
def test_list_pre_translations(self, m_request, base_absolut_url):
41+
m_request.return_value = "response"
42+
43+
resource = self.get_resource(base_absolut_url)
44+
params = resource.get_page_params()
45+
assert resource.list_pre_translations(projectId=1) == "response"
46+
m_request.assert_called_once_with(
47+
method="get",
48+
path="projects/1/pre-translations",
49+
params=params,
50+
)
51+
3852
@pytest.mark.parametrize(
3953
"in_params, request_data",
4054
(
@@ -105,6 +119,26 @@ def test_apply_pre_translation(self, m_request, in_params, request_data, base_ab
105119
path="projects/1/pre-translations",
106120
)
107121

122+
@mock.patch("crowdin_api.requester.APIRequester.request")
123+
def test_edit_bundle(self, m_request, base_absolut_url):
124+
m_request.return_value = "response"
125+
126+
data = [
127+
{
128+
"value": "value",
129+
"op": PreTranslationEditOperation.REPLACE,
130+
"path": "/status",
131+
}
132+
]
133+
134+
resource = self.get_resource(base_absolut_url)
135+
assert resource.edit_pre_translation(projectId=1, preTranslationId="pre-id", data=data) == "response"
136+
m_request.assert_called_once_with(
137+
method="patch",
138+
path="projects/1/pre-translations/pre-id",
139+
request_data=data,
140+
)
141+
108142
@pytest.mark.parametrize(
109143
"in_params, request_data, headers",
110144
(

crowdin_api/api_resources/translations/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
from crowdin_api.typing import TypedDict
44

5+
from crowdin_api.api_resources.translations.enums import PreTranslationEditOperation
6+
57

68
class FallbackLanguages(TypedDict):
79
languageId: Iterable[str]
10+
11+
12+
class EditPreTranslationScheme(TypedDict):
13+
op: PreTranslationEditOperation
14+
path: str
15+
value: str

0 commit comments

Comments
 (0)