diff options
author | Simon Rozman <simon@rozman.si> | 2019-11-14 09:27:05 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-15 14:49:27 -0600 |
commit | 8a476b326136f5c03790fa168686848884c8cd5a (patch) | |
tree | 3fac4f2940c3012734a4147ef627e2b52a27a859 /l18n/l18n.go | |
parent | manager: chdir into unelevated profile before execing (diff) | |
download | wireguard-windows-8a476b326136f5c03790fa168686848884c8cd5a.tar.xz wireguard-windows-8a476b326136f5c03790fa168686848884c8cd5a.zip |
l18n: add localization support
Revise the messages to make them localizable.
Note: The log messages are not marked for localization. Probably, we
want to keep log files in English for easier global troubleshooting.
Having a user run `go generate` requires a valid and up-to-date Go
environment. Rather than instructing users how to setup the environment
correctly, the `go generate` was integrated into build.bat. This reuses
the Go building environment downloaded and prepared by build.bat to
provide controllable and consistent result.
Use `make generate` on Linux.
As the zgotext.go output varies for GOARCH=386 and amd64, one had to be
chosen to provide stable output. The former is the first one to build in
build.bat.
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'l18n/l18n.go')
-rw-r--r-- | l18n/l18n.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/l18n/l18n.go b/l18n/l18n.go new file mode 100644 index 00000000..717e4861 --- /dev/null +++ b/l18n/l18n.go @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package l18n + +import ( + "sync" + + "golang.org/x/sys/windows" + "golang.org/x/text/language" + "golang.org/x/text/message" +) + +var printer *message.Printer +var printerLock sync.Mutex + +// prn returns the printer for user preferred UI language. +func prn() *message.Printer { + if printer != nil { + return printer + } + printerLock.Lock() + if printer != nil { + printerLock.Unlock() + return printer + } + printer = message.NewPrinter(lang()) + printerLock.Unlock() + return printer +} + +// lang returns the user preferred UI language we have most confident translation in the default catalog available. +func lang() (tag language.Tag) { + tag = language.English + confidence := language.No + languages, err := windows.GetUserPreferredUILanguages(windows.MUI_LANGUAGE_NAME) + if err != nil { + return + } + for i := range languages { + t, _, c := message.DefaultCatalog.Matcher().Match(message.MatchLanguage(languages[i])) + if c > confidence { + tag = t + confidence = c + } + } + return +} + +// Sprintf is like fmt.Sprintf, but using language-specific formatting. +func Sprintf(key message.Reference, a ...interface{}) string { + return prn().Sprintf(key, a...) +} + +// EnumerationSeparator returns enumeration separator. For English and western languages, +// enumeration separator is a comma followed by a space (i.e. ", "). For Chinese, it returns +// "\u3001". +func EnumerationSeparator() string { + // BUG: We could just use `Sprintf(", " /* ...translator instructions... */)` and let the + // individual locale catalog handle its translation. Unfortunately, the gotext utility tries to + // be nice to translators and skips all strings without letters when updating catalogs. + return Sprintf("[EnumerationSeparator]" /* Text to insert between items when listing - most western languages will translate ‘[EnumerationSeparator]’ into ‘, ’ to produce lists like ‘apple, orange, strawberry’. Eastern languages might translate into ‘、’ to produce lists like ‘リンゴ、オレンジ、イチゴ’. */) +} |