|
5 | 5 | use GuzzleHttp\Client;
|
6 | 6 | use GuzzleHttp\Exception\GuzzleException;
|
7 | 7 | use JsonException;
|
| 8 | +use Stichoza\GoogleTranslate\Exceptions\LanguagesRequestException; |
8 | 9 | use Stichoza\GoogleTranslate\Exceptions\LargeTextException;
|
9 | 10 | use Stichoza\GoogleTranslate\Exceptions\RateLimitException;
|
10 | 11 | use Stichoza\GoogleTranslate\Exceptions\TranslationDecodingException;
|
@@ -366,7 +367,7 @@ protected function injectParameters(string $string, array $replacements): string
|
366 | 367 | {
|
367 | 368 | // Remove space added by google in the parameters
|
368 | 369 | $string = preg_replace('/#\{\s*(\d+)\s*\}/', '#{$1}', $string);
|
369 |
| - |
| 370 | + |
370 | 371 | return preg_replace_callback(
|
371 | 372 | '/\#{(\d+)}/',
|
372 | 373 | fn($matches) => $replacements[$matches[1]],
|
@@ -456,4 +457,81 @@ protected function isValidLocale(string $lang): bool
|
456 | 457 | {
|
457 | 458 | return (bool) preg_match('/^([a-z]{2,3})(-[A-Za-z]{2,4})?$/', $lang);
|
458 | 459 | }
|
| 460 | + |
| 461 | + /** |
| 462 | + * Fetch the list of supported languages from Google Translate |
| 463 | + * |
| 464 | + * @param string|null $target iso code of display language, when null returns only iso codes |
| 465 | + * @return string[]|array<string, string> |
| 466 | + * @throws RateLimitException |
| 467 | + * @throws LanguagesRequestException |
| 468 | + */ |
| 469 | + public static function langs(?string $target = null): array |
| 470 | + { |
| 471 | + return (new self)->languages($target); |
| 472 | + } |
| 473 | + |
| 474 | + /** |
| 475 | + * Fetch the list of supported languages from Google Translate |
| 476 | + * |
| 477 | + * @param string|null $target iso code of display language, when null returns only iso codes |
| 478 | + * @return string[]|array<string, string> |
| 479 | + * @throws RateLimitException |
| 480 | + * @throws LanguagesRequestException |
| 481 | + */ |
| 482 | + public function languages(?string $target = null): array |
| 483 | + { |
| 484 | + $languages = $this->localizedLanguages($target ?? $this->target ?? $this->source ?? ''); |
| 485 | + |
| 486 | + if ($target === null) { |
| 487 | + return array_keys($languages); |
| 488 | + } |
| 489 | + |
| 490 | + return $languages; |
| 491 | + } |
| 492 | + |
| 493 | + /** |
| 494 | + * Fetch the list of supported languages from Google Translate |
| 495 | + * |
| 496 | + * @param string $target iso code of localized display language |
| 497 | + * @return array<string, string> |
| 498 | + * @throws RateLimitException |
| 499 | + * @throws LanguagesRequestException |
| 500 | + */ |
| 501 | + public function localizedLanguages(string $target): array |
| 502 | + { |
| 503 | + $menu = 'sl'; // 'tl'; |
| 504 | + $url = parse_url($this->url); |
| 505 | + $url = $url['scheme'].'://'.$url['host']."/m?mui=$menu&hl=$target"; |
| 506 | + |
| 507 | + try { |
| 508 | + $response = $this->client->get($url, $this->options); |
| 509 | + } catch (GuzzleException $e) { |
| 510 | + match ($e->getCode()) { |
| 511 | + 429, 503 => throw new RateLimitException($e->getMessage(), $e->getCode()), |
| 512 | + default => throw new LanguagesRequestException($e->getMessage(), $e->getCode()), |
| 513 | + }; |
| 514 | + } catch (Throwable $e) { |
| 515 | + throw new LanguagesRequestException($e->getMessage(), $e->getCode()); |
| 516 | + } |
| 517 | + |
| 518 | + // add a meta tag to ensure the HTML content is treated as UTF-8, fixes xpath node values |
| 519 | + $html = preg_replace('/<head>/i', '<head><meta charset="UTF-8">', $response->getBody()->getContents()); |
| 520 | + |
| 521 | + // Prepare to crawl DOM |
| 522 | + $dom = new \DOMDocument(); |
| 523 | + $dom->loadHTML($html); |
| 524 | + $xpath = new \DOMXPath($dom); |
| 525 | + |
| 526 | + $nodes = $xpath->query('//div[@class="language-item"]/a'); |
| 527 | + |
| 528 | + $languages = []; |
| 529 | + foreach ($nodes as $node) { |
| 530 | + $href = $node->getAttribute('href'); |
| 531 | + $code = strtok(substr($href, strpos($href, "$menu=") + strlen("$menu=")), '&'); |
| 532 | + $languages[$code] = $node->nodeValue; |
| 533 | + } |
| 534 | + |
| 535 | + return $languages; |
| 536 | + } |
459 | 537 | }
|
0 commit comments