Translates English text to Pig Latin, with proper handling of capitalization, punctuation, and hyphenated phrases.
Igpay Atinlay orfay ouryay AvaScriptjay/YpeScript-tay ojectpray. Atwhay ouldcay ebay etterbay? (Exceptyay aconbay, ofyay oursecay.)
Other Pig-Latin translators exist, but none of them worked as well as I wanted, so I created this one. Highlights:
-
It preserves all punctuation and whitespace perfectly, including tabs and line breaks.
-
It splits input words on whitespace, slashes, underscores, and dashes (not just whitespace), so
perfectly-fried bacon and/or ham is _so_tasty_
translates toerfectlypay-iedfray aconbay andyay/oryay amhay isyay _osay_astytay_
. -
It handles capitalization and all-caps nicely, so
Bacon is AWESOME!
translates toAconbay isyay AWESOMEYAY!
. -
It knows that
Y
is a consonant at the beginning of a word, and a vowel otherwise, soyellow
becomesellowyay
andbytes
becomesytesbay
. -
It hyphenates when needed to prevent hard-to-read repeated consonants from appearing at the end of translated words, so
sheesh
andpeep
becomeeesh-shay
andeep-pay
. -
It removes apostrophes in possessives and contractions to avoid mangling them, while still preserving leading and trailing apostrophes, e.g.
A pig's ears aren't tasty, don't eat 'em
translates toAyay igspay earsyay arentyay astytay, ontday eatyay 'emyay
- no awkwardig'spay
,aren'tyay
, oron'tday
. -
It doesn't mangle words for which there is no rational Pig Latin translation, such as
HTML
,Odd13
, andv1.0
- they're passed though as-is. -
It allows words to be added to an exclusions list; excluded words will be passed through without translation.
-
It has no dependencies, so it won't bloat up your bundle.
-
It comes with TypeScript bindings.
import PigLatin from "pig-latinizer"
const pigLatin: PigLatin = new PigLatin()
pigLatin.exclusions.push("woot")
const translated: string = pigLatin.translate("Bacon, woot!") // Aconbay, woot!
const PigLatin = require("pig-latinizer").default
const pigLatin = new PigLatin()
pigLatin.exclusions.push("woot")
const translated = pigLatin.translate("Bacon, woot!") // Aconbay, woot!