In message "[ruby-talk] Re: newbie locale/ascii sort question"
on 03/07/01, Bengt Dahlqvist <bengt.dahlqvistling.uu.se> writes:

|Does such utilities exist in the Ruby context?

Not yet. But a small extension like this seems to work.

require "locale"

Locale::setlocale(Locale::LC_ALL, "ja_JP.eucJP")
my_array.sort{|a,b| Locale::strcoll(a,b)}

Just for information. Does anybody want to work further?


---- locale.c
#include "ruby.h"
#include <locale.h>

static VALUE
locale_setlocale(self, category, locale)
VALUE self, category, locale;
char *l;

if (NIL_P(locale)) l = NULL;
else l = StringValuePtr(locale);
l = setlocale(NUM2INT(category), l);
if (!l) return Qnil;
return rb_str_new2(l);

static VALUE
locale_strcoll(self, s1, s2)
VALUE self, s1, s2;
int n = strcoll(StringValuePtr(s1), StringValuePtr(s2));
return INT2NUM(n);

VALUE mLocale = rb_define_module("Locale");
rb_define_module_function(mLocale, "setlocale", locale_setlocale, 2);
rb_define_module_function(mLocale, "strcoll", locale_strcoll, 2);

rb_define_const(mLocale, "LC_ALL", INT2NUM(LC_ALL));
rb_define_const(mLocale, "LC_COLLATE", INT2NUM(LC_COLLATE));
rb_define_const(mLocale, "LC_CTYPE", INT2NUM(LC_CTYPE));
rb_define_const(mLocale, "LC_MESSAGES", INT2NUM(LC_MESSAGES));
rb_define_const(mLocale, "LC_MONETARY", INT2NUM(LC_MONETARY));
rb_define_const(mLocale, "LC_NUMERIC", INT2NUM(LC_NUMERIC));
rb_define_const(mLocale, "LC_TIME", INT2NUM(LC_TIME));