Laravel Translation via Google Spreadsheet
Websites nowadays are needed to support multi-lingual, in order to reach more people in many countries. Along with fast growth of technologies, we have a lot of framework equipped with packages.
This time I’d like to write about one of my favorite framework, Laravel, along with a package by Nikaia. This package use Google Spreadsheet to facilitate team collaboration. As you know that Google Spreadsheet is real time cloud-synchronized, this makes possible to access and edit at the same time without making any conflict with another team member.
# Installation
- Install package
$ composer require nikaia/translation-sheet
- Add service provider to your ‘config/app.php’
Nikaia\TranslationSheet\TranslationSheetServiceProvider::class,
- Configuration can be done via environments variables, but if you prefer you can override the configuration by publishing the package config file using :
$ php artisan vendor:publish --provider="Nikaia\TranslationSheet\ServiceProvider"
# Google API
- Go to https://console.developers.google.com/
- Create a new project
- Activate Sheet API for the project:
Go to Library > Search Google Sheet API > Select it > Enable - Create a Service Account and Credentials
- Go 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.
(You can choose another name/folder if you want in your application
folder)
- Make sure to write down the service account email, you will need it later
for the package configuration.
# Spreadsheet
- Create a blank/new spreadsheet here https://docs.google.com/spreadsheets/ .
- Share it with the service account email with
Can edit
permission
# Configuration
In your .env file or in your published config file (config/translation_sheet.php
)
- Set service account email
TS_SERVICE_ACCOUNT_EMAIL=***@***.iam.gserviceaccount.com
- Set path to the downloaded service account credentials
TS_SERVICE_ACCOUNT_CREDENTIALS_FILE=resources/google/service-account.json
- Set ID of the spreadsheet that we will be using for translation
TS_SPREADSHEET_ID=xxxx
- Set locales of the application (separated by comma)
TS_LOCALES=fr,en,es
# Usage
- Setup the spreadsheet (only once)
$ php artisan translation_sheet:setup
- Prepare the sheet (to avoid conflicts, run this command to rewrite the locale languages files)
$ php artisan translation_sheet:prepare
- Publish translation to sheet
$ php artisan translation_sheet:push
- Lock translations on the spreadsheet (the sheet will be disabled for edit)
$ php artisan translation_sheet:lock
- Unlock translations on the spreadsheet
$ php artisan translation_sheet:unlock
- Pull translations (this will write languages files in your applications. You can use git diff here to make sure everything is ok)
$ php artisan translation_sheet:pull
- Open spreadsheet in browser
$ php artisan translation_sheet:open
Note: Don’t forget to share your spreadsheet to your team.
You can follow this package’s updates in Nikaia github.
Thanks for reading. I hope this article can be a big help for your multi language website problem. Feel free to leave comments below if you have any questions. Cheers!
“If you talk to a man in a language he understand, that goes to his head. If you talk to him in his language, that goes to his heart.” ~ (Nelson Mandela)