How to translate a string from a keyboard layout to another?

Viewed 276

I sometimes type with the wrong input language; I like to be able to convert the text typed incorrectly into the correct input language programmatically. I currently use this approach:

##
export persian_chars="ضصثقفغعهخحجچشسیبلاتنمکگظطزرذدپو.ًٌٍَُِّْ][}{|ؤئيإأآة»«:؛كٓژٰ‌ٔء<>؟٬٫﷼٪×،)(ـ"
export en_chars="qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?@#\$%^&()_"
##
function per2en() {
    gsed "y/$persian_chars/$en_chars/"
}

function en2per() {
    gsed "y/$en_chars/$persian_chars/"
}

But this seems brittle and specific to my current keyboard layouts. Any better approachs?

PS: Some examples:

❯ echo آهو ئفشزنخرثقبمخص\! | per2en                                                                                       
Hi, Stackoverflow!

❯ echo 'sghl& nkdh' | en2per                                                                                            
سلام، دنیا
0 Answers
Related