Manage Laravel translations with Google Spreadsheets
- 2 Min. Read.
Managing translations with Google Spreadsheets in your Laravel application is very easy thanks to nikaia/translation-sheet.
Install the package
In your Laravel project root, run the follwing composer command:
1 |
composer require nikaia/translation-sheet |
Creating a project in the Google Cloud Console
- Head to https://console.developers.google.com/ and create a new project.
- Activate the Sheet Api for the project (Library -> search for ‘Sheets’ -> Enable)
- Create a Service Account and credentials
- Navigate to “Credentials”
- Click “Create credentials”
- Choose “Service Account key”
- Choose A “New Service Account” in the “Service account” select
- Choose a name. (ie. This is the name that will show up in the Spreadsheet history operations), “Editor” as role and “JSON” for the key type.
- Save the credentials to ‘resources/google/service-account.json’ folder. (This can be configured later)
- Make sure to write down the service account email, you will need it later for the package configuration.
Create a new spreadsheet
- Create a blank/new spreadsheet here https://docs.google.com/spreadsheets/ .
- Share it with the service account email (from the cloud console) with
Can edit
permission. - Copy the ID from the URL, the ID looks like this: https://docs.google.com/spreadsheets/d/1Vt3QwJV6qovvHZhSxGVdpb8VEYEzRwV-gxyGlRwugWa/edit#gid=0
Configure your Laravel project
In your .env file, add the following variables:
1 2 3 4 5 6 7 8 9 10 11 |
# The service account email (from the cloud console) TS_SERVICE_ACCOUNT_EMAIL=***@***.iam.gserviceaccount.com # The path to the downloaded service account credentials file TS_SERVICE_ACCOUNT_CREDENTIALS_FILE=resources/google/service-account.json # The ID of the spreadsheet that we will be using for translation TS_SPREADSHEET_ID=xxxx # The locales of the application (comma separated) TS_LOCALES=en,fr,de |
Then, in your console run the following sequence of commands:
1 2 3 4 5 6 7 8 |
# Setup the sheet (this has to be run once) php artisan translation_sheet:setup # Rewrite the local language files, to avoid conflicts php artisan translation_sheet:prepare # Publish the translations to the sheet php artisan translation_sheet:push |
Adding translations to the sheet
Open up the Google sheet (if you forgot the URL, you can open it by executing php artisan translation_sheet:open
in the console).
1 2 3 4 |
=GOOGLETRANSLATE(COLUMN, "SOURCE LANGUAGE", "DESTINATION LANGUAGE") # For example =GOOGLETRANSLATE(B11, "EN", "FR") |
Importing translations into Laravel
After translating the keys to your desired languages, it’s time to import them back into the Laravel application. You can import the translations with the following command:
1 |
php artisan translation_sheet:pull |