github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/i18n/messages.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package i18n
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"golang.org/x/text/language"
    24  	"golang.org/x/text/message"
    25  	"golang.org/x/text/message/catalog"
    26  )
    27  
    28  // MessageKey is the english translation text
    29  type MessageKey string
    30  
    31  // Expand for use in docs and logging - returns a translated message, translated the language of the context
    32  func Expand(ctx context.Context, key MessageKey, inserts ...interface{}) string {
    33  	return pFor(ctx).Sprintf(string(key), inserts...)
    34  }
    35  
    36  // ExpandWithCode for use in error scenarios - returns a translated message with a "MSG012345:" prefix, translated the language of the context
    37  func ExpandWithCode(ctx context.Context, key MessageKey, inserts ...interface{}) string {
    38  	return string(key) + ": " + pFor(ctx).Sprintf(string(key), inserts...)
    39  }
    40  
    41  // WithLang sets the language on the context
    42  func WithLang(ctx context.Context, lang language.Tag) context.Context {
    43  	return context.WithValue(ctx, ctxLangKey{}, lang)
    44  }
    45  
    46  type (
    47  	ctxLangKey struct{}
    48  )
    49  
    50  type msg struct {
    51  	msgid       MessageKey
    52  	localString catalog.Message
    53  }
    54  
    55  type lang struct {
    56  	tag      string
    57  	messages []*msg
    58  }
    59  
    60  var serverLangs = []language.Tag{
    61  	language.AmericanEnglish, // Only English currently supported
    62  }
    63  
    64  var langMatcher = language.NewMatcher(serverLangs)
    65  
    66  // enTranslations are special, as new messages are added here first using the en_translations.go file
    67  // and are allocated their IDs there
    68  var enTranslations = []*msg{}
    69  
    70  var statusHints = map[string]int{}
    71  var msgIDUniq = map[string]bool{}
    72  
    73  // ffm is the enTranslations helper to define a new message (not used in translation files)
    74  func ffm(key, enTranslation string, statusHint ...int) MessageKey {
    75  	if _, exists := msgIDUniq[key]; exists {
    76  		panic(fmt.Sprintf("Message ID %s re-used", key))
    77  	}
    78  	msgIDUniq[key] = true
    79  	m := msg{MessageKey(key), catalog.String(enTranslation)}
    80  	enTranslations = append(enTranslations, &m)
    81  	if len(statusHint) > 0 {
    82  		statusHints[key] = statusHint[0]
    83  	}
    84  	return m.msgid
    85  }
    86  
    87  var defaultLangPrinter *message.Printer
    88  
    89  func pFor(ctx context.Context) *message.Printer {
    90  	lang := ctx.Value(ctxLangKey{})
    91  	if lang == nil {
    92  		return defaultLangPrinter
    93  	}
    94  	return message.NewPrinter(lang.(language.Tag))
    95  }
    96  
    97  func init() {
    98  	all := []*lang{
    99  		{"en", enTranslations},
   100  	}
   101  	for _, e := range all {
   102  		tag := language.MustParse(e.tag)
   103  		for _, msg := range e.messages {
   104  			_ = message.Set(tag, string(msg.msgid), msg.localString)
   105  		}
   106  	}
   107  	SetLang("en")
   108  	msgIDUniq = map[string]bool{} // Clear out that memory as no longer needed
   109  }
   110  
   111  func SetLang(lang string) {
   112  	// Allow a lang var to be used
   113  	tag, _, _ := langMatcher.Match(language.Make(lang))
   114  	defaultLangPrinter = message.NewPrinter(tag)
   115  }
   116  
   117  func GetStatusHint(code string) (int, bool) {
   118  	i, ok := statusHints[code]
   119  	return i, ok
   120  }