From a7fdade41d3f6c8ae439ff8c109fa176e659b461 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Sun, 21 Jan 2018 16:49:17 +1000 Subject: [PATCH] Extend to include example translation stuff --- irc/client.go | 1 + irc/languages.go | 54 +++++++++++++++++++++++++++++++++++++ irc/server.go | 13 ++++++++- languages/example.lang.yaml | 5 +++- 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 irc/languages.go diff --git a/irc/client.go b/irc/client.go index 5cfd9bed..071cffb3 100644 --- a/irc/client.go +++ b/irc/client.go @@ -56,6 +56,7 @@ type Client struct { idletimer *IdleTimer isDestroyed bool isQuitting bool + languages []string maxlenTags uint32 maxlenRest uint32 nick string diff --git a/irc/languages.go b/irc/languages.go new file mode 100644 index 00000000..67debcff --- /dev/null +++ b/irc/languages.go @@ -0,0 +1,54 @@ +// Copyright (c) 2018 Daniel Oaks +// 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 +} diff --git a/irc/server.go b/irc/server.go index 60ac5db2..bb927413 100644 --- a/irc/server.go +++ b/irc/server.go @@ -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 [] 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() diff --git a/languages/example.lang.yaml b/languages/example.lang.yaml index c0144a50..2c8424ce 100644 --- a/languages/example.lang.yaml +++ b/languages/example.lang.yaml @@ -10,9 +10,12 @@ code: "example" # maintainers - these are the maintainer details given maintainers: "Daniel Oaks " +# 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"