Skip to content

Commit f330039

Browse files
feat: Add style rules CRUD methods
1 parent 9cd9d39 commit f330039

4 files changed

Lines changed: 575 additions & 6 deletions

File tree

README.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,34 @@ Style rules allow you to customize your translations using a managed, shared lis
633633
of rules for style, formatting, and more. Multiple style rules can be stored with
634634
your account, each with a user-specified name and a uniquely-assigned ID.
635635

636-
#### Creating and managing style rules
636+
#### Creating a style rule
637637

638-
Currently style rules must be created and managed in the DeepL UI via
639-
https://www.deepl.com/en/custom-rules. Full CRUD functionality via the APIs will
640-
come shortly.
638+
Use `createStyleRule()` to create a new style rule with a name and language.
639+
You can optionally pass configured rules and custom instructions:
640+
641+
```php
642+
// Create a basic style rule
643+
$styleRule = $deeplClient->createStyleRule('My Style', 'en');
644+
echo "Created: {$styleRule->name} ({$styleRule->styleId})\n";
645+
646+
// Create with configured rules and custom instructions
647+
$styleRule = $deeplClient->createStyleRule(
648+
'Formal English',
649+
'en',
650+
['style_and_tone' => ['formal_informal' => 'formal']],
651+
[['label' => 'Formality', 'prompt' => 'Always use formal language']]
652+
);
653+
```
654+
655+
#### Getting a style rule
656+
657+
Use `getStyleRule()` to retrieve a single style rule by its ID or a
658+
`StyleRuleInfo` object:
659+
660+
```php
661+
$styleRule = $deeplClient->getStyleRule('dca2e053-8ae5-45e6-a0d2-881156e7f4e4');
662+
echo "{$styleRule->name} ({$styleRule->language})\n";
663+
```
641664

642665
#### Listing all style rules
643666

@@ -663,6 +686,60 @@ foreach ($styleRulesDetailed as $rule) {
663686
}
664687
```
665688

689+
#### Updating a style rule
690+
691+
Use `updateStyleRuleName()` to rename a style rule, and
692+
`updateStyleRuleConfiguredRules()` to replace the configured rules:
693+
694+
```php
695+
// Update the name
696+
$updated = $deeplClient->updateStyleRuleName($styleRule, 'New Name');
697+
698+
// Replace configured rules
699+
$updated = $deeplClient->updateStyleRuleConfiguredRules($styleRule, [
700+
'style_and_tone' => ['formal_informal' => 'informal'],
701+
'punctuation' => ['oxford_comma' => 'always'],
702+
]);
703+
```
704+
705+
#### Deleting a style rule
706+
707+
Use `deleteStyleRule()` to delete a style rule:
708+
709+
```php
710+
$deeplClient->deleteStyleRule($styleRule);
711+
```
712+
713+
#### Managing custom instructions
714+
715+
Custom instructions allow you to add free-text prompts to a style rule. Use the
716+
custom instruction methods to create, get, update, and delete them:
717+
718+
```php
719+
// Create a custom instruction
720+
$instruction = $deeplClient->createStyleRuleCustomInstruction(
721+
$styleRule,
722+
'Formality',
723+
'Always use formal language',
724+
'de' // optional source language
725+
);
726+
echo "Instruction ID: {$instruction->id}\n";
727+
728+
// Get a custom instruction
729+
$instruction = $deeplClient->getStyleRuleCustomInstruction($styleRule, $instruction->id);
730+
731+
// Update a custom instruction
732+
$updated = $deeplClient->updateStyleRuleCustomInstruction(
733+
$styleRule,
734+
$instruction->id,
735+
'Updated Label',
736+
'Use very formal language'
737+
);
738+
739+
// Delete a custom instruction
740+
$deeplClient->deleteStyleRuleCustomInstruction($styleRule, $instruction->id);
741+
```
742+
666743
#### Using a stored style rule
667744

668745
You can use a stored style rule for text translation by setting the `style_id`

src/CustomInstruction.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
class CustomInstruction
1313
{
14+
/** @var string|null Unique ID assigned to the custom instruction. */
15+
public $id;
16+
1417
/** @var string Label for the custom instruction. */
1518
public $label;
1619

@@ -23,8 +26,10 @@ class CustomInstruction
2326
public function __construct(
2427
string $label,
2528
string $prompt,
26-
?string $sourceLanguage = null
29+
?string $sourceLanguage = null,
30+
?string $id = null
2731
) {
32+
$this->id = $id;
2833
$this->label = $label;
2934
$this->prompt = $prompt;
3035
$this->sourceLanguage = $sourceLanguage;
@@ -38,7 +43,8 @@ public static function fromJson(array $json): CustomInstruction
3843
return new CustomInstruction(
3944
$json['label'],
4045
$json['prompt'],
41-
$json['source_language'] ?? null
46+
$json['source_language'] ?? null,
47+
$json['id'] ?? null
4248
);
4349
}
4450
}

0 commit comments

Comments
 (0)