Skip to content

Commit 2dad1ab

Browse files
feat: Add style rules CRUD endpoints
1 parent 207736e commit 2dad1ab

5 files changed

Lines changed: 505 additions & 18 deletions

File tree

README.md

Lines changed: 103 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -657,42 +657,128 @@ Style rules allow you to customize your translations using a managed, shared lis
657657
of rules for style, formatting, and more. Multiple style rules can be stored with
658658
your account, each with a user-specified name and a uniquely-assigned ID.
659659

660-
#### Creating and managing style rules
660+
#### Creating a style rule
661661

662-
Currently style rules must be created and managed in the DeepL UI via
663-
https://www.deepl.com/en/custom-rules. Full CRUD functionality via the APIs will
664-
come shortly.
662+
Use `createStyleRule()` to create a new style rule. You must specify a name and
663+
language code. You may optionally provide configured rules and custom
664+
instructions:
665665

666-
#### Listing all style rules
666+
```java
667+
class Example { // Continuing class Example from above
668+
public void createStyleRuleExample() throws Exception {
669+
// Create a simple style rule
670+
StyleRuleInfo rule = client.createStyleRule("My Style", "en", null, null);
671+
System.out.println("Created: " + rule.getStyleId());
672+
673+
// Create with custom instructions
674+
List<CustomInstruction> instructions = new ArrayList<>();
675+
instructions.add(new CustomInstruction("Formal tone", "Use formal language", null));
676+
StyleRuleInfo ruleWithInstructions = client.createStyleRule(
677+
"Formal Style", "en", null, instructions);
678+
}
679+
}
680+
```
667681

668-
`getAllStyleRules()` returns a list of `StyleRuleInfo` objects
669-
corresponding to all of your stored style rules. The method accepts optional
670-
parameters: `page` (page number for pagination, 0-indexed), `pageSize` (number
671-
of items per page), and `detailed` (whether to include detailed configuration
672-
rules in the `configuredRules` property).
682+
#### Retrieving and listing style rules
683+
684+
`getStyleRule()` retrieves a single style rule by its ID. `getAllStyleRules()`
685+
returns a list of `StyleRuleInfo` objects corresponding to all of your stored
686+
style rules. The method accepts optional parameters: `page` (page number for
687+
pagination, 0-indexed), `pageSize` (number of items per page), and `detailed`.
688+
When `true`, the response includes `configuredRules` and `customInstructions`
689+
for each style rule. When `false` (default), these fields are omitted for faster
690+
responses.
673691

674692
```java
675693
class Example { // Continuing class Example from above
676-
public void styleRulesExample() throws Exception {
694+
public void listStyleRulesExample() throws Exception {
695+
// Get a single style rule by ID
696+
StyleRuleInfo rule = client.getStyleRule("dca2e053-8ae5-45e6-a0d2-881156e7f4e4");
697+
System.out.println(rule.getName());
698+
677699
// Get all style rules
678700
List<StyleRuleInfo> styleRules = client.getAllStyleRules();
679-
for (StyleRuleInfo rule : styleRules) {
680-
System.out.println(String.format("%s (%s)", rule.getName(), rule.getStyleId()));
701+
for (StyleRuleInfo r : styleRules) {
702+
System.out.println(String.format("%s (%s)", r.getName(), r.getStyleId()));
681703
}
682704

683705
// Get style rules with detailed configuration
684706
List<StyleRuleInfo> styleRulesDetailed = client.getAllStyleRules(null, null, true);
685-
for (StyleRuleInfo rule : styleRulesDetailed) {
686-
if (rule.getConfiguredRules() != null && rule.getConfiguredRules().getNumbers() != null) {
707+
for (StyleRuleInfo r : styleRulesDetailed) {
708+
if (r.getConfiguredRules() != null && r.getConfiguredRules().getNumbers() != null) {
687709
System.out.println(String.format("Number formatting rules: %s",
688-
String.join(", ", rule.getConfiguredRules().getNumbers().keySet())));
710+
String.join(", ", r.getConfiguredRules().getNumbers().keySet())));
689711
}
690712
}
691713
}
692714
}
693715
```
694716

