# Iconv and incompatible encodings

**URL:** <https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205>\
**Category:** Ruby\
**Created:** [June 27, 2006, 9:12am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205 "2006-06-27T09:12:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 27, 2006, 9:12am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/1 "2006-06-27T09:12:06Z")

</div>

Hi all,

All this talk of Unicode handling has brought up a problem I run into  
every so often.

Is there any way to use the Iconv library to lossily convert between  
partially incompatible encodings? In other words, if, for example, I’ve  
got a UTF-8 string that I need to convert down to 7-bit ASCII, and I  
don’t especially care what happens to the extended characters (short of  
a single character being mapped to a single character - ideally one I  
can specify), is there any way of forcing the recode?

I realise I could use the failure message to figure out which characters  
in the input string are incompatible and blank them out case-by-case in  
a loop, but that seems awfully wasteful.

Any ideas?

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 27, 2006, 9:52am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/2 "2006-06-27T09:52:59Z")

</div>

On 27/06/06, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way to use the Iconv library to lossily convert between  
> partially incompatible encodings? In other words, if, for example, I’ve  
> got a UTF-8 string that I need to convert down to 7-bit ASCII, and I  
> don’t especially care what happens to the extended characters (short of  
> a single character being mapped to a single character - ideally one I  
> can specify), is there any way of forcing the recode?

Yes, there is. Add //IGNORE to the destination encoding to ignore  
unavailable characters, or //TRANSLIT to transliterate them into  
combinations of ASCII characters (e.g. `e for Ã¨).

E.g.:

#!/usr/bin/env rubby  
$KCODE = ‘u’  
require ‘iconv’

s = ‘caffÃ¨’

ic\_ignore = Iconv.new(‘US-ASCII//IGNORE’, ‘UTF-8’)  
puts ic\_ignore.iconv(s) # =\> caff

ic\_translit = Iconv.new(‘US-ASCII//TRANSLIT’, ‘UTF-8’)  
puts ic\_translit.iconv(s) # =\> caff`e

//TRANSLIT will raise an exception on characters it can’t  
transliterate, however; this can be solved by using  
‘//IGNORE//TRANSLIT’ together (in that order).

Paul.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 27, 2006, 10:08am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/3 "2006-06-27T10:08:31Z")

</div>

Paul B. wrote:

> combinations of ASCII characters (e.g. `e for Ã¨).  
> puts ic\_ignore.iconv(s) # =\> caff  
> Ooh, that’s nice. Thanks for that. I guess it’s wishful thinking to  
> hope for:

puts ic\_ignore.iconv(s) # =\> caffe

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 27, 2006, 11:35am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/4 "2006-06-27T11:35:33Z")

</div>

Paul B. wrote:

> require ‘unicode’  
> or via gems.  
> Hah! That’s fantastic 🙂

I love the smell of Ruby in the morning.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 27, 2006, 10:40am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/5 "2006-06-27T10:40:07Z")

</div>

On 27/06/06, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ooh, that’s nice. Thanks for that. I guess it’s wishful thinking to  
> hope for:
> 
> puts ic\_ignore.iconv(s) # =\> caffe

Your wish is granted:

#!/usr/bin/env rubby  
$KCODE = ‘u’  
require ‘unicode’

s = ‘caffÃ¨’

puts Unicode.normalize\_KD(s).gsub(/[^\x00-\x7F]/n,‘’) # =\> caffe

It works by decomposing characters and rejecting everything above  
position 127, which includes all the now-separated accents.

This does require the slightly-flaky unicode library (work is underway  
to update it). You can get that from [http://www.yoshidam.net/Ruby.html](http://www.yoshidam.net/Ruby.html)  
or via gems.

Paul.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 10, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/6 "2006-07-10T15:31:33Z")

</div>

Paul B. wrote:

> s = ‘caffÃ¨’
> 
> ic\_ignore = Iconv.new(‘US-ASCII//IGNORE’, ‘UTF-8’)  
> puts ic\_ignore.iconv(s) # =\> caff
> 
> ic\_translit = Iconv.new(‘US-ASCII//TRANSLIT’, ‘UTF-8’)  
> puts ic\_translit.iconv(s) # =\> caff`e
> 
> //TRANSLIT will raise an exception on characters it can’t  
> transliterate, however; this can be solved by using  
> ‘//IGNORE//TRANSLIT’ together (in that order).

Can anyone else get this to work? Instead of “caff`e” I just get “caff?”

Daniel

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 10, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/7 "2006-07-10T16:45:52Z")

</div>

Paul B. wrote:

> On 10/07/06, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Can anyone else get this to work? Instead of “caff`e” I just get “caff?”
> 
> What’s your platform?

ubuntu breezy with ruby 1.8.4  
iconv 2.3.5

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 10, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/8 "2006-07-10T15:54:30Z")

</div>

On 10/07/06, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone else get this to work? Instead of “caff`e” I just get “caff?”

What’s your platform?

Paul.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 13, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/9 "2008-10-13T19:58:02Z")

</div>

On Oct 13, 2008, at 12:48 PM, Davi Barbosa wrote:

> I already checked with str.each\_byte {|x| puts x} and the strings are  
> exactly the same. Does anyone have any idea why I get two different  
> answers from Iconv?
> 
> My system:  
> $ irb --version  
> irb 0.9.5(05/04/13)  
> $ ruby --version  
> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
> 
> I have ENV[‘LANG’]==en\_US.UTF-8 in both cases.

Try adding the -KU switch to Ruby, to put it in UTF-8 mode.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 13, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/10 "2008-10-13T20:47:38Z")

</div>

James G. wrote:

> Try adding the -KU switch to Ruby, to put it in UTF-8 mode.

Thank you for your really fast answer. I tried this:  
$ ruby -KU -e “require ‘iconv’; puts  
Iconv.conv(‘US-ASCII//TRANSLIT’,‘UTF-8’,‘Ã©Ã¨ÃªÃ«’)”  
???

$ ruby -e “$KCODE=‘u’; require ‘iconv’; puts  
Iconv.conv(‘US-ASCII//TRANSLIT’,‘UTF-8’,‘Ã©Ã¨ÃªÃ«’)”  
???

and for information, I have also also:  
$ echo ‘Ã©Ã¨ÃªÃ«’ | iconv -t ASCII//TRANSLIT -f UTF-8  
eeee

$ iconv --version  
iconv (GNU libc) 2.7

irb(main):002:0\> ‘Ã©’.each\_byte {|x| puts x}  
195  
169  
=\> “\303\251”

$ ruby -e “‘Ã©’.each\_byte {|x| puts x}”  
195  
169

and finally, the most weird, irb doesn’t work if I use pipe:  
$ echo “require ‘iconv’; puts  
Iconv.conv(‘US-ASCII//TRANSLIT’,‘UTF-8’,‘Ã©’); ‘Ã©’.each\_byte{|x| puts x}”  
| irb  
require ‘iconv’; puts Iconv.conv(‘US-ASCII//TRANSLIT’,‘UTF-8’,‘Ã©’);  
‘Ã©’.each\_byte{|x| puts x}  
?  
195  
169  
“\303\251”

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [November 3, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/11 "2008-11-03T20:05:50Z")

</div>

Hello,  
I was going crazy with this problem. I searched a lot and found some  
people with the same problem: Iconv works with irb but not in a ruby  
script.  
The solution was take another way. For example, Daniel L.  
([http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/306663](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/306663))  
made 3 suggestions. The first one is use Ruby-GNOME2 library and:

require ‘gtk2’  
ascii = GLib.convert(string, “ASCII//translit”, “UTF-8”)

This not only worked for me, as the Iconv started to work as expected!  
For instance:  
require ‘iconv’  
require ‘gtk2’  
puts Iconv.conv(“ASCII//translit”,“UTF-8”,“Ã¡Ã Ã¢Ã¤”)  
gives ‘aaaa’.

The second solution:  
ascii = %x{echo “#{str}” | iconv -f “ISO-8859-1” -t  
“US-ASCII//TRANSLIT”}  
also worked here.

The problem is that I’m not using a ruby script, I’m making an web page  
with mod\_ruby. So, %x{} gives an ‘Insecure operation’ error and “require  
‘gtk2’” gives:  
/var/www/dev/q/test.rbx:12: Cannot open display:  
/usr/lib/ruby/1.8/gtk2.rb:12  
./lib.rb:31:in `require’

His last suggestion is to write your own wrapper. Of course I’ve not  
tried. Finally, I used the hack:  
Unicode.normalize\_KD(string).gsub(/[^\x00-\x7F]/n,‘’)  
as described here: [Iconv and incompatible encodings - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/70827), and this looks  
to work fine to remove accents (but I’m not sure if the result is an  
ascii string)

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [November 4, 2008, 10:50am UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/12 "2008-11-04T10:50:43Z")

</div>

Hi,

At Tue, 4 Nov 2008 04:04:23 +0900,  
Davi Barbosa wrote in [ruby-talk:319309]:

