# Generate translations

How to translate the Greyd Theme with **WP-CLI** and **PoEdit**.

## Setup

- Download and Install PoEdit: [Get Poedit desktop app](https://poedit.net/download)
- Install and setup WP-CLI: [Installing WP-CLI with MAMP](https://tommcfarlin.com/installing-wp-cli-with-mamp/)
- Follow the instructions on how to generate translations: [Internationalization | Block Editor Handbook](https://developer.wordpress.org/block-editor/how-to-guides/internationalization/)

## Generate translations

### Create the template file:
Move to the theme folder and generate the translation template. You probably want to change the uri according to your setup.

```bash
cd /Applications/MAMP/htdocs/wordpress/wp-content/themes/greyd-theme
wp i18n make-pot ./ languages/greyd-theme.pot
```

### Create the `.po` file:
1. Set the name of the file the same as the locale, eg. `de_DE.po`
2. Here you can find a [Complete List of WordPress Locale Codes](https://wpastra.com/docs/complete-list-wordpress-locale-codes/).
3. Once created you can start translating.

### Use PoEdit to update your translations:
1. Open the `.po` file with PoEdit.
2. Go to **'Translation > Update from POT File...'** and select the generated file.
3. The source text in your file is now updated.
4. Translate all parts of the theme.
5. Save your file and generate an `.mo` file.
6. Now take a look at your WordPress Backend (maybe change your user language).

### Generating Javascript Translation Files

In order for them to work properly you have to rename the files after creation.
Just run the following script to do it for you.

```bash
wp i18n make-json languages/de_DE.po --no-purge

for file in languages/*.json; do
    # Get the filename without extension
    filename=$(basename -- "$file" .json)
    # Check if the filename already contains the textdomain prefix
    if [[ "$filename" != "greyd-theme-"* ]]; then
        # Check if a file with the same name exists with the textdomain prefix
        if [ -f "languages/greyd-theme-$filename.json" ]; then
            # If it exists, delete it
            rm "languages/greyd-theme-$filename.json"
        fi
        # Rename the file with the textdomain prefix
        mv "$file" "languages/greyd-theme-$filename.json"
    fi
done
```