Skip to content

Commit

Permalink
Add chunk_size config and use in translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kargnas committed Jul 8, 2024
1 parent e0585eb commit 1fda012
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config/ai-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
'model' => 'gpt-3.5-turbo', // Recommend to use for testing purpose. It sometimes doesn't translate.
'api_key' => env('OPENAI_API_KEY'),
'retries' => 5,

// Translate strings in a batch. The higher, the cheaper.
'chunk_size' => 10,
],

'locale_names' => [
Expand Down
8 changes: 5 additions & 3 deletions src/Console/TranslateStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TranslateStrings extends Command

protected $sourceLocale;
protected $sourceDirectory;
protected $chunkSize;

public function __construct() {
parent::__construct();
Expand All @@ -25,6 +26,7 @@ public function __construct() {
public function handle() {
$this->sourceLocale = config('ai-translator.source_locale');
$this->sourceDirectory = config('ai-translator.source_directory');
$this->chunkSize = config('ai-translator.chunk_size', 10);

$this->translate();
}
Expand Down Expand Up @@ -86,8 +88,8 @@ public function translate() {
})
->toArray();

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

0 comments on commit 1fda012

Please sign in to comment.