Skip to content

tucnak/tr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tr

Easy drop-in i18n solution for Go applications.

GoDoc

I was looking for a real easy way to provide i18n support for my Telegram bot, for which the data is pretty much a set of 20 different text messages. I couldn't find a single solution that would utilize the file system. Here's how tr works:

  1. You have to create a locales directory, e.g. $ tree lang:

    lang
    ├── en
    │   ├── hello.txt
    │   └── inner
    │       └── text.txt
    ├── fr
    │   ├── hello.txt
    │   └── inner
    │       └── text.txt
    └── ru
        ├── hello.txt
        └── inner
            └── text.html
    
    6 directories, 6 files
    

    Your files could be of any extension, it doesn't really matter, since tr ignores extensions anyway.

  2. Init tr properly in your program:

    package main
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/tucnak/tr"
    )
    
    func init() {
    	// tr.Init(localesDirectory, defaultLocale)
    	if err := tr.Init("lang", "en"); err != nil {
    		fmt.Println(err)
    		os.Exit(1)
    	}
    }
  3. Use simple syntax for i18n:

    // Inline syntax:
    fmt.Println("In English:", tr.Lang("en").Tr("hello"))
    fmt.Println("In French:", tr.Lang("fr").Tr("hello"))
    fmt.Println("In Russian:", tr.Lang("ru").Tr("hello"))
    
    // Shadowing
    tr := tr.Lang("fr")
    fmt.Println(tr.Tr("inner/text"))

Pass an optional third true argument to tr.Init() if you wish to trim all \ns from the end of the string returned.

About

Easy drop-in i18n solution for Go applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages