...

Source file src/github.com/chai2010/gettext-go/example_test.go

Documentation: github.com/chai2010/gettext-go

     1  // Copyright 2020 ChaiShushan <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  package gettext_test
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"log"
    11  
    12  	"github.com/chai2010/gettext-go"
    13  )
    14  
    15  func Example() {
    16  	gettext := gettext.New("hello", "./examples/locale").SetLanguage("zh_CN")
    17  	fmt.Println(gettext.Gettext("Hello, world!"))
    18  
    19  	// Output:
    20  	// 你好, 世界!
    21  }
    22  
    23  func Example_zip() {
    24  	gettext := gettext.New("hello", "./examples/locale.zip").SetLanguage("zh_CN")
    25  	fmt.Println(gettext.Gettext("Hello, world!"))
    26  
    27  	// Output:
    28  	// 你好, 世界!
    29  }
    30  
    31  func Example_zipData() {
    32  	zipData, err := ioutil.ReadFile("./examples/locale.zip")
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  
    37  	gettext := gettext.New("hello", "???", zipData).SetLanguage("zh_CN")
    38  	fmt.Println(gettext.Gettext("Hello, world!"))
    39  
    40  	// Output:
    41  	// 你好, 世界!
    42  }
    43  
    44  func Example_bind() {
    45  	gettext.BindLocale(gettext.New("hello", "./examples/locale.zip"))
    46  	gettext.SetLanguage("zh_CN")
    47  
    48  	fmt.Println(gettext.Gettext("Hello, world!"))
    49  
    50  	// Output:
    51  	// 你好, 世界!
    52  }
    53  
    54  func Example_multiLang() {
    55  	zh := gettext.New("hello", "./examples/locale").SetLanguage("zh_CN")
    56  	tw := gettext.New("hello", "./examples/locale").SetLanguage("zh_TW")
    57  
    58  	fmt.Println(zh.PGettext(
    59  		"code.google.com/p/gettext-go/examples/hi.SayHi",
    60  		"pkg hi: Hello, world!",
    61  	))
    62  
    63  	fmt.Println(tw.PGettext(
    64  		"code.google.com/p/gettext-go/examples/hi.SayHi",
    65  		"pkg hi: Hello, world!",
    66  	))
    67  
    68  	// Output:
    69  	// 来自"Hi"包的问候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
    70  	// 來自"Hi"包的問候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)
    71  }
    72  
    73  func Example_json() {
    74  	const jsonData = `{
    75  		"zh_CN": {
    76  			"LC_MESSAGES": {
    77  				"hello.json": [{
    78  					"msgctxt"     : "",
    79  					"msgid"       : "Hello, world!",
    80  					"msgid_plural": "",
    81  					"msgstr"      : ["你好, 世界!"]
    82  				}]
    83  			}
    84  		}
    85  	}`
    86  
    87  	gettext := gettext.New("hello", "???", jsonData).SetLanguage("zh_CN")
    88  	fmt.Println(gettext.Gettext("Hello, world!"))
    89  
    90  	// Output:
    91  	// 你好, 世界!
    92  }
    93  

View as plain text