File: main.go

package info (click to toggle)
golang-github-nicksnyder-go-i18n.v2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 464 kB
  • sloc: xml: 198; sh: 5; makefile: 3
file content (155 lines) | stat: -rw-r--r-- 3,882 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Command goi18n manages message files used by the i18n package.
//
//     go get -u github.com/nicksnyder/go-i18n/v2/goi18n
//     goi18n -help
//
// Use `goi18n extract` to create a message file that contains the messages defined in your Go source files.
//     # en.toml
//     [PersonCats]
//     description = "The number of cats a person has"
//     one = "{{.Name}} has {{.Count}} cat."
//     other = "{{.Name}} has {{.Count}} cats."
//
// Use `goi18n merge` to create message files for translation.
//     # translate.es.toml
//     [PersonCats]
//     description = "The number of cats a person has"
//     hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
//     one = "{{.Name}} has {{.Count}} cat."
//     other = "{{.Name}} has {{.Count}} cats."
//
// Use `goi18n merge` to merge translated message files with your existing message files.
//     # active.es.toml
//     [PersonCats]
//     description = "The number of cats a person has"
//     hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
//     one = "{{.Name}} tiene {{.Count}} gato."
//     other = "{{.Name}} tiene {{.Count}} gatos."
//
// Load the active messages into your bundle.
//     bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
//     bundle.MustLoadMessageFile("active.es.toml")
package main

import (
	"flag"
	"fmt"
	"os"

	"golang.org/x/text/language"
)

func mainUsage() {
	fmt.Fprintf(os.Stderr, `goi18n (v2) is a tool for managing message translations.

Usage:

	goi18n command [arguments]

The commands are:

	merge		merge message files
	extract		extract messages from Go files

Workflow:

	Use 'goi18n extract' to create a message file that contains the messages defined in your Go source files.

		# en.toml
		[PersonCats]
		description = "The number of cats a person has"
		one = "{{.Name}} has {{.Count}} cat."
		other = "{{.Name}} has {{.Count}} cats."

	Use 'goi18n merge' to create message files for translation.

		# translate.es.toml
		[PersonCats]
		description = "The number of cats a person has"
		hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
		one = "{{.Name}} has {{.Count}} cat."
		other = "{{.Name}} has {{.Count}} cats."

	Use 'goi18n merge' to merge translated message files with your existing message files.

		# active.es.toml
		[PersonCats]
		description = "The number of cats a person has"
		hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
		one = "{{.Name}} tiene {{.Count}} gato."
		other = "{{.Name}} tiene {{.Count}} gatos."

	Load the active messages into your bundle.

		bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
		bundle.MustLoadMessageFile("active.es.toml")
`)
}

type command interface {
	name() string
	parse(arguments []string) error
	execute() error
}

func main() {
	os.Exit(testableMain(os.Args[1:]))
}

func testableMain(args []string) int {
	flags := flag.NewFlagSet("goi18n", flag.ContinueOnError)
	flags.Usage = mainUsage
	if err := flags.Parse(args); err != nil {
		if err == flag.ErrHelp {
			return 2
		}
		return 1
	}
	if flags.NArg() == 0 {
		mainUsage()
		return 2
	}
	commands := []command{
		&mergeCommand{},
		&extractCommand{},
	}
	cmdName := flags.Arg(0)
	for _, cmd := range commands {
		if cmd.name() == cmdName {
			if err := cmd.parse(flags.Args()[1:]); err != nil {
				fmt.Fprintln(os.Stderr, err)
				return 1
			}
			if err := cmd.execute(); err != nil {
				fmt.Fprintln(os.Stderr, err)
				return 1
			}
			return 0
		}
	}
	fmt.Fprintf(os.Stderr, "goi18n: unknown subcommand %s\n", cmdName)
	return 1
}

type languageTag language.Tag

func (lt languageTag) String() string {
	return lt.Tag().String()
}

func (lt *languageTag) Set(value string) error {
	t, err := language.Parse(value)
	if err != nil {
		return err
	}
	*lt = languageTag(t)
	return nil
}

func (lt languageTag) Tag() language.Tag {
	tag := language.Tag(lt)
	if tag.IsRoot() {
		return language.English
	}
	return tag
}