{"id":13878999,"url":"https://github.com/shlima/translate_enum","last_synced_at":"2025-04-05T17:06:51.578Z","repository":{"id":55103973,"uuid":"84202369","full_name":"shlima/translate_enum","owner":"shlima","description":" Easily Translate Enums in Rails","archived":false,"fork":false,"pushed_at":"2022-11-13T15:04:55.000Z","size":30,"stargazers_count":121,"open_issues_count":4,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T16:07:00.362Z","etag":null,"topics":["enum","i18n","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shlima.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-07T13:30:14.000Z","updated_at":"2024-10-28T14:52:43.000Z","dependencies_parsed_at":"2023-01-21T15:17:02.188Z","dependency_job_id":null,"html_url":"https://github.com/shlima/translate_enum","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shlima%2Ftranslate_enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shlima%2Ftranslate_enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shlima%2Ftranslate_enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shlima%2Ftranslate_enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shlima","download_url":"https://codeload.github.com/shlima/translate_enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247369952,"owners_count":20927928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","i18n","rails"],"created_at":"2024-08-06T08:02:06.562Z","updated_at":"2025-04-05T17:06:51.557Z","avatar_url":"https://github.com/shlima.png","language":"Ruby","readme":"![CI](https://github.com/shlima/translate_enum/workflows/CI/badge.svg)\n[![Code Climate](https://codeclimate.com/github/shlima/translate_enum/badges/gpa.svg)](https://codeclimate.com/github/shlima/translate_enum)\n[![gem version](https://badge.fury.io/rb/health_bit.svg)](https://rubygems.org/gems/translate_enum)\n\n# TranslateEnum\n\nSimple, zero-dependant `enum` translation gem for Rails\n\n## Installation\n\n`gem install translate_enum`\n\n## Usage\n\nHere is a regular use case. ActiveRecord model:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  include TranslateEnum\n  \n  enum status: { published: 0, archive: 1 }\n  translate_enum :status\nend\n```\n\nLocalization file\n\n```yaml\nen:\n  activerecord:\n    attributes:\n      post:\n        status_list:\n          published: Was published\n          archive: Was archived\n```\n\nOr if you wish your locales to be available across all models\n\n```yaml\nen:\n  attributes:\n    status_list:\n      published: Was published\n      archive: Was archived\n```\n\n```ruby\nPost.translated_status(:published) #=\u003e \"Was published\"\nPost.translated_statuses =\u003e [[\"Was published\", :published, 0], [\"Was archived\", :archive, 1]]\n\n@post = Post.new(status: :published)\n@post.translated_status #=\u003e \"Was published\"\n\n# Each `translated` method supports \n# I18n.translate attributes:\n\nPost.translated_status(:published, raise: true, throw: true, locale: :en, count: 10)\n```\n\n### Use in a Form\n\n```haml\n= form_for @post do |f|\n  = f.select :status, options_for_select(f.object.class.translated_statuses.map { |translation, k, _v| [translation, k] })\n```\n\n## Extending ActiveRecord\n\nBe default you should extend each `ActiveRecord` model manually by including `TranslateEnum` module in it.\nYou can extend `ActiveRecord` by requiring `translate_enum/active_record` in initializer or inside yout `Gemfile`:\n\nGemfile:\n\n```ruby\ngem 'translate_enum', require: 'translate_enum/active_record'\n```\n\nInitializer:\n\n```ruby\n# config/initializers/translate_enum.rb\nrequire 'translate_enum/active_record'\n```\n\n## Advanced options\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  enum gender: [:male, :female]\n  \n  translate_enum :gender do |tr|\n    tr.i18n_scope = 'activerecord.attributes'\n    tr.i18n_key = 'gender_list'\n    tr.enum_klass_method_name = 'genders'\n    tr.enum_instance_method_name = 'gender'\n    tr.method_name_singular = 'translated_gender'\n    tr.method_name_plural = 'translated_genders'\n  end\n  \n  # Or even provide your own logic\n  def self.translated_gender(key)\n    I18n.t(key, scope: 'global.gender_list')\n  end\nend\n```\n# How To?\n## How to use pluralization\n```yaml\nen:\n  activerecord:\n    attributes:\n      person:\n        gender_list:\n          male: \n            zero: No males\n            one: One male\n            other: %{count} males\n            \n```\n\n```ruby\nPerson.translated_gender(:make, count: 0) #=\u003e \"No males\"\nPerson.translated_genders =\u003e [[\"One male\", :male, 0]] # and others\nPerson.translated_genders(count: 0) =\u003e [[\"No males\", :male, 0]] # and others\n\n@person = Person.new(gender: :male)\n@person.translated_gender #=\u003e \"One male\"\n@person.translated_gender(count: 10) #=\u003e \"10 Males\"\n```\n\n## How use translate enum in serializer \n\nExample for Grape:\n\n```ruby\nclass Feedback \u003c ApplicationRecord\n  include TranslateEnum\n  \n  enum topic: {\n    question: 'question', issue: 'issue', complaint: 'complaint', offer: 'offer',\n    investment: 'investment'\n  }\n\n  translate_enum :topic\nend\n```\n\n```ruby\nclass FeedbacksApi \u003c Grape::API\n  resource :feedbacks do\n    get 'enums' do\n      present Feedback.method(:translated_topics), with: TranslateEnumSerializer\n    end\n  end\nend\n```\n\n```ruby\nclass TranslateEnumSerializer \u003c Grape::Entity\n  expose :enum, as: -\u003e(method) { method.name[/translated_(.*)/, 1] } do |method|\n    method.call.map do |translation, key, _value|\n      { value: key, translation: translation }\n    end\n  end\nend\n```\n\n```bash\ncurl http://localhost:3000/feedbacks/enums\n```\n\n```json\n{\n  \"topics\": [\n    {\n      \"value\": \"question\",\n      \"translation\": \"Vopros\"\n    },\n    {\n      \"value\": \"issue\",\n      \"translation\": \"Problema\"\n    },\n    {\n      \"value\": \"complaint\",\n      \"translation\": \"Zhaloba\"\n    },\n    {\n      \"value\": \"offer\",\n      \"translation\": \"Predlozhenie\"\n    },\n    {\n      \"value\": \"investment\",\n      \"translation\": \"Invisticii\"\n    }\n  ]\n}\n```\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshlima%2Ftranslate_enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshlima%2Ftranslate_enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshlima%2Ftranslate_enum/lists"}