# 添加国际化支持

## 1. 配置 locale

```properties
spring:
  web:
    locale: zh
    locale-resolver: accept_header
```

> spring boot 默认使用 <mark style="color:blue;">**AcceptHeaderLocaleResolver**</mark> 支持国际化。

## 2. 在 /src/main/resources 源文件夹下创建以下文件：

* messages.properties

  ```properties
  license.create.message=%s
  license.update.message=%s
  license.delete.message=%s, %s
  ```
* messages\_en.properties

  ```properties
  license.create.message=License created %s
  license.update.message=License %s updated
  license.delete.message=Deleting license with id %s for the organization %s
  ```
* messages\_zh.properties

  ```properties
  license.create.message=许可证 %s 被创建
  license.update.message=许可证 %s 被更新
  license.delete.message=删除组织 %s 下的id为 %s 的许可证
  ```

## 3. 修改 Controller

```java
@PostMapping
public String createLicense(
        @PathVariable("organizationId") String organizationId,
        @RequestBody License license) {
    return licenseService.createLicense(license, organizationId);
}

@PutMapping
public String updateLicense(
        @PathVariable("organizationId") String organizationId,
        @RequestBody License license) {
    return licenseService.updateLicense(license, organizationId);
}

@DeleteMapping
public String deleteLicense(
        @PathVariable("organizationId") String organizationId,
        @PathVariable("licenseId") String licenseId) {
    return licenseService.deleteLicense(licenseId, organizationId);
}
```

## 4. 修改 Service 接口和实现

{% code overflow="wrap" %}

```java
@Autowired
private MessageSource messageSource;

public String createLicense(License license, String organizationId) {
    String responseMessage = null;
    if (license != null) {
        license.setOrganizationId(organizationId);
        responseMessage = String.format(
                messageSource.getMessage("license.create.message", null, LocaleContextHolder.getLocale()),
                license);
    }
    return responseMessage;
}

public String updateLicense(License license, String organizationId) {
    String responseMessage = null;
    if (license != null) {
        license.setOrganizationId(organizationId);
        responseMessage = String.format(
                messageSource.getMessage("license.update.message", null, LocaleContextHolder.getLocale()),
                license);
    }
    return responseMessage;
}

public String deleteLicense(String licenseId, String organizationId) {
    return String.format(
            messageSource.getMessage("license.delete.message", null, LocaleContextHolder.getLocale()),
            licenseId,
            organizationId);

}
```

{% endcode %}

## 5. 测试

<figure><img src="/files/DfuTUZu3F12yqRSttEma" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Wo0dyZhcwsuyMEP4JABP" alt=""><figcaption></figcaption></figure>

**国际化的关键在于&#x20;**<mark style="color:blue;">**Accept-Language**</mark>**&#x20;请求头！**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/ji-chu/spring/group-1/spring-cloud/shi-yong-spring-boot-gou-jian-wei-fu-wu/tian-jia-guo-ji-hua-zhi-chi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
