Skip to content

Commit b604182

Browse files
committed
Add docs about preserveParameters()` method.
1 parent d142619 commit b604182

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,36 @@ Return value will be `null` if the language couldn't be detected.
9393

9494
Supported languages are listed in [Google API docs](https://cloud.google.com/translate/docs/languages).
9595

96+
### Preserving Parameters
97+
98+
The `preserveParameters()` method allows you to preserve certain parameters in strings while performing translations. This is particularly useful when dealing with localization files or templating engines where specific placeholders need to be excluded from translation.
99+
100+
Default regex is `/:(\w+)/` which covers parameters starting with `:`. Useful for translating language files of Laravel and other frameworks. You can also pass your custom regex to modify the parameter syntax.
101+
102+
```php
103+
$tr = new GoogleTranslate('de');
104+
105+
$text = $tr->translate('Page :current of :total'); // Seite :aktuell von :gesamt
106+
107+
$text = $tr->preserveParameters()
108+
->translate('Page :current of :total'); // Seite :current von :total
109+
```
110+
111+
Or use custom regex:
112+
113+
```php
114+
$text = $tr->preserveParameters('/\{\{([^}]+)\}\}/')
115+
->translate('Page {{current}} of {{total}}'); // Seite {{current}} von {{total}}
116+
```
117+
118+
You can use same feature with static `trans()` method too.
119+
120+
```php
121+
GoogleTranslate::trans('Welcome :name', 'fr', preserveParameters: true); // Default regex
122+
123+
GoogleTranslate::trans('Welcome {{name}}', 'fr', preserveParameters: '/\{\{([^}]+)\}\}/'); // Custom regex
124+
```
125+
96126
### Using Raw Response
97127

98128
For advanced usage, you might need the raw results that Google Translate provides. you can use `getResponse` method for that.

0 commit comments

Comments
 (0)