leonel
leonel

Reputation: 10214

Rails 3. Why is Rails removing the decimal points in my edit form?

I changed the locale to spanish (es) and now when I go to an edit form and it contains an amount such as 100.00, it removed the zeroes and converts the amount to 10000 instead. Why is it doing that and how can I fix it?

I think it's related to the time.es.yml file...

es:
  date:
    abbr_day_names:
    - dom
    - lun
    - mar
    - mié
    - jue
    - vie
    - sáb
    abbr_month_names:
    - 
    - ene
    - feb
    - mar
    - abr
    - may
    - jun
    - jul
    - ago
    - sep
    - oct
    - nov
    - dic
    day_names:
    - domingo
    - lunes
    - martes
    - miércoles
    - jueves
    - viernes
    - sábado
    formats:
      default: ! '%d/%m/%Y'
      long: ! '%d de %B de %Y'
      short: ! '%d de %b'
    month_names:
    - 
    - enero
    - febrero
    - marzo
    - abril
    - mayo
    - junio
    - julio
    - agosto
    - septiembre
    - octubre
    - noviembre
    - diciembre
    order:
    - :day
    - :month
    - :year
  datetime:
    distance_in_words:
      about_x_hours:
        one: alrededor de 1 hora
        other: alrededor de %{count} horas
      about_x_months:
        one: alrededor de 1 mes
        other: alrededor de %{count} meses
      about_x_years:
        one: alrededor de 1 año
        other: alrededor de %{count} años
      almost_x_years:
        one: casi 1 año
        other: casi %{count} años
      half_a_minute: medio minuto
      less_than_x_minutes:
        one: menos de 1 minuto
        other: menos de %{count} minutos
      less_than_x_seconds:
        one: menos de 1 segundo
        other: menos de %{count} segundos
      over_x_years:
        one: más de 1 año
        other: más de %{count} años
      x_days:
        one: 1 día
        other: ! '%{count} días'
      x_minutes:
        one: 1 minuto
        other: ! '%{count} minutos'
      x_months:
        one: 1 mes
        other: ! '%{count} meses'
      x_seconds:
        one: 1 segundo
        other: ! '%{count} segundos'
    prompts:
      day: Día
      hour: Hora
      minute: Minutos
      month: Mes
      second: Segundos
      year: Año
  errors: &errors
    format: ! '%{attribute} %{message}'
    messages:
      accepted: debe ser aceptado
      blank: no puede estar en blanco
      confirmation: no coincide con la confirmación
      empty: no puede estar vacío
      equal_to: debe ser igual a %{count}
      even: debe ser par
      exclusion: está reservado
      greater_than: debe ser mayor que %{count}
      greater_than_or_equal_to: debe ser mayor que o igual a %{count}
      inclusion: no está incluido en la lista
      invalid: no es válido
      less_than: debe ser menor que %{count}
      less_than_or_equal_to: debe ser menor que o igual a %{count}
      not_a_number: no es un número
      not_an_integer: debe ser un entero
      odd: debe ser impar
      record_invalid: ! 'La validación falló: %{errors}'
      taken: ya está en uso
      too_long: es demasiado largo (%{count} caracteres máximo)
      too_short: es demasiado corto (%{count} caracteres mínimo)
      wrong_length: no tiene la longitud correcta (%{count} caracteres exactos)
    template:
      body: ! 'Se encontraron problemas con los siguientes campos:'
      header:
        one: No se pudo guardar este/a %{model} porque se encontró 1 error
        other: No se pudo guardar este/a %{model} porque se encontraron %{count} errores
  helpers:
    select:
      prompt: Por favor seleccione
    submit:
      create: Crear %{model}
      submit: Guardar %{model}
      update: Actualizar %{model}
  number:
    currency:
      format:
        precision: 2
    human:
      decimal_units:
        format: ! '%n %u'
        units:
          billion: mil millones
          million: millón
          quadrillion: mil billones
          thousand: mil
          trillion: billón
          unit: ''
  support:
    array:
      last_word_connector: ! ', y '
      two_words_connector: ! ' y '
      words_connector: ! ', '
  time:
    am: am
    formats:
      default: ! '%A, %d de %B de %Y %H:%M:%S %z'
      long: ! '%d de %B de %Y %H:%M'
      short: ! '%d de %b %H:%M'
    pm: pm
  # remove these aliases after 'activemodel' and 'activerecord' namespaces are removed from Rails repository
  activemodel:
    errors:
      <<: *errors
  activerecord:
    errors:
      <<: *errors

Upvotes: 0

Views: 311

Answers (1)

Andrew Gorcester
Andrew Gorcester

Reputation: 19973

In Mexico, the decimal separator is "." so you should be fine. But in Spain, the decimal separator is "," (according to Wikipedia at least -- if I'm reading that file you posted correctly it seems to be " "). Presumably you are set to the Spain locale instead of the Mexican Spanish locale and Rails is stripping the . from your numbers in the assumption that it is a thousands separator.

Can you set the locale to Mexican Spanish somehow? If you can't, or you definitely want the Spain locale, you'll have to edit the locale file to modify the decimal format to match the English format.

Edit: scratch that. Despite what Wikipedia says about decimal separators, this page says that the es-MX locale uses "," as the decimal separator.

I don't think editing your locale file is a very good idea, but if you're sure that's what you want, you should be able to copy and paste the relevant sections in from a working English locale file.

Upvotes: 1

Related Questions