# locale **Repository Path**: nrgo/locale ## Basic Information - **Project Name**: locale - **Description**: locale interface for nrgo - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-02-19 - **Last Updated**: 2023-04-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # locale #### 介绍 locale interface for nrgo #### 接口定义 ```go // Pack 语言包标准接口 type Pack interface { // Localize 返回根据制定的区域,语言包类别,语言包标记处理得到的具体字符串 // `locale` 指定的区域,如 `en-us`,但不强制使用此区域标记,根据语言包具体实现判断 // `catalog` 指定的语言包查询的类别,如 `sys` // `fallback` 必须提供,当语言包查询失败时,会以`fallback`提供默认返回 Localize(locale, catalog, key, fallback string, args ...interface{}) string } ``` #### 使用说明 实现 `Pack` 接口,提供语言文本查询 ```go type simplePack struct { ... } func (sp *simplePack) Localize(locale, catalog, key, fallback string, args ...interface{}) string { ... } ``` 在编码时,对需要使用多语言的文本提前进行特殊编码 * 不指定 `locale` ,用于按默认区域获取语言文本 ```go // L 提供语言包的对外调用方法,由语言包的具体实现返回处理结果 // @catalog 指定的语言包查询的类别,如 `sys` // @key 语言文本对应的 key 值 // @fallback 必须提供,当语言包查询失败时,会以`fallback`提供默认返回 // @args... 若语言文本为格式化模板,则需要提供具体替换值 // returns // @string 语言文本 func L(catalog, key, fallback string, args ...interface{}) string // demo currentLocaleString := L(`catalog`, `demo`, `fallback`) // demo2 currentLocaleString2 := L(`catalog`, `demo`, `fallback %s`, `hello locale`) ``` * 指定 `locale` ,主要用于同时提供多语言的演示 ```go // LWL 提供语言包的对外调用方法(同时指定locale,主要用于同时提供多语言的演示) // @locale 指定使用的区域 // @catalog 指定的语言包查询的类别,如 `sys` // @key 语言文本对应的 key 值 // @fallback 必须提供,当语言包查询失败时,会以`fallback`提供默认返回 // @args... 若语言文本为格式化模板,则需要提供具体替换值 // returns // @string 语言文本 func LWL(locale, catalog, key, fallback string, args ...interface{}) string // demo cnLocaleString := LWL(`zh-CN` ,`catalog`, `demo`, `我是失败字符串`) // demo2 usLocaleString2 := L(`en-US`, `demo`, `fallback %s`, `hello locale`) ```