github.com/erda-project/erda-infra@v1.0.9/providers/i18n/examples/main.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"embed"
    19  	"os"
    20  
    21  	"github.com/erda-project/erda-infra/base/logs"
    22  	"github.com/erda-project/erda-infra/base/servicehub"
    23  	"github.com/erda-project/erda-infra/providers/i18n"
    24  )
    25  
    26  //go:embed i18n
    27  var i18nFS embed.FS
    28  
    29  type provider struct {
    30  	Log  logs.Logger
    31  	I18n i18n.I18n       `autowired:"i18n"`
    32  	Tran i18n.Translator `translator:"hello"`
    33  }
    34  
    35  func (p *provider) Init(ctx servicehub.Context) error {
    36  	if err := p.I18n.RegisterFilesFromFS("i18n", i18nFS); err != nil {
    37  		return err
    38  	}
    39  	langs, err := i18n.ParseLanguageCode("en,zh-CN;q=0.9,zh;q=0.8,en-US;q=0.7,en-GB;q=0.6")
    40  	if err != nil {
    41  		return err
    42  	}
    43  	i := ctx.Service("i18n").(i18n.I18n)
    44  	text := i.Text("hello", langs, "name")
    45  	p.Log.Info(text)
    46  
    47  	text = p.Tran.Text(langs, "name")
    48  	p.Log.Info(text)
    49  
    50  	text = p.Tran.Text(langs, "other")
    51  	p.Log.Info(text)
    52  
    53  	text = p.Tran.Text(langs, "common name")
    54  	p.Log.Info(text)
    55  
    56  	text = p.Tran.Text(langs, "file name")
    57  	p.Log.Info(text)
    58  
    59  	text = p.Tran.Sprintf(langs, "${Internal Error}: reason ${Reason} %s", "test error")
    60  	p.Log.Info(text)
    61  
    62  	text = i.Sprintf("not-exist-namespace", langs, "${Internal Error}: reason ${Reason} %s", "test error")
    63  	p.Log.Info(text)
    64  	return nil
    65  }
    66  
    67  func init() {
    68  	servicehub.Register("hello", &servicehub.Spec{
    69  		Services:     []string{"hello"},
    70  		Dependencies: []string{"i18n"},
    71  		Description:  "hello for example",
    72  		Creator: func() servicehub.Provider {
    73  			return &provider{}
    74  		},
    75  	})
    76  }
    77  
    78  func main() {
    79  	hub := servicehub.New()
    80  	hub.Run("examples", "", os.Args...)
    81  }
    82  
    83  // OUTPUT:
    84  // INFO[2021-09-14 18:05:25.629] load i18n files: [], [hello.yaml]             module=i18n
    85  // INFO[2021-09-14 18:05:25.629] provider i18n initialized
    86  // INFO[2021-09-14 18:05:25.629] 名字                                            module=hello
    87  // INFO[2021-09-14 18:05:25.630] 名字                                            module=hello
    88  // INFO[2021-09-14 18:05:25.630] other                                         module=hello
    89  // INFO[2021-09-14 18:05:25.630] 内部错误: reason 未知 test error                    module=hello
    90  // INFO[2021-09-14 18:05:25.630] Internal Error: reason Reason test error      module=hello
    91  // INFO[2021-09-14 18:05:25.630] provider hello (depends services: [i18n], providers: [i18n]) initialized
    92  // INFO[2021-09-14 18:05:25.630] signals to quit: [hangup interrupt terminated quit]