Android Get Country Emoji Flag Using Locale

后端 未结 6 2061
难免孤独
难免孤独 2021-01-04 05:31

I have seen that since Lollipop, Android has built in Emoji flags for different countries. Is it possible to use the devices locale to retrieve the

6条回答
  •  情歌与酒
    2021-01-04 05:44

    When I first wrote this answer I somehow overlooked that I've only worked on Android via React Native!

    Anyway, here's my JavaScript solution that works with or without ES6 support.

        function countryCodeToFlagEmoji(country) {
          return typeof String.fromCodePoint === "function"
            ? String.fromCodePoint(...[...country].map(c => c.charCodeAt() + 0x1f185))
            : [...country]
                .map(c => "\ud83c" + String.fromCharCode(0xdd85 + c.charCodeAt()))
                .join("");
        }
    
    console.log(countryCodeToFlagEmoji("au"));
    console.log(countryCodeToFlagEmoji("aubdusca"));

    If you want to pass in the country codes as capital letters instead, just change the two offsets to 0x1f1a5 and 0xdda5.

提交回复
热议问题