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

  • Home
  • SEARCH
  • 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 7039443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:47:56+00:00 2026-05-28T01:47:56+00:00

So following is what I would like to do, be able to access I18n

  • 0

So following is what I would like to do, be able to access I18n locale as a constant.For example:

if (I18n.locale == I18n.locales.en)
puts "You are using viewing page in english"
end

Is there a way to access these constants (I18n.locales.en is just example to clarify)? I can always write

if (I18n.locale.to_s == "en")

but I would like to avoid that. Since code is less readable with that approach.

  • 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-28T01:47:57+00:00Added an answer on May 28, 2026 at 1:47 am

    You could shorten your second statement to be if (I18n.locale == :en) rather than converting to a string, however that’s still missing the point of using Rails’ locale support. For example, with this yml file:

    en:
      hello: "Hello world!"
    fr:
      hello: "Bonjour tout le monde !"
    

    You should just be able to do this without any conditional statements:

    puts I18n.translate(:hello)
    

    and it would give you the appropriate translation.

    If you really wanted to be able to do something like your first example, then you could override method_missing on the Symbol class. If you did this:

    class Symbol
      def method_missing(method_name, *arguments)
        if method_name.to_s[-1,1] == "?"
          self.to_s == method_name.to_s[0..-2]
        else
          super
        end
      end
    end
    

    Then you could do this:

    if I18n.locale.en?
      puts "english"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to access the following rest URLs: http://localhost:9998/helloworld http://localhost:9998/helloworld/
I would like to be able to do the following: $obj = new stdClass;
The following: >>>import struct >>>struct.unpack('!d', ('40d11998e5035288').decode('hex'))[0] >>> >>>17510.3889778429 I would like to be able
I would like to be able to access the current working directory in my
I would like my program to be able to access a website that processes
I have the following use case: I would like to be able to push
I would like to define properties for classes, and be able to access them
Within the following method, I would like to access any optional arguments that are
I would like my PHP script to be able to access the width of
I would like to know if the following code would be a good pattern

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.