Localized convert_misc_characters ? #42

Closed
mattiassvedhem opened this Issue Feb 17, 2012 · 9 comments

Projects

None yet

4 participants

@mattiassvedhem

It would be great if we could localize the replacements in convert_misc_characters.
Is this something that's seems reasonable ?

Thanks

@rsl
Owner

got idea for implementation? i'm not against it but sounds like it would be more complexity and harder to explain how to do.

@lassebunk

+1 to this – that would be really great :)

My solution suggestion is this:

Inside the convert_misc_characters method:

if localized = options[:localized]
  misc_characters = Hash[misc_characters.map { |k, v| [k, (localized[v] || v)] }]
end

You can now call it like this:

danish_conversions = {
  'and' => 'og',
  'number' => 'nummer',
  'at' => 'snabel-a',
  'star' => 'stjerne',
  'percent' => 'procent'
}

"me & my".convert_misc_characters(:localized => danish_conversions)

=> "me og my"

Could that be a solution?

/Lasse

PS. The full method with the above modifications goes here:

def convert_misc_characters(options = {})
  dummy = dup.gsub(/\.{3,}/, " dot dot dot ") # Catch ellipses before single dot rule!
  # Special rules for money
  {
    /(\s|^)\$(\d+)\.(\d+)(\s|$)/ => '\2 dollars \3 cents',
    /(\s|^)£(\d+)\.(\d+)(\s|$)/u => '\2 pounds \3 pence',
  }.each do |found, replaced|
    replaced = " #{replaced} " unless replaced =~ /\\1/
    dummy.gsub!(found, replaced)
  end
  # Back to normal rules
  misc_characters =
  {
    /\s*&\s*/ => "and",
    /\s*#/ => "number",
    /\s*@\s*/ => "at",
    /(\S|^)\.(\S)/ => '\1 dot \2',
    /(\s|^)\$(\d*)(\s|$)/ => '\2 dollars',
    /(\s|^)£(\d*)(\s|$)/u => '\2 pounds',
    /(\s|^)¥(\d*)(\s|$)/u => '\2 yen',
    /\s*\*\s*/ => "star",
    /\s*%\s*/ => "percent",
    /(\s*=\s*)/ => " equals ",
    /\s*\+\s*/ => "plus",
    /\s\s*/ => "degrees"
  }
  misc_characters[/\s*(\\|\/|/)\s*/] = 'slash' unless options[:allow_slash]

  if localized = options[:localized]
    misc_characters = Hash[misc_characters.map { |k, v| [k, (localized[v] || v)] }]
  end

  misc_characters.each do |found, replaced|
    replaced = " #{replaced} " unless replaced =~ /\\1/
    dummy.gsub!(found, replaced)
  end
  dummy = dummy.gsub(/(^|[[:alpha:]])'([[:alpha:]]|$)/, '\1\2').gsub(/[\.,:;()\[\]\/\?!\^'ʼ"_]/, " ").strip
end
@rsl
Owner

care to knock up a patch and tests? trying desperately to get to all these tickets i've let pile up. hands full of a ruby 1.8.7 issue with unicode right now.

@rsl
Owner

take a look at #68 as well. in fact, you might want to hold off on a patch until i get an idea implemented for allowing config like this.

@ByScripts

Hi. Any news on this?

I'm working on a french website, and it would be great to be able to translate "dot" to "point" for example :)

Thanks, and keep up the good work 👍

@rsl
Owner

any update on a patch?

@rsl
Owner

note: you need to make sure yr on master when writing a patch on this. thanks. lots of lots of refactors and restructuring.

@lassebunk

I'm looking into it. Thanks 👍

@rsl
Owner

thanks.

@rsl rsl closed this in 5f8221d Jan 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment