'Using Iso639 comparison in Rails Active Record Json field filter

I want to search if there are any records which has the specific language . Language is stored in short forms, for example : 'en-US' and I want to search it against plain text format, for example : "English". The values are stored as json.

  def rule_is
      filtered_visitor_ids = Insights::Visit.where(
        "visitor_id IN (?) AND lower(insights_visits.context#>>'{browser, language}')) = lower(?)", visitors.pluck(:id), visitor_attribute_value).distinct.pluck(:visitor_id)
      visitors.where(id: filtered_visitor_ids)
    end

Example:

visitor_attribute_value is "English" and I want to search it against "en-US" . How can I do this using Iso639?



Solution 1:[1]

i18n_data can help you.

require 'i18n_data'

I18nData.language_code('English') # EN
I18nData.languages('en') # List of countries

Or if you want to have more control you can use the raw files with the information, or even from the source and parse them yourself.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 javiyu