Pela Liberdade de Escolha

sexta-feira, 26 de janeiro de 2007

RadRails x UTF-8

Depois de bater a cabeça um pouquinho consegui uma maneira de fazer com que o RadRails se gravasse os arquivos do Rails com utf-8.

Vá em:

Windows -> Prefernces -> General -> Content Types

Na caixa ao lado selecione text -> ruby Source File e abaixo acrescente *.html e *.rhtm.

E configure o Default enconding par : UTF-8 e de um click em update.

Pronto, agora você pode editar suas views usando os acentos normalmente.

Até a próxima.

Marcadores:

Pluralize em Português

Bem pessoal continuando minha jornada de aprendizado com Rails, quero mostrar como fazer o Rails tarbalhar pluralize com nome das tabelas em português.
Com a ajuda deste post do rubyonbr , onde você pode achar o conteúdo das configurações que se segue:

  1. Inflector.inflections do |inflect|
  2. inflect.clear

  3. #general rule: add "s" to the end of the word
  4. #casa - casas
  5. inflect.plural /^([a-zA-z]*)a$/i, '\1as'
  6. #pe - pes
  7. inflect.plural /^([a-zA-z]*)e$/i, '\1es'
  8. #no example
  9. inflect.plural /^([a-zA-z]*)i$/i, '\1is'
  10. #carro - carros
  11. inflect.plural /^([a-zA-z]*)o$/i, '\1os'
  12. #pneu - pneus
  13. inflect.plural /^([a-zA-z]*)u$/i, '\1us'

  14. #if word ends in "r" or "z", add "es"
  15. #luz - luzes
  16. #flor - flores
  17. #arroz - arrozes
  18. inflect.plural /^([a-zA-z]*)r$/i, '\1res'
  19. inflect.plural /^([a-zA-z]*)z$/i, '\1zes'

  20. #if word ends in "al", "el", "ol", "ul": trade "l" with "is"
  21. #farol - farois
  22. #hospital - hospitais
  23. #telemovel - telemoveis
  24. #pincel - pinceis
  25. #anzol - anzois
  26. inflect.plural /^([a-zA-z]*)al$/i, '\1ais'
  27. inflect.plural /^([a-zA-z]*)el$/i, '\1eis'
  28. inflect.plural /^([a-zA-z]*)ol$/i, '\1ois'
  29. inflect.plural /^([a-zA-z]*)ul$/i, '\1uis'

  30. #if word ends in "il" and has tónic accent in last syllable, trade "il" with "is"
  31. #cantil - cantis
  32. inflect.plural /^([a-zA-z]*)il$/i, '\1is'

  33. #TODO
  34. #if word ends in "il" and has tónic accent in penultimate syllable, trade "il" with "eis"
  35. #no example

  36. #if word ends in "m", trade "m" with "ns"
  37. #armazem - armazens
  38. #portagem - portagens
  39. inflect.plural /^([a-zA-z]*)m$/i, '\1ns'

  40. #TODO
  41. #if word ends in "s" and has one silable, trade "s" with "es"
  42. #no example
  43. #inflect.plural /^([a-zA-z]*)e$/i, '\1es'

  44. #TODO
  45. #if word ends in "x" stays the same
  46. #no example... professor X, maybe?
  47. #inflect.plural /^([a-zA-z]*)x$/i, '\1x'

  48. #if word ends in "ão", there are three ways of plural: ãos, ães, ões
  49. #NOTE: hard to detect, so I'll use the most common case
  50. #and then use irregular cases for the others. if someone knows of
  51. #more cases please add to the list & mail me. thanks!
  52. #
  53. #cão - cães
  54. #colchão - colchões
  55. #portão - portões
  56. #pão - pães
  57. #alemão - alemães
  58. #chão - ?
  59. #pilhão - pilhões
  60. #canhão - canhões
  61. #bidão - bidões
  62. #mão - mãos
  63. inflect.plural /^([a-zA-z]*)ao$/i, '\1oes'
  64. inflect.irregular 'cao', 'caes'
  65. inflect.irregular 'pao', 'paes'
  66. inflect.irregular 'mao', 'maos'
  67. inflect.irregular 'alemao', 'alemaes'

  68. ############################
  69. #singularize rules #
  70. ############################

  71. #pes - pe
  72. #carros - carro
  73. #pneus - pneu
  74. inflect.singular /^([a-zA-z]*)as$/i, '\1a'
  75. inflect.singular /^([a-zA-z]*)es$/i, '\1e'
  76. inflect.singular /^([a-zA-z]*)is$/i, '\1i'
  77. inflect.singular /^([a-zA-z]*)os$/i, '\1o'
  78. inflect.singular /^([a-zA-z]*)us$/i, '\1u'

  79. #luzes - luz
  80. #flores - flor
  81. #arrozes - arroz
  82. inflect.singular /^([a-zA-z]*)res$/i, '\1r'
  83. inflect.singular /^([a-zA-z]*)zes$/i, '\1z'

  84. #cantis - cantil
  85. inflect.singular /^([a-zA-z]*)is$/i, '\1il'

  86. #farois - farol
  87. #hospitais - hospital
  88. #telemoveis - telemovel
  89. #pinceis - pincel
  90. #anzois - anzol
  91. inflect.singular /^([a-zA-z]*)ais$/i, '\1al'
  92. inflect.singular /^([a-zA-z]*)eis$/i, '\1el'
  93. inflect.singular /^([a-zA-z]*)ois$/i, '\1ol'
  94. inflect.singular /^([a-zA-z]*)uis$/i, '\1ul'

  95. #armazens - armazem
  96. #portagens - portagem
  97. inflect.singular /^([a-zA-z]*)ns$/i, '\1m'

  98. #cães - cão
  99. #colchões - colchão
  100. #portões - portão
  101. #pães - pão
  102. #alemães - alemão
  103. inflect.singular /^([a-zA-z]*)oes$/i, '\1ao'
  104. inflect.singular /^([a-zA-z]*)aes$/i, '\1ao'
  105. inflect.singular /^([a-zA-z]*)aos$/i, '\1ao'

  106. # inflect.plural /^(ox)$/i, '\1en'
  107. # inflect.singular /^(ox)en/i, '\1'
  108. # inflect.irregular 'person', 'people'
  109. # inflect.uncountable %w( fish sheep )
  110. end

Coloque estas linhas no arquivo environment.rb dentro da pasta config, salve e pronto o rails já está pronto para "falar português".

Obs: Eu tive um problema com acões, ele colocava açães então eu fiz:

inflect.irregular 'acao', 'acoes'

e funcionou beleza
abraços e até a próxima.

Marcadores: