apixu / apixu-php

PHP library for Apixu Weather API
http://www.apixu.com
MIT License
22 stars 12 forks source link

custom lang #9

Closed acharsoft closed 5 years ago

acharsoft commented 5 years ago

how i can use my custom lang in this api?

andreiavrammsd commented 5 years ago

There is no support for localization. You could use an adapter for the API responses you need it for.

Response:

<?php declare(strict_types = 1);

namespace YourApp\LocalizedResponse;

use Apixu\Response\Location;

class LocalizedLocation
{
    private static $translations = [
        'Islington, Greater London' => 'Islington, Grand Londres',
        // other translations
    ];

    /**
     * @var Location
     */
    private $location;

    /**
     * @param Location $location
     */
    public function __construct(Location $location)
    {
        $this->location = $location;
    }

    /**
     * @return string
     */
    public function getRegion() : string
    {
        $region = $this->location->getRegion();

        return self::$translations[$region] ?? $region;
    }

    // the other methods
}

Usage:

$search = $api->search($q);

/** @var \Apixu\Response\Location $location */
foreach ($search as $location) {
    $location = new YourApp\LocalizedResponse\LocalizedLocation($location);
    echo $location->getRegion()."\n\n";
}

Or, of course, "translate" when you display:

$translations = [
    'Islington, Greater London' => 'Islington, Grand Londres',
    // other translations
];

/** @var \Apixu\Response\Location $location */
foreach ($search as $location) {
    $region = $this->location->getRegion();
    echo $translations[$region] ?? $region;
    echo "\n\n";
}