...

Source file src/github.com/chai2010/gettext-go/examples/hello.go

Documentation: github.com/chai2010/gettext-go/examples

     1  // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This is a gettext-go exmaple.
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/chai2010/gettext-go"
    12  	"github.com/chai2010/gettext-go/examples/hi"
    13  )
    14  
    15  func init() {
    16  	fmt.Println("=== main.init: default ===")
    17  
    18  	// bind app domain
    19  	gettext.BindLocale(gettext.New("hello", "locale"))
    20  
    21  	// $(LC_MESSAGES) or $(LANG) or empty
    22  	fmt.Println(gettext.Gettext("Gettext in init."))
    23  	fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
    24  	hi.SayHi()
    25  
    26  	// Output(depends on locale environment):
    27  	// ?
    28  	// ?
    29  	// ?
    30  	// ?
    31  
    32  	fmt.Println("=== main.init: zh_CN ===")
    33  
    34  	// set simple chinese
    35  	gettext.SetLanguage("zh_CN")
    36  
    37  	// simple chinese
    38  	fmt.Println(gettext.Gettext("Gettext in init."))
    39  	fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
    40  	hi.SayHi()
    41  
    42  	// Output:
    43  	// Init函数中的Gettext.
    44  	// Init函数中的Gettext.(ctx:main.init)
    45  	// 来自"Hi"包的问候: 你好, 世界!
    46  	// 来自"Hi"包的问候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
    47  }
    48  
    49  func main() {
    50  	fmt.Println("=== main.main: zh_CN ===")
    51  
    52  	// simple chinese
    53  	fmt.Println(gettext.Gettext("Hello, world!"))
    54  	fmt.Println(gettext.PGettext("main.main", "Hello, world!"))
    55  	hi.SayHi()
    56  
    57  	// Output:
    58  	// 你好, 世界!
    59  	// 你好, 世界!(ctx:main.main)
    60  	// 来自"Hi"包的问候: 你好, 世界!
    61  	// 来自"Hi"包的问候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
    62  
    63  	fmt.Println("=== main.main: zh_TW ===")
    64  
    65  	// set traditional chinese
    66  	gettext.SetLanguage("zh_TW")
    67  
    68  	// traditional chinese
    69  	func() {
    70  		fmt.Println(gettext.Gettext("Gettext in func."))
    71  		fmt.Println(gettext.PGettext("main.func", "Gettext in func."))
    72  		hi.SayHi()
    73  
    74  		// Output:
    75  		// 閉包函數中的Gettext.
    76  		// 閉包函數中的Gettext.(ctx:main.func)
    77  		// 來自"Hi"包的問候: 你好, 世界!
    78  		// 來自"Hi"包的問候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
    79  	}()
    80  
    81  	fmt.Println()
    82  
    83  	// translate resource
    84  	fmt.Println("=== main.main: zh_CN ===")
    85  	gettext.SetLanguage("zh_CN")
    86  	fmt.Println("poems(simple chinese):")
    87  	fmt.Println(string(gettext.Getdata("poems.txt")))
    88  	fmt.Println("=== main.main: zh_TW ===")
    89  	gettext.SetLanguage("zh_TW")
    90  	fmt.Println("poems(traditional chinese):")
    91  	fmt.Println(string(gettext.Getdata("poems.txt")))
    92  	fmt.Println("=== main.main: ?? ===")
    93  	gettext.SetLanguage("??")
    94  	fmt.Println("poems(default is english):")
    95  	fmt.Println(string(gettext.Getdata("poems.txt")))
    96  	// Output: ...
    97  }
    98  

View as plain text