Exemple #1
0
// GetLocaleLabel returns field label by locale.
// locale format: en en_US  / zh zh_CN zh_HK etc.
func GetLocaleLabel(v interface{}, fieldName string, locale string) string {
	labelValue, e := GetLabelTag(v, fieldName)

	// not exist
	if !e {
		return mcore.ToLabel(fieldName)
	}
	locale = strings.Replace(locale, "-", "_", -1)

	lang := strings.Split(locale, "_")[0]

	m := ParseKeyValueMap(labelValue)

	// include lang_coutry locale
	if v, ok := m[locale]; ok {
		return v
	}

	// include lang
	if v, ok := m[lang]; ok {
		return v
	}

	// defult en
	if v, ok := m["en"]; ok {
		return v
	}

	// default return
	return mcore.ToLabel(fieldName)
}
Exemple #2
0
// Perform a message look-up for the given locale and message using the given arguments.
// When either an unknown locale or message is detected, a specially formatted string is returned.
func Message(locale, message string, args ...interface{}) string {
	language, region := parseLocale(locale)

	messageConfig, knownLanguage := messages[language]
	if !knownLanguage {
		logger.Warnf("Unsupported language for locale '%s' and message '%s', trying default language", locale, message)
		messageConfig, knownLanguage = messages[defaultLanguage]
		if !knownLanguage {
			logger.Warnf("Unsupported default language for locale '%s' and message '%s'", defaultLanguage, message)
			return fmt.Sprintf(unknownValueFormat, message)
		}
	}

	// This works because unlike the mconfig documentation suggests it will actually
	// try to resolve message in DEFAULT if it did not find it in the given section.
	if value, err := messageConfig.String(region, message); err == nil {
		return doMsgArgs(value, args...)
	}
	// try default region
	if value, err := messageConfig.String("", message); err == nil {
		return doMsgArgs(value, args...)
	}
	// cannot find
	logger.Debugf("Cannot found message. Locale:%s, Key:%s", locale, message)
	return mcore.ToLabel(message)
}
Exemple #3
0
// GetTableColumnLabel
// returns table column locale label message.
// if table is "", set table to common
// key format: common-UserName etc.
func GetTableColumnLabel(locale, table, column string) string {

	value := ""

	if table == "" {
		table = "common"
	}

	key := fmt.Sprintf("%s-%s-label", table, column)

	if Contains(locale, key) {
		logger.Tracef("Found table field label. Table:%s, Column:%s Key: %s", table, column, key)
		value = Message(locale, key)
	}

	// not found in table label, try common
	keyComm := fmt.Sprintf("%s-label", column)

	if value == "" && Contains(locale, keyComm) {
		value = Message(locale, keyComm)
	}

	// if not found, name to label name.
	if value == "" {
		logger.Debugf("Not found table field label. Table:%s, Column:%s", table, column)
		value = mcore.ToLabel(column)
	}

	return value
}
Exemple #4
0
// DemoKey demo
func DemoKey() {
	keyStr := mcore.NewString("OrgName-label")
	//keyStr = keyStr.TrimStarts("label.", "msg.", "label-", "msg-")
	//keyStr = keyStr.TrimEnds(".label", ".msg", "-label", "-msg")
	keyStr = keyStr.ReplaceAll("label", "").ReplaceAll("msg", "")
	fmt.Println(keyStr)
	fmt.Println(mcore.ToLabel(keyStr.String()))
}
Exemple #5
0
// return value and if contain
func MessageContains(locale string, key string, args ...interface{}) (string, bool) {
	language, region := parseLocale(locale)
	messageConfig, knownLanguage := messages[language]

	keyLabel := mcore.ToLabel(key)

	if !knownLanguage {
		return keyLabel, false
	}
	// check message.
	if v, err := messageConfig.String(region, key); err != nil {
		return keyLabel, false
	} else {
		return v, true
	}
}
Exemple #6
0
// Demo demo
func Demo() {
	name := "How_HOW-How_how"
	println(name)
	println(mcore.ToLabel(name))
	println(mcore.UpperCaseFirst("how are you"))
}