Skill站外收录 @skilllib/skill-2117939930272414316
i18n
将面向用户的静态文本抽取到统一的资源文件中进行集中管理,支持按语言代码动态加载对应翻译内容,使前端展示文本可灵活适配多语言环境,并确保代码中不再硬编码任何用户可见字符串。
效率工具 AI 技能
Skills.MD
## Workflow
Copy this checklist and track your progress:
```
Externalizing and translating strings:
- [ ] Step 1: Create String Bundle
- [ ] Step 2: Transfer Strings
- [ ] Step 3: Translate Strings for Requested Languages
- [ ] Step 4: Update References in Go Files
- [ ] Step 5: Update References in Templates
- [ ] Step 6: Versioning
```
#### Step 1: Create String Bundle
Create a `text.yaml` file under the `resources` directory of the microservice, if one does not already exist.
#### Step 2: Transfer Strings
Locate static strings in the microservice that are ultimately shown to the end user. These are likely to be in `service.go` or in HTML or text templates in the `resources` directory.
For each string, create an entry in `text.yaml` that maps a unique key to its localized value on a per language basis. Use PascalCase for the string key (e.g. `MyString`) and ISO 639 language codes for each language (e.g. `en-US`).
```yaml
HelloWorld:
en: Hello World
```
#### Step 3: Translate Strings for Requested Languages
Update `text.yaml` and add a localized value for each explicitly requested language.
Use the ISO 639 language code under the key of each localization.
```yaml
HelloWorld:
en: Hello World
en-AU: G'day World
fr: Bonjour le Monde
es: Hola Mundo
```
The `en` or `en-US` localizations are used by default when no other language matches the request's context.
If neither localization is included, a `default` value should be provided instead.
```yaml
HolaMundo:
fr: Bonjour le Monde
es: Hola Mundo
default: Hola Mundo
```
#### Step 4: Update References in Go Files
Use `svc.MustLoadResString` to load strings in Go files such as `service.go`.
Before:
```go
func (svc *Service) HelloWorld(w http.ResponseWriter, r *http.Request) (err error) {
w.Write([]byte("Hello World"))
return err
}
```
After:
```go
func (svc *Service) HelloWorld(w http.ResponseWriter, r *http.Request) (err error) {
ctx := r.Context()
textHelloWorld := svc.MustLoadResString(ctx, "HelloWorld")
w.Write([]byte(textHelloWorld))
return err
}
```
#### Step 5: Update References in Templates
To use localized strings in HTML templates, load all strings with `svc.MustLoadResStrings` into a map, pass the map as part of the data to the template, and use the map in the template to obtain the string by key, instead of the static string.
Before:
```go
func (svc *Service) HelloWorld(w http.ResponseWriter, r *http.Request) (err error) {
data := struct{
OtherData int
}{
OtherData: 5,
}
content, err := svc.ExecuteResTemplate("mytemplate.html", data)
if err != nil {
return errors.Trace(err)
}
w.Write(content)
return nil
}
```
```html
<b>Hello World</b>
```
After:
```go
func (svc *Service) HelloWorld(w http.ResponseWriter, r *http.Request) (err error) {
ctx := r.Context()
data := struct{
OtherData int
Text map[string]string
}{
OtherData: 5,
Text: svc.MustLoadResStrings(ctx),
}
content, err := svc.ExecuteResTemplate("mytemplate.html", data)
if err != nil {
return errors.Trace(err)
}
w.Write(content)
return nil
}
```
```html
<b>{{ .Text.HelloWorld }}</b>
```
#### Step 6: Document the Microservice
Update the microservice's local `AGENTS.md` file to indicate that user-facing text must be externalized and translated to the requested languages.
#### Step 7: Versioning
Run `go generate` to version the code.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「i18n」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次