77
88Manage all your laravel translations easily:
99
10- - Translate strings to other languages (DeepL, OpenAI or any custom service)
11- - Proofread your translations strings and automatically fix grammar and syntax (OpenAI, or any custome service)
12- - Find missing translations strings in all your locales
13- - Find dead translations keys (keys not used anywhere in your codebase)
14- - Sort your tranlations in natural order
15- - Format your translations files
10+ - ** Translate** strings to other languages (DeepL, OpenAI or any custom service)
11+ - ** Proofread** your translations strings and automatically fix grammar and syntax (OpenAI, or any custome service)
12+ - ** Find missing** translations strings in all your locales
13+ - ** Find dead** translations keys (keys not used anywhere in your codebase)
14+ - ** Sort** your tranlations in natural order
1615
1716## Installation
1817
@@ -22,7 +21,7 @@ You can install the package via composer:
2221composer require-dev elegantly/laravel-translator
2322```
2423
25- You can publish the config file with:
24+ Then publish the config file with:
2625
2726``` bash
2827php artisan vendor:publish --tag=" translator-config"
@@ -40,12 +39,20 @@ return [
4039 */
4140 'sort_keys' => false,
4241
42+ 'services' => [
43+ 'openai' => [
44+ 'key' => env('OPENAI_API_KEY'),
45+ 'organization' => env('OPENAI_ORGANIZATION'),
46+ 'request_timeout' => env('OPENAI_REQUEST_TIMEOUT'),
47+ ],
48+ 'deepl' => [
49+ 'key' => env('DEEPL_KEY'),
50+ ],
51+ ],
52+
4353 'translate' => [
4454 'service' => 'openai',
4555 'services' => [
46- 'deepl' => [
47- 'key' => env('DEEPL_KEY'),
48- ],
4956 'openai' => [
5057 'model' => 'gpt-4o',
5158 'prompt' => "Translate the following json to the locale '{targetLocale}' while preserving the keys.",
@@ -73,20 +80,20 @@ return [
7380 'service' => 'php-parser',
7481
7582 /**
76- * Files or directories to include
83+ * Files or directories to include in the deadcode scan
7784 */
7885 'paths' => [
7986 app_path(),
8087 resource_path(),
8188 ],
8289
8390 /**
84- * Files or directories to exclude
91+ * Files or directories to exclude from the deadcode scan
8592 */
8693 'excluded_paths' => [],
8794
8895 /**
89- * Translations to exclude from deadcode detection
96+ * Translations keys to exclude from deadcode detection
9097 */
9198 'ignored_translations' => [
9299 // 'validation',
@@ -105,92 +112,208 @@ return [
105112];
106113```
107114
108- ## Usage
115+ ## Translate your strings automatically
116+
117+ Before translating anyhting, you must chose and setup a translation service. This package includes two services by default:
109118
110- This package can be used:
119+ - OpenAI
120+ - DeepL
111121
112- - Like a CLI tool, using commands.
113- - In a programmatic way using ` \Elegantly\Translator\Facades\Translator::class ` facade.
122+ ### Setting up OpenAI
114123
115- ### Sort all translations in natural order
124+ First configure the OpenAI key in the config file or define the env value:
116125
117- You can format and sort all your php translations files using:
126+ ``` php
127+ return [
128+ // ...
118129
119- ``` bash
120- php artisan translator:sort
130+ 'services' => [
131+ 'openai' => [
132+ 'key' => env('OPENAI_API_KEY'),
133+ 'organization' => env('OPENAI_ORGANIZATION'),
134+ 'request_timeout' => env('OPENAI_REQUEST_TIMEOUT'),
135+ ],
136+ ],
137+
138+ // ...
139+ ]
121140```
122141
142+ ### Setting up DeepL
143+
144+ First configure the DeepL key in the config file or define the env value:
145+
123146``` php
124- use Elegantly\Translator\Facades\Translator;
147+ return [
148+ // ...
125149
126- Translator::sortAllTranslations();
127- ```
150+ 'services' => [
151+ // ...
152+ 'deepl' => [
153+ 'key' => env('DEEPL_KEY'),
154+ ],
155+ ],
128156
129- ### Find the missing translations
157+ // ...
158+ ]
159+ ```
130160
131- You can display all the missing translations keys defined for a given locale but not for the other ones:
161+ ### From CLI
132162
133163``` bash
134- php artisan translator:missing fr
164+ php artisan translator:translate
135165```
136166
167+ ### From code
168+
137169``` php
138170use Elegantly\Translator\Facades\Translator;
139171
140- Translator::getAllMissingTranslations('fr');
172+ // Translate strings defined in php files
173+ Translator::translateTranslations(
174+ source: 'fr',
175+ target: 'en',
176+ namespace: 'validation',
177+ keys: ['title', ...]
178+ );
179+
180+ // Translate strings defined in JSON files
181+ Translator::translateTranslations(
182+ source: 'fr',
183+ target: 'en',
184+ namespace: null,
185+ keys: ['title', ...]
186+ );
141187```
142188
143- ### Auto translate strings
189+ ## Proofread your translations
144190
145- This package can automatically translate your translations strings for you .
146- Right now, it includes 2 services :
191+ This package allow you to proofread (i.e fix grammar and syntax) your translations strings.
192+ For now the package includes one service :
147193
148- - DeepL
149194- OpenAI
150195
151- You can also define your own service.
196+ But you can create you own service if you need .
152197
153- ### Auto-translate using DeepL
198+ ### Setting up OpenAI
154199
155- First, you need to edit the config file to add your DeepL api key and select deepl as your service :
200+ First configure the OpenAI key in the config file or define the env value :
156201
157202``` php
158203return [
159- 'translate' => [
160- 'service' => 'deepl', // select the default service here
161-
162- 'services' => [
163- 'deepl' => [
164- 'key' => env('DEEPL_KEY'), // add you api key here
165- ],
204+ // ...
166205
206+ 'services' => [
207+ 'openai' => [
208+ 'key' => env('OPENAI_API_KEY'),
209+ 'organization' => env('OPENAI_ORGANIZATION'),
210+ 'request_timeout' => env('OPENAI_REQUEST_TIMEOUT'),
167211 ],
168212 ],
213+
214+ // ...
169215]
170216```
171217
172- To translate all the missing translations use:
218+ ### From CLI
173219
174220``` bash
175- php artisan translator:translate
221+ php artisan translator:proofread
176222```
177223
178- To translate all translations use:
224+ ### From code
225+
226+ ``` php
227+ use Elegantly\Translator\Facades\Translator;
228+
229+ // proofread translations strings defined in php files
230+ Translator::proofreadTranslations(
231+ locale: 'fr',
232+ namespace: 'auth',
233+ keys: ['title', ...]
234+ );
235+
236+ // proofread translations strings defined in JSON files
237+ Translator::proofreadTranslations(
238+ locale: 'fr',
239+ namespace: null,
240+ keys: ['title', ...]
241+ );
242+ ```
243+
244+ ## Find missing translations
245+
246+ ### From CLI
179247
180248``` bash
181- php artisan translator:translate --all
249+ php artisan translator:missing
182250```
183251
184- Ommitting the ` --to ` option will translate to every available languages in your project.
252+ ### From code
185253
186254``` php
187- use Elegantly\Translator\Facades\Translator;
255+ // compare /fr/validation.php and /en/validation.php
256+ Translator::getMissingTranslations(
257+ source: 'fr',
258+ target: 'en',
259+ namespace: 'validation'
260+ );
188261
189- Translator::translateTranslations(
262+ // compare /fr.json and /en.json
263+ Translator::getMissingTranslations(
190264 source: 'fr',
191265 target: 'en',
192- namespace: 'namespace-file-or-null',
193- keys: ['title', ...]
266+ namespace: null
267+ );
268+ ```
269+
270+ ## Find dead translations
271+
272+ ### From CLI
273+
274+ ``` bash
275+ php artisan translator:dead
276+ ```
277+
278+ ### From code
279+
280+ ``` php
281+ // compare /fr/validation.php and /en/validation.php
282+ Translator::getDeadTranslations(
283+ locale: 'fr',
284+ namespace: 'validation'
285+ );
286+
287+ // compare /fr.json and /en.json
288+ Translator::getDeadTranslations(
289+ locale: 'fr',
290+ namespace: null
291+ );
292+ ```
293+
294+ ## Sort & format your translations files
295+
296+ ### From CLI
297+
298+ ``` bash
299+ php artisan translator:sort
300+ ```
301+
302+ ### From code
303+
304+ ``` php
305+ use Elegantly\Translator\Facades\Translator;
306+
307+ // sort translations from `/fr/validation.php`
308+ Translator::sortTranslations(
309+ locale: 'fr',
310+ namespace: 'validation'
311+ );
312+
313+ // sort translations from `/fr.json`
314+ Translator::sortTranslations(
315+ locale: 'fr',
316+ namespace: null
194317);
195318```
196319
0 commit comments