Monday, December 1, 2008

Show numeric digit in your language

My previous post was about a Ruby on Rails plugin I wrote to convert any numeric digit to other languages' representations. Here is the internals; how that works.

That plugin converts a whole string character by character. To add any language, just add that language's digits to "NUMERIC_LOCALIZE_MAP" hash.


NUMERIC_LOCALIZE_MAP = {
'bn' => ["","","","","","","","","",""],
'hi' => ["","","","","","","","","",""],
'ar' => ["٠", "١","٢","٣","٤","٥","٦","٧","٨","٩"]
}

def localize_numeric(source, locale_code = nil)
string_representation = source
result_string = nil

if locale_code && NUMERIC_LOCALIZE_MAP.include?(locale_code)
result_string = string_representation.gsub(/\d/){|digit|
NUMERIC_LOCALIZE_MAP[locale_code][digit.to_i]
}
else
result_string = string_representation
end
result_string
end

No comments: