File: hello.go

package info (click to toggle)
golang-github-chai2010-gettext-go 0.0~git20191225.6b9f4b1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,256 kB
  • sloc: makefile: 23
file content (83 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This is a gettext-go exmaple.
package main

import (
	"fmt"

	"github.com/chai2010/gettext-go/examples/hi"
	"github.com/chai2010/gettext-go/gettext"
)

func init() {
	// bind app domain
	gettext.BindTextdomain("hello", "local", nil)
	gettext.Textdomain("hello")

	// $(LC_MESSAGES) or $(LANG) or empty
	fmt.Println(gettext.Gettext("Gettext in init."))
	fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
	hi.SayHi()
	// Output(depends on local environment):
	// ?
	// ?
	// ?
	// ?

	// set simple chinese
	gettext.SetLocale("zh_CN")

	// simple chinese
	fmt.Println(gettext.Gettext("Gettext in init."))
	fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
	hi.SayHi()
	// Output:
	// Init函数中的Gettext.
	// Init函数中的Gettext.
	// 来自"Hi"包的问候: 你好, 世界!
	// 来自"Hi"包的问候: 你好, 世界!
}

func main() {
	// simple chinese
	fmt.Println(gettext.Gettext("Hello, world!"))
	fmt.Println(gettext.PGettext("main.main", "Hello, world!"))
	hi.SayHi()
	// Output:
	// 你好, 世界!
	// 你好, 世界!
	// 来自"Hi"包的问候: 你好, 世界!
	// 来自"Hi"包的问候: 你好, 世界!

	// set traditional chinese
	gettext.SetLocale("zh_TW")

	// traditional chinese
	func() {
		fmt.Println(gettext.Gettext("Gettext in func."))
		fmt.Println(gettext.PGettext("main.func", "Gettext in func."))
		hi.SayHi()
		// Output:
		// 閉包函數中的Gettext.
		// 閉包函數中的Gettext.
		// 來自"Hi"包的問候: 你好, 世界!
		// 來自"Hi"包的問候: 你好, 世界!
	}()

	fmt.Println()

	// translate resource
	gettext.SetLocale("zh_CN")
	fmt.Println("poems(simple chinese):")
	fmt.Println(string(gettext.Getdata("poems.txt")))
	gettext.SetLocale("zh_TW")
	fmt.Println("poems(traditional chinese):")
	fmt.Println(string(gettext.Getdata("poems.txt")))
	gettext.SetLocale("??")
	fmt.Println("poems(default is english):")
	fmt.Println(string(gettext.Getdata("poems.txt")))
	// Output: ...
}