> require ‘gtk2’  
> ascii = GLib.convert(string, “ASCII//translit”, “UTF-8”)
> 
> This not only worked for me, as the Iconv started to work as expected!  
> For instance:  
> require ‘iconv’  
> require ‘gtk2’  
> puts Iconv.conv(“ASCII//translit”,“UTF-8”,“áàâä”)  
> gives ‘aaaa’.

GNU libiconv seems to need the locale set.  
The issue would be fixed by the following patch.

# Index: configure.in

— configure.in (revision 20103)  
+++ configure.in (working copy)  
@@ -559,5 +559,5 @@ AC\_CHECK\_HEADERS(stdlib.h string.h unist  
syscall.h pwd.h grp.h a.out.h utime.h memory.h direct.h  
sys/resource.h   
sys/mkdev.h sys/utime.h netinet/in\_systm.h float.h ieeefp.h  
pthread.h \

- 

```
ucontext.h intrinsics.h)

```

- 

```
ucontext.h intrinsics.h locale.h)

```

# dnl Check additional types. Index: main.c

— main.c (revision 20103)  
+++ main.c (working copy)  
@@ -12,4 +12,7 @@

#include “ruby.h”  
+#ifdef HAVE\_LOCALE\_H  
+#include \<locale.h\>  
+#endif

#ifdef **human68k**  
@@ -35,4 +38,7 @@ main(argc, argv)  
char \*\*argv;  
{  
+#ifdef HAVE\_LOCALE\_H

- setlocale(LC\_CTYPE, “”);  
+#endif  
#ifdef \_WIN32  
NtInitialize(&argc, &argv);

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 3, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/13 "2009-06-03T17:28:22Z")

</div>

As this patch is not going to be accepted which is fair:  
[http://redmine.ruby-lang.org/issues/show/1528](http://redmine.ruby-lang.org/issues/show/1528)

Alternative and light solution is to create own extension as below:

- locale.c -------------------------------------------

#include \<locale.h\>  
#include \<ruby.h\>

#ifndef RSTRING\_PTR  
#define RSTRING\_PTR(str) RSTRING(str)-\>ptr  
#endif

VALUE Locale = Qnil;

VALUE method\_setlocale(VALUE self, VALUE category, VALUE locale);

void Init\_locale() {  
Locale = rb\_define\_module(“Locale”);  
rb\_define\_module\_function(Locale, “setlocale”, method\_setlocale, 2);  
rb\_define\_const(Locale, “LC\_CTYPE”, INT2NUM(0));  
rb\_define\_const(Locale, “LC\_NUMERIC”, INT2NUM(1));  
rb\_define\_const(Locale, “LC\_TIME”, INT2NUM(2));  
rb\_define\_const(Locale, “LC\_COLLATE”, INT2NUM(3));  
rb\_define\_const(Locale, “LC\_MONETARY”, INT2NUM(4));  
rb\_define\_const(Locale, “LC\_MESSAGES”, INT2NUM(5));  
rb\_define\_const(Locale, “LC\_ALL”, INT2NUM(6));  
}

VALUE method\_setlocale(VALUE self, VALUE category, VALUE locale) {  
int c = NUM2INT(category);  
char \*r;  
if(locale == Qnil) {  
r = setlocale(c, NULL);  
} else {  
Check\_Type(locale, T\_STRING);  
r = setlocale(c, RSTRING\_PTR(locale));  
}  
return r == NULL ? Qnil : rb\_str\_new2(r);  
}

- extconf.rb -------------------------------------------

require ‘mkmf’  
extension\_name = ‘locale’  
dir\_config(extension\_name)  
create\_makefile(extension\_name)

… and use:  
require ‘locale’  
Locale::setlocale Locale::LC\_CTYPE, ‘’

This is what I do in one of my projects and it works fine with Iconv,  
note I use LC\_CTYPE not LC\_ALL to not affect numbers or dates  
formatting.

## Regards,

Adam S. | [nanoant.com](http://nanoant.com)

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 6, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/14 "2009-06-06T16:24:24Z")

</div>

Hi,

At Thu, 4 Jun 2009 00:28:25 +0900,  
Adam S. wrote in [ruby-talk:338275]:

> Alternative and light solution is to create own extension as below:

First of all, option after // is GNU iconv local extension.

Second, extconf.rb must check for the necessary header, and  
whether each categories are defined.

# extconf.rb

require ‘mkmf’  
extension\_name = ‘locale’  
header = “locale.h”  
dir\_config(extension\_name)  
if have\_header(header)  
lc = %w[CTYPE NUMERIC TIME COLLATE MONETARY MESSAGES ALL]  
lc = lc.delete\_if {|n| !have\_macro(“LC\_#{n}”, header)}.  
collect {|n| “def(#{n.downcase}, LC\_#{n})”}.  
join(’ ')  
$defs \<\< “-Dforeach\_categories(def)=”#{lc}""  
create\_header  
create\_makefile(extension\_name)  
end

Next, StringValueCStr() is much better than Check\_Type().

> … and use:  
> require ‘locale’  
> Locale::setlocale Locale::LC\_CTYPE, ‘’

And it feels too redundant. I guess Locale.ctype = ‘’ would be  
easy.

/\* locale.c \*/  
#include \<locale.h\>  
#include “ruby.h”

static VALUE  
rb\_setlocale(int category, VALUE locale)  
{  
char \*r = setlocale(category, StringValueCStr(locale));  
return r ? rb\_str\_new2® : Qnil;  
}

static VALUE  
rb\_getlocale(int category)  
{  
char \*r = setlocale(category, NULL);  
return r ? rb\_str\_new2® : Qnil;  
}

#define funcs(n, c)   
static VALUE rb\_getlocale\_##n(VALUE self) {return rb\_getlocale©;}   
static VALUE rb\_setlocale\_##n(VALUE self, VALUE val) {return  
rb\_setlocale(c, val);}   
/\* end of funcs \*/

foreach\_categories(funcs)

void  
Init\_locale(void)  
{  
VALUE locale = rb\_define\_module(“Locale”);  
#define methods(n, c)   
rb\_define\_singleton\_method(locale, #n, rb\_getlocale\_##n, 0);   
rb\_define\_singleton\_method(locale, #n"=", rb\_setlocale\_##n, 1);   
/\* end of methods \*/

```
foreach_categories(methods);

```

}

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 6, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/15 "2009-06-06T17:24:51Z")

</div>

Nobuyoshi,

Thanks, your solution is really more Ruby-way. I just wonder why  
“setlocale” isn’t a part of Ruby standard library. Since Ruby maps/wraps  
most of the standard (POSIX) functions (especially those available on  
Windows too), this one should be also taken into consideration.

> First of all, option after // is GNU iconv local extension.  
> Sure I know that, but it doesn’t mean it is EVIL, is it? Still it is  
> very useful for creating permalinks and removing accented characters  
> simply, w/o using any third party libraries and so, but unusable until  
> we call POSIX setlocale, which isn’t present in Ruby API.

> Second, extconf.rb must check for the necessary header, and  
> whether each categories are defined.

Still it should be present on every system (AFAIK it is), since quoting  
man: “The setlocale() function conforms to ISO/IEC 9899:1999 (``ISO  
C99’’).”. Is there anyone who checks whether \<stdio.h\> exists?

> And it feels too redundant. I guess Locale.ctype = ‘’ would be easy.

Sure yours is better. Mine didn’t consider fact that some of constants  
may have different values on different systems.

If it is was to be included into standard library I’d leave  
Locale::setlocale method as well, as you may combine types there and  
also check returned value, where nil means failed association and String  
on successful one, where documentation doesn’t explicitly say that  
returned string is exactly the one that was passed. So with simple  
Locale::ctype= we may miss some important feedback.

Cheers,  
Adam.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 6, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/16 "2009-06-06T19:53:16Z")

</div>

Nobuyoshi N. wrote:

> It affects library functions, such as printf(), and can cause  
> problems.

Of course, but FileUtils.rm\_rf ‘/’ can do harm as well, but it is still  
included in Ruby. So I don’t really get why I can’t hook into setlocale  
with Ruby? This function is present at every recent OS, and accessible  
for every C, C++, Perl or Python programmer. Sure I know it does change  
way printf works and so on, but Ruby is for sober developers, isn’t it?  
If setlocale causes trouble for someone, my answer is “don’t use it”,  
but not “prohibit it”.

> > we call POSIX setlocale, which isn’t present in Ruby API.
> 
> You can’t rely on it if you want write portable script.

I don’t care. ascii//translit//IGNORE is present at Linux and Mac OSX,  
and maybe many others, it is enough for me. Kernel#fork AFAIK doesn’t  
work on Windows, but would you tell me not to use it because my script  
won’t be portable?

> Not all systems conform to C99, and not all categories are  
> available on all systems. For instance, mingw and perhaps  
> mswin don’t have LC\_MESSAGES.

Right. It is just checking for \<locale.h\> existence is a bit paranoiac  
for me :), but it’s just my point of view.

> > Sure yours is better. Mine didn’t consider fact that some of constants  
> > may have different values on different systems.
> 
> Of course, otherwise why they need the macros?  
> 🙂

> You mean setters should raise an exception on error?

IMHO yes they should if setlocale returns NULL.

Anyway it is pretty too much for someone who wants just call:

setlocale LC\_CTYPE, ‘’

But if we want to get a real interface for setlocale, yours is the  
perfect one to have it included in official stdlib.

Cheers,  
Adam.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [June 6, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/17 "2009-06-06T19:29:02Z")

</div>

Hi,

At Sun, 7 Jun 2009 00:24:55 +0900,  
Adam S. wrote in [ruby-talk:338586]:

> Thanks, your solution is really more Ruby-way. I just wonder why  
> “setlocale” isn’t a part of Ruby standard library. Since Ruby maps/wraps  
> most of the standard (POSIX) functions (especially those available on  
> Windows too), this one should be also taken into consideration.

It affects library functions, such as printf(), and can cause  
problems.

> > First of all, option after // is GNU iconv local extension.  
> > Sure I know that, but it doesn’t mean it is EVIL, is it? Still it is  
> > very useful for creating permalinks and removing accented characters  
> > simply, w/o using any third party libraries and so, but unusable until  
> > we call POSIX setlocale, which isn’t present in Ruby API.

You can’t rely on it if you want write portable script.

> > Second, extconf.rb must check for the necessary header, and  
> > whether each categories are defined.
> 
> Still it should be present on every system (AFAIK it is), since quoting  
> man: “The setlocale() function conforms to ISO/IEC 9899:1999 (``ISO  
> C99’’).”. Is there anyone who checks whether \<stdio.h\> exists?

Not all systems conform to C99, and not all categories are  
available on all systems. For instance, mingw and perhaps  
mswin don’t have LC\_MESSAGES.

> > And it feels too redundant. I guess Locale.ctype = ‘’ would be easy.
> 
> Sure yours is better. Mine didn’t consider fact that some of constants  
> may have different values on different systems.

Of course, otherwise why they need the macros?

> If it is was to be included into standard library I’d leave  
> Locale::setlocale method as well, as you may combine types there and  
> also check returned value, where nil means failed association and String  
> on successful one, where documentation doesn’t explicitly say that  
> returned string is exactly the one that was passed. So with simple  
> Locale::ctype= we may miss some important feedback.

You mean setters should raise an exception on error?

#include \<locale.h\>  
#include “ruby.h”

static VALUE  
rb\_setlocale(int category, const char \*locale)  
{  
char \*r = setlocale(category, locale);  
if (!r) rb\_raise(rb\_eRuntimeError, “setlocale”);  
return rb\_str\_new2®;  
}

static inline VALUE  
locale\_set(int category, VALUE locale)  
{  
return rb\_setlocale(category, StringValueCStr(locale));  
}

static inline VALUE  
locale\_get(int category)  
{  
return rb\_setlocale(category, NULL);  
}

#define funcs(n, c)   
static VALUE rb\_getlocale\_##n(VALUE self) {return locale\_get©;}   
static VALUE rb\_setlocale\_##n(VALUE self, VALUE val) {return  
locale\_set(c, val);}   
/\* end of funcs \*/

foreach\_categories(funcs)

void  
Init\_locale(void)  
{  
VALUE locale = rb\_define\_module(“Locale”);  
#define methods(n, c)   
rb\_define\_singleton\_method(locale, #n, rb\_getlocale\_##n, 0);   
rb\_define\_singleton\_method(locale, #n"=", rb\_setlocale\_##n, 1);   
/\* end of methods \*/

```
foreach_categories(methods);

```

}

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [October 13, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/iconv-and-incompatible-encodings/96205/18 "2008-10-13T19:51:06Z")

</div>

I have also a problem with iconv. I’m under linux (configured with utf-8  
as usual) and under irb I get:  
irb(main):016:0\> Iconv.conv(“US-ASCII//TRANSLIT”,“UTF-8”,‘Ã©Ã¨ÃªÃ«’)  
=\> “eeee”

But when I try the same in ruby or mod\_ruby I get ‘???’, for example:  
$ ruby -e “require ‘iconv’; puts  
Iconv.conv(‘US-ASCII//TRANSLIT’,‘UTF-8’,‘Ã©Ã¨ÃªÃ«’)”  
???  
I already checked with str.each\_byte {|x| puts x} and the strings are  
exactly the same. Does anyone have any idea why I get two different  
answers from Iconv?

My system:  
$ irb --version  
irb 0.9.5(05/04/13)  
$ ruby --version  
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

I have ENV[‘LANG’]==en\_US.UTF-8 in both cases.
