Giter Site home page Giter Site logo

tr's Introduction

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.

tr's People

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.