github.com/erda-project/erda-infra@v1.0.9/providers/i18n/lang.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 i18n
    16  
    17  import (
    18  	"sort"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  // LanguageCode .
    24  type LanguageCode struct {
    25  	Code    string  `json:"code"`
    26  	Quality float32 `json:"quality"`
    27  }
    28  
    29  // RestrictedCode .
    30  func (lc *LanguageCode) RestrictedCode() string {
    31  	idx := strings.Index(lc.Code, "-")
    32  	if idx < 0 {
    33  		return lc.Code
    34  	}
    35  	return lc.Code[:idx]
    36  }
    37  
    38  // ElaboratedCode .
    39  func (lc *LanguageCode) ElaboratedCode() string {
    40  	idx := strings.Index(lc.Code, "-")
    41  	if idx < 0 {
    42  		return ""
    43  	}
    44  	return lc.Code[idx+1:]
    45  }
    46  
    47  // Codes .
    48  func (lc *LanguageCode) Codes() (string, string) {
    49  	parts := strings.SplitN(lc.Code, "-", 3)
    50  	if len(parts) > 1 {
    51  		return parts[0], parts[1]
    52  	}
    53  	return parts[0], ""
    54  }
    55  
    56  // String .
    57  func (lc *LanguageCode) String() string {
    58  	if lc.Quality == 1 {
    59  		return lc.Code
    60  	}
    61  	return lc.Code + ";" + strconv.FormatFloat(float64(lc.Quality), 'f', -1, 32)
    62  }
    63  
    64  // LanguageCodes .
    65  type LanguageCodes []*LanguageCode
    66  
    67  // Len 返回slice长度
    68  func (ls LanguageCodes) Len() int { return len(ls) }
    69  
    70  // Less 比较两个位置上的数据
    71  func (ls LanguageCodes) Less(i, j int) bool {
    72  	return ls[i].Quality > ls[j].Quality
    73  }
    74  
    75  // Swap 交换两个位置上的数据
    76  func (ls LanguageCodes) Swap(i, j int) {
    77  	ls[i], ls[j] = ls[j], ls[i]
    78  }
    79  
    80  // ParseLanguageCode .
    81  func ParseLanguageCode(text string) (list LanguageCodes, err error) {
    82  	for _, item := range strings.Split(text, ",") {
    83  		parts := strings.SplitN(item, ";", 2)
    84  		lc := &LanguageCode{
    85  			Quality: 1,
    86  		}
    87  		if len(parts) > 1 {
    88  			q := strings.TrimSpace(parts[1])
    89  			if len(q) > 0 {
    90  				kv := strings.Split(q, "=")
    91  				if len(kv) == 2 && kv[0] == "q" {
    92  					q, err := strconv.ParseFloat(kv[1], 32)
    93  					if err != nil {
    94  						sort.Sort(list)
    95  						return list, err
    96  					}
    97  					lc.Quality = float32(q)
    98  				}
    99  			}
   100  		}
   101  		lc.Code = strings.TrimSpace(parts[0])
   102  		if len(item) > 0 {
   103  			list = append(list, lc)
   104  		}
   105  	}
   106  	sort.Sort(list)
   107  	return list, nil
   108  }