All Projects → huacnlee → enumize

huacnlee / enumize

Licence: MIT license
Extend ActiveRecord::Enum for add more helpful methods.

Programming Languages

ruby
36898 projects - #4 most used programming language
HTML
75241 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to enumize

ember-i18n-cp-validations
ember-i18n support for ember-cp-validations
Stars: ✭ 20 (-20%)
Mutual labels:  i18n
rosetta
Easy to use Rust i18n library based on code generation
Stars: ✭ 37 (+48%)
Mutual labels:  i18n
android-studio-plugin
Integrate your Android project with Crowdin
Stars: ✭ 52 (+108%)
Mutual labels:  i18n
i18n-iso-languages
i18n for ISO 639-1 language codes
Stars: ✭ 38 (+52%)
Mutual labels:  i18n
rails
Rails translation made _('simple').
Stars: ✭ 65 (+160%)
Mutual labels:  i18n
cakephp-translate
A CakePHP plugin to manage translations of your static content the easy way via web backend.
Stars: ✭ 18 (-28%)
Mutual labels:  i18n
deepl-php-lib
🧠 DeepL API Client Library supporting PHP >= 7.3
Stars: ✭ 50 (+100%)
Mutual labels:  i18n
vue-filter-pluralize
Simple pluralize filter for Vue.js
Stars: ✭ 13 (-48%)
Mutual labels:  i18n
i18n lazy scope
Use lazy lookup with custom i18n scopes.
Stars: ✭ 11 (-56%)
Mutual labels:  i18n
react-i18next-phraseapp
Library support to use react-i18next with the Phrase In-Context Editor - DEPRECATED
Stars: ✭ 14 (-44%)
Mutual labels:  i18n
Kotsu
✨ Clean, opinionated foundation for new projects — to boldly go where no man has gone before
Stars: ✭ 48 (+92%)
Mutual labels:  i18n
i18n-language.js
i18n-language.js is Simple i18n language with Vanilla Javascript
Stars: ✭ 21 (-16%)
Mutual labels:  i18n
i18n-command
Provides internationalization tools for WordPress projects.
Stars: ✭ 76 (+204%)
Mutual labels:  i18n
express-mvp
Express.js project template ready to go
Stars: ✭ 21 (-16%)
Mutual labels:  i18n
TranslationFiles
[READ-ONLY] This repo contains all the necessary files to generate translation packs for PrestaShop 1.6 and 1.7. It's updated automatically.
Stars: ✭ 16 (-36%)
Mutual labels:  i18n
next.js-boilerplate
next.js bolierplate, next.js 的开发模板
Stars: ✭ 28 (+12%)
Mutual labels:  i18n
figma-static-localizer
A Figma plugin for static localization
Stars: ✭ 30 (+20%)
Mutual labels:  i18n
I18N
I18N Library for .NET, and Delphi
Stars: ✭ 48 (+92%)
Mutual labels:  i18n
unicode-programming
Unicode programming examples
Stars: ✭ 33 (+32%)
Mutual labels:  i18n
bcp-47
Parse and stringify BCP 47 language tags
Stars: ✭ 51 (+104%)
Mutual labels:  i18n

Enumize

build

Extend ActiveRecord::Enum for add more helpful methods, from rails/rails#36503.

🚨 This Gem does not change the way you use ActiveRecord::Enum, just adds method extensions.

中文介绍与使用说明

Usage

class Book
  enum status: %i[draft published archived]
end

Now we have status_name, status_color, status_value and Book.status_options methods:

  • #{attribute}_name - return I18n name for display.
  • #{attribute}_color - return color for enum value from I18n file.
  • #{attribute}_value - return raw value for enum, Now we don't need use Book.statuses[@book.status].
  • Book.#{attribute}_options - return a array list for select tag options, <%= f.select :status, Book.status_options %>.

Write I18n:

en:
  activerecord:
    enums:
      book:
        status:
          draft: Drafting
          published: Published
          archived: Archived
        status_color:
          draft: '#999999'
          published: 'green'
          archived: 'red'

zh-CN:
  activerecord:
    enums:
      book:
        status:
          draft: '草稿'
          published: '已发布'
          archived: '归档'

use the methods:

irb> @book = Book.new(status: :draft)
irb> @book.status_value
0
irb> @book.status_name
"草稿"
irb> @book.status_color
"#999999"
# You can still use the original methods from ActiveRecord::Enum
irb> @book.status
"draft"
irb> @book.draft?
true
irb> @book.published!
irb> @book.published?
true
irb> @book.status
"published"

For _options methods for select helper:

<% form_for(@book) do |f| %>
  <%= f.text_field :name %>
  <%= f.select :status, Book.status_options %>
  <%= f.submit %>
<% end >

Installation

Add this line to your application's Gemfile:

gem 'enumize'

And then execute:

$ bundle

Configure the I18n fallbacks for color method

You can write color config in en.yml once for all locales, so follow the I18n fallback, the others locale will use color value from en:

config/application.rb

module Dummy
  class Application < Rails::Application
    # Add to tell I18n fallback translate to en
    config.i18n.available_locales = ["en", "zh-CN"]
    config.i18n.fallbacks = true
  end
end

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].