0

We're using Prawn to generate PDFs, and occasionally we get content in foreign languages like Hebrew that run right-to-left. I can specify text direction in Prawn, but I don't have any way of knowing when the language in question requires it. Is it possible to detect which language text is written in using Ruby, in order to make the appropriate decision about which direction to use?

David Aldridge
  • 51,479
  • 8
  • 68
  • 96
Yarin
  • 173,523
  • 149
  • 402
  • 512

2 Answers2

3

Using the technique in this answer, I was able to test easily for Hebrew and Arabic at least (requires Ruby 1.9+):

if unknown_text.match(/\p{Hebrew}|\p{Arabic}/)
  text_direction = :rtl
end

Another way would be to inspect the Unicode char ranges of the text in question, as described here.

Community
  • 1
  • 1
Yarin
  • 173,523
  • 149
  • 402
  • 512
  • That `\p{...}` trick is quite clever. I would use `match` in preference to `=~` since that operator is a quirky hold-over from Perl that sort of flies in the face of Ruby's generally good readability. Also testing that something is not `nil?` is redundant. Just check if it's defined: `if (text.match(...))` is sufficient. – tadman Jul 09 '14 at 18:14
  • why can't I use p{Korean} ? is that not supported? – amitava mozumder Oct 30 '18 at 11:16
1

I found a gem that might do what your looking for. I have no experience using it myself, so you may need to look into the readme to see how to set it up or use it in code

https://github.com/peterc/whatlanguage/tree/master

Mayshar
  • 190
  • 7
  • Thanks - Unfortunately this project still has a way to go (See https://github.com/peterc/whatlanguage/issues/27). However the developer is responsive and planning improvements, so worthwhile watching. But right now not a solution... – Yarin Jul 09 '14 at 17:07
  • Gotcha, sorry :( I had it on my radar but never had a reason to use it or try it, so wasnt sure how well it worked. – Mayshar Jul 09 '14 at 18:42