Permalink
Browse files

Add an api to transliterate serbian cyrillic (#699)

  • Loading branch information...
Vincent Lequertier authored and pascalchevrel committed May 2, 2016
1 parent c65c251 commit 930e5021bc31b38003473e471b03af559097f52e
Showing with 43 additions and 3 deletions.
  1. +1 −0 README.md
  2. +10 −3 app/classes/Transvision/API.php
  3. +3 −0 app/controllers/api.php
  4. +27 −0 app/models/api/transliterate.php
  5. +2 −0 tests/functional/api.php
View
@@ -33,6 +33,7 @@ The Transvision team uses Git and GitHub for both development and issue tracking
* Composer (Dependency Manager for PHP, https://getcomposer.org/)
* mercurial, git, svn to check out data sources (only for a Full installation for production)
* php5-xsl and GraphViz packages for generating the documentation with [phpDocumentor][]
* php5-intl for the transliteration feature
* Apache with mod_rewrite activated
* [npm][] and eslint for JavaScript files (optional)
```
@@ -43,7 +43,7 @@ class API
public $api_versions = ['v1' => 'stable'];
public $services = [
'entity', 'locales', 'repositories', 'search',
'suggestions', 'tm', 'versions',
'suggestions', 'tm', 'transliterate', 'versions',
];
public $error;
public $logging = true;
@@ -292,6 +292,13 @@ private function isValidServiceCall($service)
return false;
}
break;
case 'transliterate':
// ex: /api/v1/transliterate/serbian/Завиритe
if (! $this->verifyEnoughParameters(4)) {
return false;
}
break;
case 'versions':
/*
@@ -306,9 +313,9 @@ private function isValidServiceCall($service)
return true;
}
public function invalidAPICall()
public function invalidAPICall($error = 400)
{
http_response_code(400);
http_response_code($error);
return ['error' => $this->error];
}
View
@@ -50,6 +50,9 @@
case 'tm':
$json = include MODELS . 'api/translation_memory.php';
break;
case 'transliterate':
$json = include MODELS . 'api/transliterate.php';
break;
case 'versions':
$json = include MODELS . 'api/versions.php';
break;
@@ -0,0 +1,27 @@
<?php
namespace Transvision;
// Check that the class exists before trying to use it
// Requires php-intl
if (! class_exists('Transliterator')) {
$request->error = 'Class Transliterator not available';
$json = $request->invalidAPICall(501);
return;
}
switch ($request->parameters[2]) {
case 'sr-Cyril':
$transliterated_locale = 'Serbian-Latin/BGN';
break;
default:
$request->error = 'Wrong language';
return $request->invalidAPICall(501);
}
$transliterator = \Transliterator::create($transliterated_locale);
$transliterated_string = $transliterator->transliterate($request->parameters[3]);
return $json = [$transliterator->transliterate(Utils::secureText($request->parameters[3]))];
View
@@ -21,6 +21,8 @@
['v1/suggestions/central/en-US/fr/bookmark/?max_results=2', 200, '["Bookmark","Bookmarks"]'],
['v1/tm/central/en-US/fr/Bookmark/?max_results=3&min_quality=80', 200, '[{"source":"Bookmark","target":"Marquer cette page","quality":100},{"source":"Bookmark","target":"Marque-page","quality":100},{"source":"Bookmarks","target":"Marque-pages","quality":88.89}]'],
['v1/tm/global/fr/en-US/Ouvrir/', 200, '[{"source":"Ouvrir dans le Finder","target":"Find in Finder","quality":28.57},{"source":"D\u00e9couvrez comment ouvrir une fen\u00eatre de navigation priv\u00e9e","target":"Learn how to open a private window","quality":8.77}]'],
['v1/transliterate/foo/bar/', 501, '{"error":"Wrong language"}'],
['v1/transliterate/sr-Cyril/%0A%D1%81%D1%80%D0%BF%D1%81%D0%BA%D0%B0/', 200, '["srpska"]'],
['v1/versions/', 200, '{"v1":"stable"}'],
];

0 comments on commit 930e502

Please sign in to comment.