bottext

package module
v0.0.0-...-e91cce1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2017 License: MIT Imports: 3 Imported by: 0

README

BotText

BotText is a Go package that help you:

  • Translate your bot into multiple languages,
  • Make your bot more interesting by expressing the same thing in different ways.

Installation

Make sure you have setup GOPATH

go get github.com/michlabs/bottext

Example

Create a new language file named text.yml:

vi:
  welcome:
    - Xin chào %s!
    - Hi %s
en:
  welcome:
    - Hello %s!
    - Welcome %s!
    - Hi %s

Then in your source code:

package main 

import (
    "fmt"
    "github.com/michlabs/bottext"
)

func main() {
    bottext.MustLoad("text.yml")
    var T1 bottext.BotTextFunc
    T1 = bottext.New("en")
    for i := 0; i < 10; i ++ {
        fmt.Printf(T1("welcome") + "\n", "Donald Trump")    
    }

    T2 := bottext.New("vi")
    for i := 0; i < 10; i ++ {
        fmt.Printf(T2("welcome"), "Đỗ Nam Trung")   
    }
}

Document

Read in Godoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(file string) error

Load reads content from a translation file (in YAML format) and returns any error occurs when reading it. Sample of a translation file (text.yml):

  vi:
    welcome:
      - Xin chào %s!
      - Chào %s
	   goodbye:
      - Tạm biệt!
  en:
    welcome:
      - Hello %s!
      - Welcome %s!
      - Hi %s
    goodbye:
      - Bye!
      - GoodBye!

func MustLoad

func MustLoad(file string)

MustLoad wraps the Load function and panics if the error is non-nil

Types

type BotTextFunc

type BotTextFunc func(id string) string

BotTextFunc is a function that returns randomly one of translated versions of the ID

func New

func New(locale string) BotTextFunc

New returns an instance of BotTextFunc corresponding to the language specified by locale code List of all locale codes: https://developer.chrome.com/webstore/i18n#localeTable

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL