views:

20

answers:

1

I need to make certain output from views dynamic based on the "type" of user logged in. Specifically, the user "type" is based on the type of business they are in and using my application, so if they are a retail store my application needs to refer to "customers", "products", "purchases", etc. whereas another type of user in a services-based business expects to see references to "clients", "services", "engagements" and so on. I've abstracted other aspects of my application so that they can work with both types of users, but I need a way to dynamically display "customer" vs. "client", "product" vs. "service", and so on. (I've simplified the problem somewhat - the wording differences are more pervasive than the examples I've provided here, and there are some situations where the wording differences also appear in models, such as in validation error messages.)

This seems somewhat like locales and internationalization, but I don't want to kludge together a solution based on locales then run into a problem when I actually need to support other languages. In Java, I think this would be solved by resource bundles - that's the type of thing I believe I need.

What's the "Rails way" of solving this problem? Thanks in advance.

+1  A: 

What about using a YAML file with each user type represented... so you could do something like this (have not tested this, but general idea may be useful):

config/language.yml:

customer:
  product_name: Product
client:
  product_name: Service

config/initializers/load_config.rb:
LANGUAGE = YAML.load_file("#{RAILS_ROOT}/config/language.yml")

view:
<%= LANGUAGE[user.type].product_name %>

Gordon Isnor
This seems like a good solution. Thanks Gordon.
Chris Hart