go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/web/localization_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"testing"
    12  
    13  	"go.charczuk.com/sdk/assert"
    14  )
    15  
    16  func Test_Localization_Printer(t *testing.T) {
    17  	l := new(Localization)
    18  
    19  	l.AddEntries(
    20  		LocalizationEntry{
    21  			Tag:     "en",
    22  			Key:     "hello",
    23  			Message: "hello",
    24  		},
    25  		LocalizationEntry{
    26  			Tag:     "es",
    27  			Key:     "hello",
    28  			Message: "hola",
    29  		},
    30  		LocalizationEntry{
    31  			Tag:     "fr",
    32  			Key:     "hello",
    33  			Message: "bonjour",
    34  		},
    35  		LocalizationEntry{
    36  			Tag:     "en",
    37  			Key:     "goodbye",
    38  			Message: "goodbye",
    39  		},
    40  		LocalizationEntry{
    41  			Tag:     "es",
    42  			Key:     "goodbye",
    43  			Message: "adios",
    44  		},
    45  		LocalizationEntry{
    46  			Tag:     "fr",
    47  			Key:     "goodbye",
    48  			Message: "au revoir",
    49  		},
    50  	)
    51  
    52  	err := l.Initialize()
    53  	assert.ItsNil(t, err)
    54  
    55  	en := l.Printer("en-us")
    56  	es := l.Printer("es")
    57  	fr := l.Printer("fr")
    58  
    59  	assert.ItsEqual(t, "hello", en.Print("hello"))
    60  	assert.ItsEqual(t, "hola", es.Print("hello"))
    61  	assert.ItsEqual(t, "bonjour", fr.Print("hello"))
    62  
    63  	assert.ItsEqual(t, "goodbye", en.Print("goodbye"))
    64  	assert.ItsEqual(t, "adios", es.Print("goodbye"))
    65  	assert.ItsEqual(t, "au revoir", fr.Print("goodbye"))
    66  
    67  	assert.ItsEqual(t, "not-found", en.Print("not-found"))
    68  	assert.ItsEqual(t, "not-found", es.Print("not-found"))
    69  	assert.ItsEqual(t, "not-found", fr.Print("not-found"))
    70  }