github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/i18n/datamap.go (about)

     1  package i18n
     2  
     3  import (
     4  	f0 "github.com/isyscore/isc-gobase/file"
     5  	"github.com/isyscore/isc-gobase/isc"
     6  )
     7  
     8  type I18NMap struct {
     9  	Language        string            // 当前的语言
    10  	DefaultLanguage string            // 默认语言
    11  	Data            map[string]string // 当前的字符串映射
    12  	DefaultData     map[string]string // 默认的字符串映射,当从Data内找不到key时,从此处找,再找不到就报错
    13  }
    14  
    15  var innerMap *I18NMap
    16  
    17  func NewI18NMap(language string, filePath string) *I18NMap {
    18  	m := loadPo(filePath)
    19  	return &I18NMap{
    20  		Language:        language,
    21  		DefaultLanguage: language,
    22  		Data:            m,
    23  		DefaultData:     m,
    24  	}
    25  }
    26  
    27  // po 文件格式
    28  // msgid "hello"
    29  // msgstr "你好"
    30  
    31  func loadPo(filePath string) map[string]string {
    32  	m := make(map[string]string)
    33  	lines := f0.ReadFileLines(filePath)
    34  	for _, s := range lines {
    35  		ss := isc.ISCString(s)
    36  		key := ss.SubStringBefore(" ")
    37  		val := ss.SubStringAfter(" ").TrimSpace().Trim("\"").ReplaceAll("\\n", "\n").ReplaceAll("\\r", "\r").ReplaceAll("\\t", "\t").ReplaceAll("\\\"", "\"").ReplaceAll("\\\\", "\\")
    38  		m[string(key)] = string(val)
    39  	}
    40  	return m
    41  }