Extend to include example translation stuff

This commit is contained in:
Daniel Oaks 2018-01-21 16:49:17 +10:00
parent ba77a95c81
commit a7fdade41d
4 changed files with 71 additions and 2 deletions

View File

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

54
irc/languages.go Normal file
View File

@ -0,0 +1,54 @@
// Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"sync"
)
// LanguageManager manages our languages and provides translation abilities.
type LanguageManager struct {
sync.RWMutex
langMap map[string]map[string]string
}
// NewLanguageManager returns a new LanguageManager.
func NewLanguageManager() *LanguageManager {
lm := LanguageManager{
langMap: make(map[string]map[string]string),
}
//TODO(dan): load language files here
return &lm
}
// Translate returns the given string, translated into the given language.
func (lm *LanguageManager) Translate(languages []string, originalString string) string {
// not using any special languages
if len(languages) == 0 {
return originalString
}
lm.RLock()
defer lm.RUnlock()
for _, lang := range languages {
langMap, exists := lm.langMap[lang]
if !exists {
continue
}
newString, exists := langMap[originalString]
if !exists {
continue
}
// found a valid translation!
return newString
}
// didn't find any translation
return originalString
}

View File

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

View File

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