Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
0
Indexable
Never
   $langs = Langs::getAll();
        $translatedLang = Langs::getDefault();
        
        $langs = array_filter($langs, fn($e) => $e !== $translatedLang);
        
        $translations = array_values(Translations::getAll($translatedLang));
        
        $words = array_map(function($word){
            return '"'.$word.'"';
        },$translations);
   
        $result = OpenAI::translatePrompt($langs,$words);
        
        $content = array_keys($result);
        
        $ids = ContentTranslation::whereContainerName('translations')->whereIn('content',$content)->get()->keyBy('container_id')->toArray();
        $word_to_id = [];
        foreach($ids as $id){
            $word_to_id[$id['content']] = $id['container_id'];
        }
        $ids = array_keys($ids);
        
        $content_translations = ContentTranslation::whereIn('container_id',$ids)->whereContainerName('translations')->where('lang','!=',$translatedLang)->get();
        foreach($result as $word => $answers){
            foreach($answers as $lang => $content){
                $translation = $content_translations->where('container_id',$word_to_id[$word])->where('lang',$lang)->first();
                if ($translation === null) {
                    dd(['id' => $word_to_id[$word],'lang' => $lang,'translations' => $content_translations]);
                }
                $translation->content = $content;
                $translation->save();
            }
        }
Leave a Comment