Skip to content

Commit fa1214b

Browse files
feat: Add support for style rules CRUD endpoints
1 parent e506b68 commit fa1214b

File tree

7 files changed

+548
-14
lines changed

7 files changed

+548
-14
lines changed

README.md

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -644,36 +644,112 @@ target language English (`"EN"`) supports translations to both American English
644644
### Style Rules
645645

646646
Style rules allow you to customize your translations using a managed, shared list
647-
of rules for style, formatting, and more. Multiple style rules can be stored with
647+
of rules for style, formatting, and more. Multiple style rules can be stored with
648648
your account, each with a user-specified name and a uniquely-assigned ID.
649649

650-
#### Creating and managing style rules
650+
#### Creating a style rule
651651

652-
Currently style rules must be created and managed in the DeepL UI via
653-
https://www.deepl.com/en/custom-rules. Full CRUD functionality via the APIs will
654-
come shortly.
652+
Use `create_style_rule()` to create a new style rule with a name and language
653+
code. You can optionally provide `configured_rules` and `custom_instructions`.
655654

656-
#### Listing all style rules
655+
```python
656+
style_rule = deepl_client.create_style_rule(
657+
name="My Style Rule",
658+
language="en",
659+
)
660+
print(f"Created: {style_rule.name} ({style_rule.style_id})")
661+
```
662+
663+
#### Retrieving and listing style rules
664+
665+
Use `get_style_rule()` to retrieve a single style rule by ID, or
666+
`get_all_style_rules()` to list all style rules.
657667

658668
`get_all_style_rules()` returns a list of `StyleRuleInfo` objects
659669
corresponding to all of your stored style rules. The method accepts optional
660670
parameters: `page` (page number for pagination, 0-indexed), `page_size` (number
661-
of items per page), and `detailed` (whether to include detailed configuration
662-
rules in the `configured_rules` property).
671+
of items per page), and `detailed`. When `True`, the response includes
672+
`configured_rules` and `custom_instructions` for each style rule. When `False`
673+
(default), these fields are omitted for faster responses.
663674

664675
```python
665-
# Get all style rules
676+
# Get a single style rule by ID
677+
style_rule = deepl_client.get_style_rule("YOUR_STYLE_ID")
678+
print(f"{style_rule.name} ({style_rule.language})")
679+
680+
# List all style rules
666681
style_rules = deepl_client.get_all_style_rules()
667682
for rule in style_rules:
668683
print(f"{rule.name} ({rule.style_id})")
669684

670-
# Get style rules with detailed configuration
685+
# List with detailed configuration
671686
style_rules = deepl_client.get_all_style_rules(detailed=True)
672687
for rule in style_rules:
673688
if rule.configured_rules:
674689
print(f" Number formatting: {rule.configured_rules.numbers}")
675690
```
676691

692+
#### Updating a style rule
693+
694+
Use `update_style_rule_name()` to rename a style rule, and
695+
`update_style_rule_configured_rules()` to update its configured rules.
696+
697+
```python
698+
# Update the name
699+
updated = deepl_client.update_style_rule_name("YOUR_STYLE_ID", "New Name")
700+
701+
# Update configured rules
702+
updated = deepl_client.update_style_rule_configured_rules(
703+
"YOUR_STYLE_ID",
704+
{"style_and_tone": {"formality": "formal"}},
705+
)
706+
```
707+
708+
#### Managing custom instructions
709+
710+
Custom instructions allow you to add free-text prompts to a style rule. Use
711+
`create_style_rule_custom_instruction()`, `get_style_rule_custom_instruction()`,
712+
`update_style_rule_custom_instruction()`, and
713+
`delete_style_rule_custom_instruction()` to manage them.
714+
715+
```python
716+
# Create a custom instruction
717+
instruction = deepl_client.create_style_rule_custom_instruction(
718+
"YOUR_STYLE_ID",
719+
label="Formal tone",
720+
prompt="Always use formal language",
721+
)
722+
print(f"Created instruction: {instruction.id}")
723+
724+
# Get a custom instruction
725+
instruction = deepl_client.get_style_rule_custom_instruction(
726+
"YOUR_STYLE_ID", instruction.id
727+
)
728+
729+
# Update a custom instruction
730+
updated = deepl_client.update_style_rule_custom_instruction(
731+
"YOUR_STYLE_ID",
732+
instruction.id,
733+
label="Updated label",
734+
prompt="Use very formal language",
735+
)
736+
737+
# Delete a custom instruction
738+
deepl_client.delete_style_rule_custom_instruction(
739+
"YOUR_STYLE_ID", instruction.id
740+
)
741+
```
742+
743+
#### Deleting a style rule
744+
745+
Use `delete_style_rule()` to delete a style rule by ID.
746+
747+
```python
748+
deepl_client.delete_style_rule("YOUR_STYLE_ID")
749+
```
750+
751+
#### Using a style rule in translations
752+
677753
Style rules can also be used with the command line interface for text translation:
678754

679755
```bash

deepl/api_data.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,10 +865,12 @@ def __init__(
865865
label: str,
866866
prompt: str,
867867
source_language: Optional[str] = None,
868+
id: Optional[str] = None,
868869
):
869870
self._label = label
870871
self._prompt = prompt
871872
self._source_language = source_language
873+
self._id = id
872874

873875
@staticmethod
874876
def from_json(json) -> "CustomInstruction":
@@ -877,8 +879,14 @@ def from_json(json) -> "CustomInstruction":
877879
label=json["label"],
878880
prompt=json["prompt"],
879881
source_language=json.get("source_language"),
882+
id=json.get("id"),
880883
)
881884

885+
@property
886+
def id(self) -> Optional[str]:
887+
"""Returns the unique ID of the custom instruction."""
888+
return self._id
889+
882890
@property
883891
def label(self) -> str:
884892
"""Returns the label of the custom instruction."""

0 commit comments

Comments
 (0)