Files
SCM-Manager/docs/en/development/plugins/i18n-for-plugins.md

81 lines
1.9 KiB
Markdown
Raw Normal View History

---
title: i18n for Plugins
---
2020-03-09 08:01:43 +01:00
How to internationalize your own plugin
### Create the plugins.json file
The translation file for plugins should be stored in the resources path
2020-03-31 17:28:30 +02:00
locales/*{lang}*/plugins.json
2020-03-09 08:01:43 +01:00
2020-03-31 17:28:30 +02:00
All translation keys are parts of a **unique root key**. It is
recommended to **use the maven artifactId of the plugin as root
key** to avoid conflicts with other plugin. All translation files
2020-03-09 08:01:43 +01:00
would be collected and merged to a single file containing all
2020-03-31 17:28:30 +02:00
translations. Therefore it is **necessary to use a unique root key**
2020-03-09 08:01:43 +01:00
for the translations.
2020-03-31 17:28:30 +02:00
***Example:***
2020-03-09 08:01:43 +01:00
the translation file of the svn plugin is stored in
locales/en/plugins.json
2020-03-31 17:28:30 +02:00
```json
{
"scm-svn-plugin": {
"information": {
"checkout" : "Checkout repository"
}
}
}
```
2020-03-09 08:01:43 +01:00
### Usage in the own React components
SCM-Manager use react-i18next to render translations.
#### Functional components
The following steps are needed to use react-i18next in the own
functional components:
- import react-i18next
```javascript
import { useTranslation } from "react-i18next";
```
- use the translation keys like this:
```javascript
const [t] = useTranslation("plugins");
...
<h3>{t("scm-svn-plugin.information.checkout")}</h3>
```
#### Class components
2020-03-09 08:01:43 +01:00
The following steps are needed to use react-i18next in the own
class components:
2020-03-09 08:01:43 +01:00
- import react-i18next
2020-03-31 17:28:30 +02:00
```javascript
2020-04-02 15:14:32 +02:00
import { WithTranslation, withTranslation } from "react-i18next";
2020-03-31 17:28:30 +02:00
```
2020-03-09 08:01:43 +01:00
2020-03-31 17:28:30 +02:00
- declare the translation method `t: string => string` as property
```javascript
2020-04-02 15:14:32 +02:00
type Props = WithTranslation & {
***your props***
2020-03-31 17:28:30 +02:00
}
```
2020-03-09 08:01:43 +01:00
- wrap the react component with the translate method and give the json
translation file name \"plugins\"
2020-03-31 17:28:30 +02:00
```javascript
2020-04-02 15:14:32 +02:00
export default withTranslation("plugins")(MyPluginComponent);
2020-03-31 17:28:30 +02:00
```
2020-03-09 08:01:43 +01:00
- use the translation keys like this:
2020-03-31 17:28:30 +02:00
```javascript
const { t } = this.props;
<h3>{t("scm-svn-plugin.information.checkout")}</h3>
```