|
| 1 | +The Emoji Component |
| 2 | +=================== |
| 3 | + |
| 4 | + The Emoji component provides utilities to work with emoji characters and |
| 5 | + sequences from the `Unicode CLDR dataset`_. |
| 6 | + |
| 7 | +Installation |
| 8 | +------------ |
| 9 | + |
| 10 | +.. code-block:: terminal |
| 11 | +
|
| 12 | + $ composer require symfony/emoji |
| 13 | +
|
| 14 | +.. include:: /components/require_autoload.rst.inc |
| 15 | + |
| 16 | + |
| 17 | +Emoji Transliteration |
| 18 | +--------------------- |
| 19 | + |
| 20 | +The ``EmojiTransliterator`` class offers a way to translate emojis into their |
| 21 | +textual representation in all languages based on the `Unicode CLDR dataset`_:: |
| 22 | + |
| 23 | + use Symfony\Component\Emoji\EmojiTransliterator; |
| 24 | + |
| 25 | + // Describe emojis in English |
| 26 | + $transliterator = EmojiTransliterator::create('en'); |
| 27 | + $transliterator->transliterate('Menus with 🍕 or 🍝'); |
| 28 | + // => 'Menus with pizza or spaghetti' |
| 29 | + |
| 30 | + // Describe emojis in Ukrainian |
| 31 | + $transliterator = EmojiTransliterator::create('uk'); |
| 32 | + $transliterator->transliterate('Menus with 🍕 or 🍝'); |
| 33 | + // => 'Menus with піца or спагеті' |
| 34 | + |
| 35 | + |
| 36 | +The ``EmojiTransliterator`` also provides special locales that convert emojis to |
| 37 | +short codes and vice versa in specific platforms, such as GitHub and Slack. |
| 38 | + |
| 39 | +GitHub |
| 40 | +~~~~~~ |
| 41 | + |
| 42 | +Convert GitHub emojis to short codes with the ``emoji-github`` locale:: |
| 43 | + |
| 44 | + $transliterator = EmojiTransliterator::create('emoji-github'); |
| 45 | + $transliterator->transliterate('Teenage 🐢 really love 🍕'); |
| 46 | + // => 'Teenage :turtle: really love :pizza:' |
| 47 | + |
| 48 | +Convert GitHub short codes to emojis with the ``github-emoji`` locale:: |
| 49 | + |
| 50 | + $transliterator = EmojiTransliterator::create('github-emoji'); |
| 51 | + $transliterator->transliterate('Teenage :turtle: really love :pizza:'); |
| 52 | + // => 'Teenage 🐢 really love 🍕' |
| 53 | + |
| 54 | +Slack |
| 55 | +~~~~~ |
| 56 | + |
| 57 | +Convert Slack emojis to short codes with the ``emoji-slack`` locale:: |
| 58 | + |
| 59 | + $transliterator = EmojiTransliterator::create('emoji-slack'); |
| 60 | + $transliterator->transliterate('Menus with 🥗 or 🧆'); |
| 61 | + // => 'Menus with :green_salad: or :falafel:' |
| 62 | + |
| 63 | +Convert Slack short codes to emojis with the ``slack-emoji`` locale:: |
| 64 | + |
| 65 | + $transliterator = EmojiTransliterator::create('slack-emoji'); |
| 66 | + $transliterator->transliterate('Menus with :green_salad: or :falafel:'); |
| 67 | + // => 'Menus with 🥗 or 🧆' |
| 68 | + |
| 69 | + |
| 70 | +Emoji Slugger |
| 71 | +------------- |
| 72 | + |
| 73 | +Combine the emoji transliterator with the :doc:`/components/string` |
| 74 | +to improve the slugs of contents that include emojis (e.g. for URLs). |
| 75 | + |
| 76 | +Call the ``AsciiSlugger::withEmoji()`` method to enable the emoji transliterator in the Slugger:: |
| 77 | + |
| 78 | + use Symfony\Component\String\Slugger\AsciiSlugger; |
| 79 | + |
| 80 | + $slugger = new AsciiSlugger(); |
| 81 | + $slugger = $slugger->withEmoji(); |
| 82 | + |
| 83 | + $slug = $slugger->slug('a 😺, 🐈⬛, and a 🦁 go to 🏞️', '-', 'en'); |
| 84 | + // $slug = 'a-grinning-cat-black-cat-and-a-lion-go-to-national-park'; |
| 85 | + |
| 86 | + $slug = $slugger->slug('un 😺, 🐈⬛, et un 🦁 vont au 🏞️', '-', 'fr'); |
| 87 | + // $slug = 'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national'; |
| 88 | + |
| 89 | +.. tip:: |
| 90 | + |
| 91 | + Integrating the Emoji Component with the String component is straightforward and requires no additional |
| 92 | + configuration.string. |
| 93 | + |
| 94 | +Removing Emojis |
| 95 | +--------------- |
| 96 | + |
| 97 | +The ``EmojiTransliterator`` can also be used to remove all emojis from a string, via the |
| 98 | +special ``strip`` locale:: |
| 99 | + |
| 100 | + use Symfony\Component\Emoji\EmojiTransliterator; |
| 101 | + |
| 102 | + $transliterator = EmojiTransliterator::create('strip'); |
| 103 | + $transliterator->transliterate('🎉Hey!🥳 🎁Happy Birthday!🎁'); |
| 104 | + // => 'Hey! Happy Birthday!' |
| 105 | + |
| 106 | +Disk space |
| 107 | +---------- |
| 108 | + |
| 109 | +The data needed to store the transliteration of all emojis (~5,000) into all |
| 110 | +languages take a considerable disk space. |
| 111 | + |
| 112 | +If you need to save disk space (e.g. because you deploy to some service with tight |
| 113 | +size constraints), run this command (e.g. as an automated script after ``composer install``) |
| 114 | +to compress the internal Symfony emoji data files using the PHP ``zlib`` extension: |
| 115 | + |
| 116 | +.. code-block:: terminal |
| 117 | +
|
| 118 | + # adjust the path to the 'compress' binary based on your application installation |
| 119 | + $ php ./vendor/symfony/emoji/Resources/bin/compress |
| 120 | +
|
| 121 | +
|
| 122 | +.. _`Unicode CLDR dataset`: https://github.com/unicode-org/cldr |
0 commit comments