日本語版 README はこちら / Japanese README
A Go internationalization (i18n) library inspired by Movable Type's localization system. It provides automatic language detection and translation support for Go applications with test-friendly language override capabilities.
- Automatic Language Detection: Automatically detects language from environment variables
- Simple Translation: Easy translation with the
T()function - Distributed Registration: Each package can provide its own translations
- Test-Friendly: Language forcing functionality for testing
- Environment Variable Support: Control default language and detection behavior
go get github.com/ideamans/go-l10nRegister translations in the init() function of each Go source file:
package main
import "github.com/ideamans/go-l10n"
func init() {
l10n.Register("ja", l10n.LexiconMap{
"Hello, World!": "こんにちは、世界!",
"File not found": "ファイルが見つかりません",
"Error occurred": "エラーが発生しました",
"Operation success": "操作が成功しました",
})
}package main
import (
"fmt"
"github.com/ideamans/go-l10n"
)
func main() {
// Language is automatically detected based on environment variables
fmt.Println(l10n.T("Hello, World!"))
// In Japanese environment: "こんにちは、世界!"
// In English environment: "Hello, World!"
}Language is determined in the following priority order:
- Forced Setting: Language set by
ForceLanguage() - Test Mode: Uses default language during test execution
- Environment Variables: Detected from the following environment variables:
LANGUAGELC_ALLLC_MESSAGESLANG
Currently supported languages:
ja: Japaneseen: English (fallback)
Sets the default language. If not set, "en" is used.
export L10N_DEFAULT_LANGUAGE=frIf set to a non-empty value, skips automatic language detection during initialization.
export L10N_SKIP_DETECTION=1Forces test mode behavior, which uses the default language (English) regardless of environment settings. This is useful for ensuring consistent behavior in non-test environments that require test-like language behavior.
export L10N_TEST_MODE=1type LexiconMap map[string]string // Maps base phrases to translated phrases
type WorldMap map[string]LexiconMap // Maps language codes to lexiconsRegisters translations for the specified language. Merges with existing translations.
l10n.Register("ja", l10n.LexiconMap{
"Save": "保存",
"Load": "読込",
})Translates a phrase. Returns the original phrase if no translation is found.
message := l10n.T("File saved successfully")Translates a phrase and formats it with fmt.Sprintf.
message := l10n.F("Found %d files", count)Translates a phrase and returns it as an error with fmt.Errorf.
err := l10n.E("Failed to open %s", filename)Forces the language to a specific value (primarily for testing).
l10n.ForceLanguage("ja")Resets language detection to automatic mode.
l10n.ResetLanguage()Returns the currently active language code.
currentLang := l10n.GetCurrentLanguage()Detects and sets the language from environment variables.
l10n.DetectLanguage()// package database
package database
import "github.com/ideamans/go-l10n"
func init() {
l10n.Register("ja", l10n.LexiconMap{
"Connection failed": "接続に失敗しました",
"Query executed": "クエリを実行しました",
"Transaction started": "トランザクションを開始しました",
})
}
// package auth
package auth
import "github.com/ideamans/go-l10n"
func init() {
l10n.Register("ja", l10n.LexiconMap{
"Login successful": "ログインに成功しました",
"Invalid credentials": "認証情報が無効です",
"Session expired": "セッションが期限切れです",
})
}func TestJapaneseMessages(t *testing.T) {
// Force Japanese for testing
l10n.ForceLanguage("ja")
defer l10n.ResetLanguage() // Reset after test
message := l10n.T("Hello, World!")
if message != "こんにちは、世界!" {
t.Errorf("Expected Japanese translation, got: %s", message)
}
}// Using F() for formatted messages
count := 5
message := l10n.F("Found %d files", count)
// English: "Found 5 files"
// Japanese: "5個のファイルが見つかりました" (if registered)
// Using E() for formatted errors
filename := "config.json"
err := l10n.E("Failed to read %s", filename)
// Returns error with translated message# Run all tests
go test ./...
# Run tests with verbose output
go test -v ./...
# Run specific test
go test -run TestT_BasicTranslation ./...This project is inspired by Movable Type's localization system, providing a simple yet powerful approach to internationalization in Go applications.
Bug reports and feature requests are welcome via Issues. Pull requests are also appreciated.
This project is licensed under the MIT License - see the LICENSE file for details.