Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:09:05+00:00 2026-05-25T19:09:05+00:00

I’m building a multilang application that has two essential models Category and Product ,

  • 0

I’m building a multilang application that has two essential models Category and Product, where a Category has many Products..

So I want the ability to display the same categories with more than a language, for example, consider a category called Cars, it should be presented as Vehicules for a user using the french version of the application.

How could I do that? Should I store them in different models? or should I add a lang column in the Category model ?

What I thought of doing is adding a lang column in the Category model and add a default_scope call to scope it to search for only the desired language, I have two questions though:

  1. How can I get the used language from inside a model, an I18n call ? Which method should I call on it ?
  2. A problem arises from using this technique, a product which references a category in french wouldn’t show up in a search under the category in english, how can I resolve this issue ?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:09:06+00:00Added an answer on May 25, 2026 at 7:09 pm

    Frankie Roberto gave you a good answer. I will just add some additional info.

    First the easy part: getting current locale in a model.

    If you set the language in a controller as I18n.locale = something, then you may read it in a model the same way. The I18n is a global constant, after all.

    Now about searching:

    I generally prefer the designs where you have one category, but the name of the category is translated to many languages. The design where one car belongs to category “Cars” and another one belongs to “Vehicules” is flawed IMHO. In the later case there is no sense of making it a single site – you could create a one-language application, and install it in multiple instances, where every one is translated to a single language.

    If you have a short, static list of supported languages, you may add a column for every language: name_en, name_fr, name_pl.

    If your list of languages will be large or unknown at the moment of designing your application, it would be easier to store the “name” (given as an example) as a serialized hash. So, you could get the intended translation as name["fr"], name["de"] and so on.

    I have already learned (the hard way 🙂 ) that users usually are too lazy to provide all the translations (or they just do not know all the languages), so you should be prepared that some of your models will not have the data in a language you are trying to display or search.

    For every ‘translated’ attribute you may want to prepare a method which will supply you with the most appropriate translation in case the required one is missing.

    This method may work in a way similar to:

    def translated_name
      ([I18n.locale] + other_languages).each do |l|
        return name[l] unless name[l].blank?
      end
      return "" # or some default value - possibly from I18n.t("some.static.default")
    end
    

    Of course, you do not need to use I18n.locale, and you should define other_languages in a way which would (ideally) match user language preferences, possibly by analyzing the “Accept-Language” header.

    I sometimes use the method name name_for(lang) or name_in(lang), if I have to support more languages for data than for interface (which I shall translate and put in I18n config files).

    Ah, yes – searching. 🙂

    If you have defined the “names” as separate attributes, you may just search the appropriate column.

    If you have the “names” as a single, serialized hash, you may search the column as a simple text (however I am not sure whether YAML will not mangle the non-ASCII characters) or create another column with searchable data, and then (at the model-level, not at the SQL level) partition the models into groups: “The search string has been found in your language”, “The search string has been found in some other language”. I believe the user of multi-lang service will be happy with search results presented in this way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.