Skip to content

Commit 1fda012

Browse files
committed
Add chunk_size config and use in translation
1 parent e0585eb commit 1fda012

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

config/ai-translator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
'model' => 'gpt-3.5-turbo', // Recommend to use for testing purpose. It sometimes doesn't translate.
2222
'api_key' => env('OPENAI_API_KEY'),
2323
'retries' => 5,
24+
25+
// Translate strings in a batch. The higher, the cheaper.
26+
'chunk_size' => 10,
2427
],
2528

2629
'locale_names' => [

src/Console/TranslateStrings.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TranslateStrings extends Command
1313

1414
protected $sourceLocale;
1515
protected $sourceDirectory;
16+
protected $chunkSize;
1617

1718
public function __construct() {
1819
parent::__construct();
@@ -25,6 +26,7 @@ public function __construct() {
2526
public function handle() {
2627
$this->sourceLocale = config('ai-translator.source_locale');
2728
$this->sourceDirectory = config('ai-translator.source_directory');
29+
$this->chunkSize = config('ai-translator.chunk_size', 10);
2830

2931
$this->translate();
3032
}
@@ -86,8 +88,8 @@ public function translate() {
8688
})
8789
->toArray();
8890

89-
if (sizeof($sourceStringList) > 50) {
90-
if (!$this->confirm("{$outputFile}, Strings: " . sizeof($sourceStringList) . " -> Too many strings to translate. Could be expensive. Continue?")) {
91+
if (sizeof($sourceStringList) > 100) {
92+
if (!$this->confirm("{$outputFile}, Strings: " . sizeof($sourceStringList) . " -> Many strings to translate. Could be expensive. Continue?")) {
9193
$this->warn("Stopped translating!");
9294
exit;
9395
}
@@ -96,7 +98,7 @@ public function translate() {
9698
// Chunk the strings because of the pricing
9799
// But also this will increase the speed of the translation, and quality of continuous translation
98100
collect($sourceStringList)
99-
->chunk(10)
101+
->chunk($this->chunkSize)
100102
->each(function ($chunk) use ($locale, $file, $targetStringTransformer) {
101103
$translator = new AIProvider(
102104
filename: $file,

0 commit comments

Comments
 (0)