Skip to content

Instantly share code, notes, and snippets.

@ChVuagniaux
Last active November 7, 2022 11:05
Show Gist options
  • Save ChVuagniaux/868caf23f7b2a34c47f4ed62d922c133 to your computer and use it in GitHub Desktop.
Save ChVuagniaux/868caf23f7b2a34c47f4ed62d922c133 to your computer and use it in GitHub Desktop.
Translate OctoberCMS RainLab.Location (https://octobercms.com/plugin/rainlab-location) Countries via https://RESTcountries.eu API
<?php
use RainLab\Location\Models\Country;
Route::get('generate-country-translations', function () {
$countries = Country::all();
$countries->each(function (Country $country) {
$response = file_get_contents("https://restcountries.com/v2/alpha/{$country->code}");
$countryInformations = json_decode($response);
$country->setAttributeTranslated('name', $countryInformations->translations->fr, 'fr');
$country->setAttributeTranslated('name', $countryInformations->name, 'en');
$country->setAttributeTranslated('name', $countryInformations->translations->de, 'de');
$country->setAttributeTranslated('name', $countryInformations->name, 'pt');
$country->save();
});
return response('end');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment