Golang code to do translation automatically
#1
I wrote a Go program that automatically translates text using the OpenAI API.

To use it:
1. Save the code in the inc/languages folder.
2. Replace OPENAI_API_KEY with your own API key.
3. Run the program, and the Chinese translations will be saved in the chinese folder.
4. Create a chinese.php file to complete the Chinese language pack.



package main
import (
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	openai "github.com/sashabaranov/go-openai"
)
func readFile(filePath string) (string, error) {
	content, err := ioutil.ReadFile(filePath)
	if err != nil {
		return "", err
	}
	return string(content), nil
}
func generateChinesePack(englishContent string) (string, error) {
	client := openai.NewClient("OPENAI_API_KEY")
	prompt := fmt.Sprintf("Generate a Chinese language pack based on the following English language pack, without any additional text, the output should be a valid php file:\n\n%s", englishContent)
	resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
		Model: openai.GPT4oMini,
		Messages: []openai.ChatCompletionMessage{
			{
				Role:    openai.ChatMessageRoleUser,
				Content: prompt,
			},
		}})
	if err != nil {
		return "", err
	}
	return resp.Choices[0].Message.Content, nil
}
func saveFile(filePath, content string) error {
	// Create the path for the chinese folder
	chineseFilePath := filepath.Join("chinese", filepath.Base(filePath))
	// Ensure the directory exists
	if err := os.MkdirAll(filepath.Dir(chineseFilePath), os.ModePerm); err != nil {
		return err
	}
	return ioutil.WriteFile(chineseFilePath, []byte(content), 0644)
}
func main() {
	englishFolder := "english"
	err := filepath.Walk(englishFolder, func(filePath string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !info.IsDir() && filepath.Ext(filePath) == ".php" {
			fmt.Printf("Processing file: %s\n", filePath)
			englishContent, err := readFile(filePath)
			if err != nil {
				fmt.Printf("Error reading file %s: %v\n", filePath, err)
				return nil // Continue with next file
			}
			if len(englishContent) == 0 {
				fmt.Printf("Warning: '%s' is empty.\n", filePath)
				return nil // Continue with next file
			}
			// Check if the file already exists in the chinese folder
			chineseFilePath := filepath.Join("chinese", filepath.Base(filePath))
			if _, err := os.Stat(chineseFilePath); err == nil {
				fmt.Printf("File already exists, skipping generation for: %s\n", chineseFilePath)
				return nil // Skip to the next file
			}
			fmt.Printf("Generating Chinese pack for %s...\n", filePath)
			chineseContent, err := generateChinesePack(englishContent)
			if err != nil {
				fmt.Printf("Error generating pack for %s: %v\n", filePath, err)
				return nil // Continue with next file
			}
			if err := saveFile(chineseFilePath, chineseContent); err != nil {
				fmt.Printf("Error saving file for %s: %v\n", filePath, err)
				return nil // Continue with next file
			}
			fmt.Printf("Saved: %s in 'chinese' folder.\n", chineseFilePath)
		}
		return nil
	})
	if err != nil {
		fmt.Println("Error walking through directory:", err)
	}
}
Reply
#2
Good idea. I tried with Python before but the translations were unfortunately not perfect.
MyBB Türkçe Destek Sitesi: https://mybbturkce.com

Please! Do not request support via private message unless I give permission. If you want paid consulting, you can contact us.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)