Created
October 5, 2016 20:59
-
-
Save miguelplazasr/7eb87038a9bd71cf1ecbcad520224d77 to your computer and use it in GitHub Desktop.
Conviert un archivo i18n de Symfony en JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AppBundle\Controller\api; | |
use FOS\RestBundle\Controller\FOSRestController; | |
use FOS\RestBundle\Routing\ClassResourceInterface; | |
use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\Yaml\Yaml; | |
class TranslateController extends FOSRestController implements ClassResourceInterface | |
{ | |
/** | |
* Get array by locale | |
* | |
* @ApiDoc( | |
* resource = true, | |
* section="Translate", | |
* statusCodes = { | |
* 200 = "Returned when successful", | |
* 400 = "Returned when the page is not found" | |
* } | |
* ) | |
* @param $part | |
* @param $locale | |
* @return JsonResponse | |
*/ | |
public function getLocaleAction($part, $locale) | |
{ | |
$response = new JsonResponse(); | |
$response | |
->setData($this->convertYml($part, $locale)) | |
->setStatusCode(200); | |
return $response; | |
} | |
private function convertYml($part, $locale) | |
{ | |
$file = $part . '.' . $locale . '.yml'; | |
try { | |
$object = Yaml::parse(file_get_contents($this->get('kernel')->getRootDir() . "/Resources/translations/" . $file)); | |
return $object; | |
} catch (\Exception $e) { | |
return 'The file "' . $file . '" does not exist.'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment