This is a tool to translate Emoji Unicode codepoints from one encoding to another.
Emoji can be encoded in several ways:
- Unified
- DoCoMo
- KDDI
- SoftBank
This utility lets you convert from one to the other.
I personally ran into this issue because Facebook uses the Google encoding in their Graph API. To get them to show up properly on OS X/iOS, you need to convert them to unified Unicode.
The methods you'll need for translation are of the form:
Emojivert.FROMENCODING_to_TOENCODING(string)
Conversions are available from the four private use to unified Unicode (and vice versa):
-
docomo_to_unified
andunified_to_docomo
-
kddi_to_unified
andunified_to_kddi
-
softbank_to_unified
andunified_to_softbank
-
google_to_unified
andunified_to_google
In addition there is an "encoding" for HTML. It creates HTML classes for use with CSS/Images (not supplied, see caveat)
-
unified_to_html
andhtml_to_unified
There are also methods for getting the Unicode name from a Unified code point and vice versa.
-
unified_to_name
andname_to_unified
The following shows the basic usage, starting with JSON as an example.
json = '"All work and no play, makes \ud83d\ude0e\ a dull boy \udbba\udf59."'
=> "\"All work and no play, makes \\ud83d\\ude0e\\ a dull boy \\udbba\\udf59.\""
str = ActiveSupport::JSON.decode(json)
=> "All work and no play, makes π a dull boy σΎ."
Emojivert.google_to_unified(str)
=> "All work and no play, makes π a dull boy π€."
Working with Unicode names:
Emojivert.name_to_unified('PILE OF POO')
=> "π©"
Emojivert.unified_to_name("π©")
=> "PILE OF POO"
I used the mappings from the rails-emoji project and I didn't check them. The standards may change (or might have already). I personally have only used the google_to_unified
translation, your mileage may vary on the rest.
There are methods to convert to an HTML representation of emoji for use with CSS and image sprites. I've included the methods, but not the CSS / images necessary to make use of them. Again, check out the rails-emoji project if you to pursue this. There are several other emoji libraries that may be useful for rendering emoji with images, see the list below.
Add this line to your application's Gemfile:
gem 'emojivert'
And then execute:
$ bundle
Or install it yourself as:
$ gem install emojivert
The hard work was already done by Uken and the rails-emoji. I simply converted it from an engine to a gem.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request