~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/chai2010/gettext-go/examples/hello.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/examples/hi"
12
 
        "github.com/chai2010/gettext-go/gettext"
13
 
)
14
 
 
15
 
func init() {
16
 
        // bind app domain
17
 
        gettext.BindTextdomain("hello", "local", nil)
18
 
        gettext.Textdomain("hello")
19
 
 
20
 
        // $(LC_MESSAGES) or $(LANG) or empty
21
 
        fmt.Println(gettext.Gettext("Gettext in init."))
22
 
        fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
23
 
        hi.SayHi()
24
 
        // Output(depends on local environment):
25
 
        // ?
26
 
        // ?
27
 
        // ?
28
 
        // ?
29
 
 
30
 
        // set simple chinese
31
 
        gettext.SetLocale("zh_CN")
32
 
 
33
 
        // simple chinese
34
 
        fmt.Println(gettext.Gettext("Gettext in init."))
35
 
        fmt.Println(gettext.PGettext("main.init", "Gettext in init."))
36
 
        hi.SayHi()
37
 
        // Output:
38
 
        // Init函数中的Gettext.
39
 
        // Init函数中的Gettext.
40
 
        // 来自"Hi"包的问候: 你好, 世界!
41
 
        // 来自"Hi"包的问候: 你好, 世界!
42
 
}
43
 
 
44
 
func main() {
45
 
        // simple chinese
46
 
        fmt.Println(gettext.Gettext("Hello, world!"))
47
 
        fmt.Println(gettext.PGettext("main.main", "Hello, world!"))
48
 
        hi.SayHi()
49
 
        // Output:
50
 
        // 你好, 世界!
51
 
        // 你好, 世界!
52
 
        // 来自"Hi"包的问候: 你好, 世界!
53
 
        // 来自"Hi"包的问候: 你好, 世界!
54
 
 
55
 
        // set traditional chinese
56
 
        gettext.SetLocale("zh_TW")
57
 
 
58
 
        // traditional chinese
59
 
        func() {
60
 
                fmt.Println(gettext.Gettext("Gettext in func."))
61
 
                fmt.Println(gettext.PGettext("main.func", "Gettext in func."))
62
 
                hi.SayHi()
63
 
                // Output:
64
 
                // 閉包函數中的Gettext.
65
 
                // 閉包函數中的Gettext.
66
 
                // 來自"Hi"包的問候: 你好, 世界!
67
 
                // 來自"Hi"包的問候: 你好, 世界!
68
 
        }()
69
 
 
70
 
        fmt.Println()
71
 
 
72
 
        // translate resource
73
 
        gettext.SetLocale("zh_CN")
74
 
        fmt.Println("poems(simple chinese):")
75
 
        fmt.Println(string(gettext.Getdata("poems.txt")))
76
 
        gettext.SetLocale("zh_TW")
77
 
        fmt.Println("poems(traditional chinese):")
78
 
        fmt.Println(string(gettext.Getdata("poems.txt")))
79
 
        gettext.SetLocale("??")
80
 
        fmt.Println("poems(default is english):")
81
 
        fmt.Println(string(gettext.Getdata("poems.txt")))
82
 
        // Output: ...
83
 
}