cvzi / flag

Flag emoji from country codes for Python πŸ³οΈβ€πŸŒˆπŸŒ
https://pypi.org/project/emoji-country-flag/
MIT License
58 stars 7 forks source link

How to detect invalid country code? #48

Closed lavanya051992 closed 1 year ago

lavanya051992 commented 1 year ago

The flags work fine when I have a valid country code. If I create a dummy country Code which doesn't exist then how do I detect that? If I do flag.flag(countrycode) it gets converted to two letter code irrespective of If it's valid or not. But I don't want to display this two letter code in my UI. Just want to display the emoji if available

cvzi commented 1 year ago

You can check if the code is a valid ISO 3166-1 alpha-2 country code with other packages, for example https://pypi.org/project/pycountry/ can do that.

However I am not aware of a way to check if the emoji is actually available or if the two letter code is displayed.

emojipedia.org has tables of all supported emoji for the major apps/devices. For example: https://emojipedia.org/whatsapp or https://emojipedia.org/microsoft These tables could be parsed to have a list of supported countries for each app/device, but I am not planing to add this to the package.

Another way could be: render the emoji as text and then check the width or inspect the pixel colors to find out whether the emoji is displayed or whether the letters are displayed.

cvzi commented 3 months ago

I have added support for this, because recently the support of flag has been similar across different devices (mainly Android and iPhones now support the same flags). Each supported flag is hardcoded in https://github.com/cvzi/flag/blob/main/flag/data/__init__.py I plan to check the support regularly.

Among other functions, you can now choose to only convert supported flags:

flag.Flag(only_supported=True).flagize("Convert supported flags like :US: but not things like :XX:")
>>> "Convert supported flags like πŸ‡ΊπŸ‡Έ but not things like :XX:"