feat: retrieve a list of supported languages #211
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds languages methods to let the package user retrieve a complete list of supported languages on Google Translate. It's implemented in the new
languages
,localizedLanguages
and staticlangs
methods, added tests for full coverage and updated the README accordingly.Details
Why
I've noticed that it's common feature of the paid translation apis (google, amazon, deelp, bing/azure) so adding it here for parity seemed like a good idea. I also use this package as one of my api drivers while locally testing my translation tools, so bringing it inline with the other drivers benefits me personally.
Feel free to change or even dismiss this PR if you find it's not needed.
Implementation
There are 2 options for users to retrieve the languages:
localizedLanguages($target)
lets users pull a list iso codes mapped to the name of the language displayed in the$target
language.While
languages
& the staticlangs
still let you pass in a$target
display language for the same result, they also let you skip the display language and return only the list of iso-639 language codes.All of this works by crawling the mobile translate site with the language menu open, https://translate.google.com/m?mui=sl. I've opted to use the built-in php
ext-dom
with itsDOMDocument
andDOMXPath
but another option could be thesymfony/dow-crawler
package.ext-dom
seems like a pretty standard extension that is often bundled with php without the need to install anything with pecl or composer.Testing
tests/SupportedLanguagesTest.php