github.com/richardwilkes/toolbox@v1.121.0/i18n/localization_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package i18n 11 12 import ( 13 "testing" 14 15 "github.com/richardwilkes/toolbox/check" 16 ) 17 18 func TestLocalization(t *testing.T) { 19 de := make(map[string]string) 20 de["a"] = "1" 21 langMap["de"] = de 22 deDE := make(map[string]string) 23 deDE["a"] = "2" 24 langMap["de_dn"] = deDE 25 Language = "de_dn.UTF-8" 26 check.Equal(t, "2", Text("a")) 27 Language = "de_dn" 28 check.Equal(t, "2", Text("a")) 29 Language = "de" 30 check.Equal(t, "1", Text("a")) 31 Language = "xx" 32 check.Equal(t, "a", Text("a")) 33 delete(langMap, "de_dn") 34 Language = "de" 35 check.Equal(t, "1", Text("a")) 36 } 37 38 func TestAltLocalization(t *testing.T) { 39 check.Equal(t, "Hello!", Text("Hello!")) 40 SetLocalizer(func(_ string) string { return "Bonjour!" }) 41 check.Equal(t, "Bonjour!", Text("Hello!")) 42 SetLocalizer(nil) 43 check.Equal(t, "Hello!", Text("Hello!")) 44 }