summaryrefslogtreecommitdiff
path: root/internal/language/language.go
blob: d91e3a4db4177099ee0850d0a2d682a4bcbe68be (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package language

import (
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"golang.org/x/text/language"
	"golang.org/x/text/language/display"
)

var namer display.Namer

// InitLangs parses languages from the
// given slice of tags, and sets the `namer`
// display.Namer for the instance.
//
// This function should only be called once,
// since setting the namer is not thread safe.
func InitLangs(tagStrs []string) (Languages, error) {
	var (
		languages = make(Languages, len(tagStrs))
		tags      = make([]language.Tag, len(tagStrs))
	)

	// Reset namer.
	namer = nil

	// Parse all tags first.
	for i, tagStr := range tagStrs {
		tag, err := language.Parse(tagStr)
		if err != nil {
			return nil, gtserror.Newf(
				"error parsing %s as BCP47 language tag: %w",
				tagStr, err,
			)
		}
		tags[i] = tag
	}

	// Check if we can set a namer.
	if len(tags) != 0 {
		namer = display.Languages(tags[0])
	}

	// Fall namer back to English.
	if namer == nil {
		namer = display.Languages(language.English)
	}

	// Parse nice language models from tags
	// (this will use the namer we just set).
	for i, tag := range tags {
		languages[i] = ParseTag(tag)
	}

	return languages, nil
}

// Language models a BCP47 language tag
// along with helper strings for the tag.
type Language struct {
	// BCP47 language tag
	Tag language.Tag
	// Normalized string
	// of BCP47 tag.
	TagStr string
	// Human-readable
	// language name(s).
	DisplayStr string
}

// MarshalText implements encoding.TextMarshaler{}.
func (l *Language) MarshalText() ([]byte, error) {
	return []byte(l.TagStr), nil
}

// UnmarshalText implements encoding.TextUnmarshaler{}.
func (l *Language) UnmarshalText(text []byte) error {
	lang, err := Parse(string(text))
	if err != nil {
		return err
	}

	*l = *lang
	return nil
}

type Languages []*Language

func (l Languages) Tags() []language.Tag {
	tags := make([]language.Tag, len(l))
	for i, lang := range l {
		tags[i] = lang.Tag
	}

	return tags
}

func (l Languages) TagStrs() []string {
	tagStrs := make([]string, len(l))
	for i, lang := range l {
		tagStrs[i] = lang.TagStr
	}

	return tagStrs
}

func (l Languages) DisplayStrs() []string {
	displayStrs := make([]string, len(l))
	for i, lang := range l {
		displayStrs[i] = lang.DisplayStr
	}

	return displayStrs
}

// ParseTag parses and nicely formats the input language BCP47 tag,
// returning a Language with ready-to-use display and tag strings.
func ParseTag(tag language.Tag) *Language {
	l := new(Language)
	l.Tag = tag
	l.TagStr = tag.String()

	var (
		// Our name for the language.
		name string
		// Language's name for itself.
		selfName = display.Self.Name(tag)
	)

	// Try to use namer
	// (if initialized).
	if namer != nil {
		name = namer.Name(tag)
	}

	switch {
	case name == "":
		// We don't have a name for
		// this language, just use
		// its own name for itself.
		l.DisplayStr = selfName

	case name == selfName:
		// Avoid repeating ourselves:
		// showing "English (English)"
		// is not useful.
		l.DisplayStr = name

	default:
		// Include our name for the
		// language, and its own
		// name for itself.
		l.DisplayStr = name + " " + "(" + selfName + ")"
	}

	return l
}

// Parse parses and nicely formats the input language BCP47 tag,
// returning a Language with ready-to-use display and tag strings.
func Parse(lang string) (*Language, error) {
	tag, err := language.Parse(lang)
	if err != nil {
		return nil, err
	}

	return ParseTag(tag), nil
}