Skip to content

Xarth-Mai/EasyI18n-Go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyI18n-Go

EasyI18n-Go is a simple internationalization (i18n) module for Go projects. It allows you to define translations for multiple languages and provides functionality to automatically set the current language based on environment variables.

💡 Features

  • Rapid deployment of multilingual support
  • Auto-set 13 languages from environment variables
  • Allows to manually set any language
  • Auto fallback to English
  • Provide a script to check translation keys

📝 Usage

1. Using the Translation Module

Use the translation module in your main.go file.

package main

import (
	"fmt"

	i18n "github.com/Xarth-Mai/EasyI18n-Go"
)

func main() {
	// Set custom translation data (required)
	i18n.SetCustomTranslations(EasyI18nTranslations)

	// Automatically set language
	i18n.InitLanguage()

	// Or manually specify language
	i18n.SetLanguage("jp")  	// You can provide any other language in the configuration file and use it like this

	// Use translation
	one := "World"
	fmt.Println(i18n.Translate("greeting", one))     // func Translate(key string, args ...interface{}) string {}
	fmt.Println(i18n.Translate("farewell", "World")) // Function supports string formatting
	fmt.Println(i18n.Translate("goodbye"))           // Returns English if the current language does not match
	fmt.Println(i18n.Translate("byebye", "World"))   // Returns ("$" + keyname) if no match is found
}

Example output:

こんにちは、World!
さようなら、World!
goodbyeeeeeeeeee
$byebye

2. Defining Translation Texts

Create a translations.go file and define your translation texts within it.

package main

var EasyI18nTranslations = map[string]map[string]string{
	"en": {
		"greeting": "Hello, %s!",
		"farewell": "Goodbye, %s!",
		"goodbye":  "goodbyeeeeeeeeee",
		"hello":    "Hello",
	},
	"zht": {
		"greeting": "你好, %s!",
		"farewell": "再見, %s!",
	},
	"jp": {
		"greeting": "こんにちは、%s!",
		"farewell": "さようなら、%s!",
	},
}

3. Using the Check Script

Run the script to check for missing or extra translation keys:

python check_translations.py

Example output:

--- en Translations ---
Missing keys:
  byebye
Extra keys:
  hello

--- zht Translations ---
Missing keys:
  byebye
  goodbye
Extra keys: None

--- jp Translations ---
Missing keys:
  byebye
  goodbye
Extra keys: None

🌐 i18n

Supports automatic setting of the following languages:

Code Language Name (Native) Language Name (English)
en English English
zhs 简体中文 Simplified Chinese
zht 繁體中文 Traditional Chinese
jp 日本語 Japanese
fr Français French
es Español Spanish
de Deutsch German
it Italiano Italian
pt Português Portuguese
ru Русский Russian
ko 한국어 Korean
ar العربية Arabic
hi हिन्दी Hindi

🛠 License

This project is licensed under the MIT License

🌟 Stargazers

Stargazers over time