Pluralize and divide into Spanish

sorry for my English...

I have a rails application designed for evaporation, so all the content is in Spanish, so I have a search box for searching the mysql database, all the rows are in Spanish, I would like to improve my search to allow users to search for keywords in single or multiple form, for example:

keyword: patatas found: patata keyword: veces found: vez keyword: vez found: veces keyword: actividades found: actividad 

In English, this can be relatively easy using the methods of singularity and multiplicity ...

 where `searching_field` like '%singularized_keyword%' or `searching_field` like '%pluralized_keyword%' 

But for the Spanish ....

Some help?

Thanks!

+4
source share
4 answers

Now you can define your own inflections.

look in config / initializers / inflections.rb

example based on your question

 ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'patata', 'patatas' end 

In this way,

 "patata".pluralize # => "patatas" "patatas".singularize #=> "patata" 

Of course, you need to know the keyword list in advance in order to use the irregular method in config / inflections.rb. Take a look at the examples in this example in this file. There are other methods that allow you to define rules using regular expressions, and you can create match patterns to influence inflections for arbitrary keywords that match known patterns.

+7
source

You need to clear all the default drag and drop in English and create new ones in Spanish.

Add to config / initializers / inflections.rb

 ActiveSupport::Inflector.inflections do |inflect| inflect.clear :all inflect.plural /([^djlnrs])([AZ]|_|$)/, '\1s\2' inflect.plural /([djlnrs])([AZ]|_|$)/, '\1es\2' inflect.plural /(.*)z([AZ]|_|$)$/i, '\1ces\2' inflect.singular /([^djlnrs])s([AZ]|_|$)/, '\1\2' inflect.singular /([djlnrs])es([AZ]|_|$)/, '\1\2' inflect.singular /(.*)ces([AZ]|_|$)$/i, '\1z\2' end 
+5
source

Now it seems that now you can use localized kinks:

 # config/initializers/inflections.rb ActiveSupport::Inflector.inflections(:es) do |inflect| inflect.plural /([^djlnrs])([AZ]|_|$)/, '\1s\2' inflect.plural /([djlnrs])([AZ]|_|$)/, '\1es\2' inflect.plural /(.*)z([AZ]|_|$)$/i, '\1ces\2' inflect.singular /([^djlnrs])s([AZ]|_|$)/, '\1\2' inflect.singular /([djlnrs])es([AZ]|_|$)/, '\1\2' inflect.singular /(.*)ces([AZ]|_|$)$/i, '\1z\2' end 

With this (and after restarting the server) you can use:

 "trebol".pluralize(:es) #=> "treboles" 
+3
source

Source: https://habr.com/ru/post/1334406/


All Articles