Просмотр исходного кода

Extend to include example translation stuff

tags/v0.11.0-alpha
Daniel Oaks 6 лет назад
Родитель
Сommit
a7fdade41d
4 измененных файлов: 71 добавлений и 2 удалений
  1. 1
    0
      irc/client.go
  2. 54
    0
      irc/languages.go
  3. 12
    1
      irc/server.go
  4. 4
    1
      languages/example.lang.yaml

+ 1
- 0
irc/client.go Просмотреть файл

@@ -56,6 +56,7 @@ type Client struct {
56 56
 	idletimer          *IdleTimer
57 57
 	isDestroyed        bool
58 58
 	isQuitting         bool
59
+	languages          []string
59 60
 	maxlenTags         uint32
60 61
 	maxlenRest         uint32
61 62
 	nick               string

+ 54
- 0
irc/languages.go Просмотреть файл

@@ -0,0 +1,54 @@
1
+// Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net>
2
+// released under the MIT license
3
+
4
+package irc
5
+
6
+import (
7
+	"sync"
8
+)
9
+
10
+// LanguageManager manages our languages and provides translation abilities.
11
+type LanguageManager struct {
12
+	sync.RWMutex
13
+	langMap map[string]map[string]string
14
+}
15
+
16
+// NewLanguageManager returns a new LanguageManager.
17
+func NewLanguageManager() *LanguageManager {
18
+	lm := LanguageManager{
19
+		langMap: make(map[string]map[string]string),
20
+	}
21
+
22
+	//TODO(dan): load language files here
23
+
24
+	return &lm
25
+}
26
+
27
+// Translate returns the given string, translated into the given language.
28
+func (lm *LanguageManager) Translate(languages []string, originalString string) string {
29
+	// not using any special languages
30
+	if len(languages) == 0 {
31
+		return originalString
32
+	}
33
+
34
+	lm.RLock()
35
+	defer lm.RUnlock()
36
+
37
+	for _, lang := range languages {
38
+		langMap, exists := lm.langMap[lang]
39
+		if !exists {
40
+			continue
41
+		}
42
+
43
+		newString, exists := langMap[originalString]
44
+		if !exists {
45
+			continue
46
+		}
47
+
48
+		// found a valid translation!
49
+		return newString
50
+	}
51
+
52
+	// didn't find any translation
53
+	return originalString
54
+}

+ 12
- 1
irc/server.go Просмотреть файл

@@ -98,6 +98,7 @@ type Server struct {
98 98
 	loggingRawIO                 bool
99 99
 	isupport                     *isupport.List
100 100
 	klines                       *KLineManager
101
+	languages                    *LanguageManager
101 102
 	limits                       Limits
102 103
 	listeners                    map[string]*ListenerWrapper
103 104
 	logger                       *logger.Manager
@@ -153,6 +154,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
153 154
 		commands:            make(chan Command),
154 155
 		connectionLimiter:   connection_limits.NewLimiter(),
155 156
 		connectionThrottler: connection_limits.NewThrottler(),
157
+		languages:           NewLanguageManager(),
156 158
 		listeners:           make(map[string]*ListenerWrapper),
157 159
 		logger:              logger,
158 160
 		monitorManager:      NewMonitorManager(),
@@ -434,7 +436,7 @@ func (server *Server) tryRegister(c *Client) {
434 436
 	// send welcome text
435 437
 	//NOTE(dan): we specifically use the NICK here instead of the nickmask
436 438
 	// see http://modern.ircdocs.horse/#rplwelcome-001 for details on why we avoid using the nickmask
437
-	c.Send(nil, server.name, RPL_WELCOME, c.nick, fmt.Sprintf("Welcome to the Internet Relay Network %s", c.nick))
439
+	c.Send(nil, server.name, RPL_WELCOME, c.nick, fmt.Sprintf(c.t("Welcome to the Internet Relay Network %s"), c.nick))
438 440
 	c.Send(nil, server.name, RPL_YOURHOST, c.nick, fmt.Sprintf("Your host is %s, running version %s", server.name, Ver))
439 441
 	c.Send(nil, server.name, RPL_CREATED, c.nick, fmt.Sprintf("This server was created %s", server.ctime.Format(time.RFC1123)))
440 442
 	//TODO(dan): Look at adding last optional [<channel modes with a parameter>] parameter
@@ -447,6 +449,15 @@ func (server *Server) tryRegister(c *Client) {
447 449
 	}
448 450
 }
449 451
 
452
+// t returns the translated version of the given string, based on the languages configured by the client.
453
+func (client *Client) t(originalString string) string {
454
+	// grab this mutex to protect client.languages
455
+	client.stateMutex.RLock()
456
+	defer client.stateMutex.RUnlock()
457
+
458
+	return client.server.languages.Translate(client.languages, originalString)
459
+}
460
+
450 461
 // MOTD serves the Message of the Day.
451 462
 func (server *Server) MOTD(client *Client) {
452 463
 	server.configurableStateMutex.RLock()

+ 4
- 1
languages/example.lang.yaml Просмотреть файл

@@ -10,9 +10,12 @@ code: "example"
10 10
 # maintainers - these are the maintainer details given
11 11
 maintainers: "Daniel Oaks <daniel@danieloaks.net>"
12 12
 
13
+# incomplete - whether to mark this language as incomplete
14
+incomplete: true
15
+
13 16
 # strings - this holds the actual replacements
14 17
 # make sure this is the last part of the file, and that the below string, "strings:", stays as-is
15 18
 # the language-update processor uses the next line to designate which part of the file to ignore and
16 19
 # which part to actually process.
17 20
 strings:
18
-  "": ""
21
+  "Welcome to the Internet Relay Network %s": "Welcome bro to the IRN broski %s"

Загрузка…
Отмена
Сохранить