File: translation.go

package info (click to toggle)
golang-github-leonelquinteros-gotext 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 404 kB
  • sloc: makefile: 4
file content (80 lines) | stat: -rw-r--r-- 1,627 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
/*
 * Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for full license information.
 */

package gotext

// Translation is the struct for the Translations parsed via Po or Mo files and all coming parsers
type Translation struct {
	ID       string
	PluralID string
	Trs      map[int]string
	Refs     []string

	dirty bool
}

// NewTranslation returns the Translation object and initialized it.
func NewTranslation() *Translation {
	return &Translation{
		Trs: make(map[int]string),
	}
}

func NewTranslationWithRefs(refs []string) *Translation {
	return &Translation{
		Trs:  make(map[int]string),
		Refs: refs,
	}
}

func (t *Translation) IsStale() bool {
	return t.dirty == false
}

func (t *Translation) SetRefs(refs []string) {
	t.Refs = refs
	t.dirty = true
}

func (t *Translation) Set(str string) {
	t.Trs[0] = str
	t.dirty = true
}

// Get returns the string of the translation
func (t *Translation) Get() string {
	// Look for Translation index 0
	if _, ok := t.Trs[0]; ok {
		if t.Trs[0] != "" {
			return t.Trs[0]
		}
	}

	// Return untranslated id by default
	return t.ID
}

func (t *Translation) SetN(n int, str string) {
	t.Trs[n] = str
	t.dirty = true
}

// GetN returns the string of the plural translation
func (t *Translation) GetN(n int) string {
	// Look for Translation index
	if _, ok := t.Trs[n]; ok {
		if t.Trs[n] != "" {
			return t.Trs[n]
		}
	}

	// Return untranslated singular if corresponding
	if n == 0 {
		return t.ID
	}

	// Return untranslated plural by default
	return t.PluralID
}