695-
#### Using a stored style rule
717+
#### Updating a style rule
718+
719+
Use `updateStyleRuleName()` to rename a style rule, and
720+
`updateStyleRuleConfiguredRules()` to replace its configured rules:
721+
722+
```java
723+
class Example { // Continuing class Example from above
724+
public void updateStyleRuleExample() throws Exception {
725+
String styleId = "dca2e053-8ae5-45e6-a0d2-881156e7f4e4";
726+
727+
// Update the name
728+
StyleRuleInfo updated = client.updateStyleRuleName(styleId, "New Name");
729+
System.out.println("Updated name: " + updated.getName());
730+
731+
// Update configured rules
732+
ConfiguredRules configuredRules = new ConfiguredRules();
733+
StyleRuleInfo updatedRules = client.updateStyleRuleConfiguredRules(
734+
styleId, configuredRules);
735+
}
736+
}
737+
```
738+
739+
#### Managing custom instructions
740+
741+
Custom instructions can be created, retrieved, updated, and deleted individually
742+
within a style rule:
743+
744+
```java
745+
class Example { // Continuing class Example from above
746+
public void customInstructionsExample() throws Exception {
747+
String styleId = "dca2e053-8ae5-45e6-a0d2-881156e7f4e4";
748+
749+
// Create a custom instruction
750+
CustomInstruction instruction = client.createStyleRuleCustomInstruction(
751+
styleId, "Formal tone", "Always use formal language", null);
752+
String instructionId = instruction.getId();
753+
754+
// Retrieve the custom instruction
755+
CustomInstruction retrieved = client.getStyleRuleCustomInstruction(
756+
styleId, instructionId);
757+
System.out.println(retrieved.getLabel());
758+
759+
// Update the custom instruction
760+
CustomInstruction updated = client.updateStyleRuleCustomInstruction(
761+
styleId, instructionId, "Updated label", "Updated prompt", null);
762+
763+
// Delete the custom instruction
764+
client.deleteStyleRuleCustomInstruction(styleId, instructionId);
765+
}
766+
}
767+
```
768+
769+
#### Deleting a style rule
770+
771+
Use `deleteStyleRule()` to permanently remove a style rule from your account:
772+
773+
```java
774+
class Example { // Continuing class Example from above
775+
public void deleteStyleRuleExample() throws Exception {
776+
client.deleteStyleRule("dca2e053-8ae5-45e6-a0d2-881156e7f4e4");
777+
}
778+
}
779+
```
780+
781+
#### Using a stored style rule in translations
696782

697783
You can use a stored style rule for text translation by setting the `styleRule`
698784
argument to either the style rule ID or `StyleRuleInfo` object:

deepl-java/src/main/java/com/deepl/api/CustomInstruction.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
/** Custom instruction for a style rule. */
1010
public class CustomInstruction {
11+
@SerializedName(value = "id")
12+
@Nullable
13+
private final String id;
14+
1115
@SerializedName(value = "label")
1216
private final String label;
1317

@@ -21,16 +25,36 @@ public class CustomInstruction {
2125
/**
2226
* Initializes a new {@link CustomInstruction} containing a custom instruction for a style rule.
2327
*
28+
* @param id Optional unique identifier for the custom instruction.
2429
* @param label Label for the custom instruction.
2530
* @param prompt Prompt text for the custom instruction.
2631
* @param sourceLanguage Optional source language code for the custom instruction.
2732
*/
28-
public CustomInstruction(String label, String prompt, @Nullable String sourceLanguage) {
33+
public CustomInstruction(
34+
@Nullable String id, String label, String prompt, @Nullable String sourceLanguage) {
35+
this.id = id;
2936
this.label = label;
3037
this.prompt = prompt;
3138
this.sourceLanguage = sourceLanguage;
3239
}
3340

41+
/**
42+
* Initializes a new {@link CustomInstruction} containing a custom instruction for a style rule.
43+
*
44+
* @param label Label for the custom instruction.
45+
* @param prompt Prompt text for the custom instruction.
46+
* @param sourceLanguage Optional source language code for the custom instruction.
47+
*/
48+
public CustomInstruction(String label, String prompt, @Nullable String sourceLanguage) {
49+
this(null, label, prompt, sourceLanguage);
50+
}
51+
52+
/** @return Optional unique identifier for the custom instruction, or {@code null} if not set. */
53+
@Nullable
54+
public String getId() {
55+
return id;
56+
}
57+
3458
/** @return Label for the custom instruction. */
3559
public String getLabel() {
3660
return label;

0 commit comments

Comments
 (0)