Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retrieve a list of supported languages #211

Merged
merged 3 commits into from
Aug 5, 2024

Conversation

a-drew
Copy link
Contributor

@a-drew a-drew commented Aug 1, 2024

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 static langs 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.

(new GoogleTranslate)->localizedLanguages('en');
(new GoogleTranslate)->languages('en');
GoogleTranslate::langs('en');

// Output
[
    ...
    'en' => English', 
    'es' => 'Spanish', 
    'it' => 'Italian', 
    ...
]

While languages & the static langs 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.

(new GoogleTranslate)->languages();

GoogleTranslate::langs()

// Output
[ 'ab', 'ace', 'ach', 'aa', 'af', 'sq', 'alz', ... ]

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 its DOMDocument and DOMXPath but another option could be the symfony/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

  • full coverage added in tests/SupportedLanguagesTest.php
  • readme contains usage details and example outputs.

@Stichoza
Copy link
Owner

Stichoza commented Aug 2, 2024

Thank you for your work! 🙌🏼 It's a very useful functionality. And thanks for including Georgian language in tests and readme ❤️

Is it possible to avoid @ error silencer for $dom->loadHTML($html);?

@a-drew
Copy link
Contributor Author

a-drew commented Aug 3, 2024

@Stichoza yes we can drop the @.

I've removed the error suppression in my last commit and don't see anything in console. I had it earlier when calling DOMDocument::loadHTML statically, but the static call's been removed in 8.3, so I had to create an instance anyway.

also did a quick fix for the failing exception test. should be all 🟢 once you run the workflow again.

@Stichoza Stichoza merged commit 9429773 into Stichoza:master Aug 5, 2024
8 checks passed
@Stichoza
Copy link
Owner

Stichoza commented Aug 5, 2024

Merged, Thank you!

Also pushed new tag v5.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants