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 8069667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:07:30+00:00 2026-06-05T13:07:30+00:00

I am using an Translator object (custom class) to expose website texts (the object

  • 0

I am using an Translator object (custom class) to expose website texts (the object stores texts from the database). The Translator object is stored in the cache, in the Application_Start() function.

My current use of the Translator object is:

  • I have a MasterViewModel

    public class MasterViewModel
    {
        public Translator Translator = HttpContext.Current.Cache.Get("Translator") as   Translator;
    }
    
  • Every view has a viewmodel, that inherents from MasterViewModel

    public class RandomViewModel : MasterViewModel
    {
    }
    
  • In my views i can use my Translator object

    @model ViewModels.RandomViewModel
    
    @Model.Translator.GetText(label)
    

I don’t think this is a nice aproach.
Is it a good idea to make a razor helper in App_Code, so that in my views i can use

    @Translate.GetText("RANDOM_TEXT")

This will be the Helper function (in Translate.cshtml)

    @helper GetText(string label)
    {
        Translator Translator = @Cache.Get("Translator") as Translator;
        @: Translator.GetTextByLabel(label);
    }

So my question is what is the best way of exposing a cached object to all my views.
Is one of the above approaches good? Or should i go with another solution?

(I hope my english is okay, i am dutch)

  • 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-06-05T13:07:32+00:00Added an answer on June 5, 2026 at 1:07 pm

    There are different ways you can achieve that but I’ll create a base class by deriving from WebViewPage and force all the razor views inherit from this class.

    public abstract class MyWebViewPageBase<T>: WebViewPage<T>
    {
       private Translator _translator;
    
       protected override void InitializePage()
       { 
         _translator = Context.Cache.Get("Translator") as Translator;
       }
    
       public string Translate(string label)
       {
         if(_translator != null)
           return _translator.GetText(label);
    
         return "";
       }
    }
    

    Now I can inherit the MyWebViewPage in razor views through couple of ways:

    The first approach is from every view you have to use the @inherits directive.

    Ex.

    // Index.cshtml
    @inherits MyWebViewPageBase
    @model ...
    
    ....
    

    The other approach is a global way of setting the base page for all the views and for that you have to modify the web.config that exists in the Views folder.

    You have to set the pageBaseType of pages element as below,

    <pages pageBaseType="MyWebViewPageBase">
    

    Now from any view you can simply call the translate method as below,

    @Translate("RANDOM_TEXT")
    

    Hope this helps!

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

Sidebar

Related Questions

I am developing a simple translator from MathML to Latex, using Lex and Yacc.
I'm using scaling and transforming my graphics object when painting a custom control, in
I am currently trying to draw an object using VBOs. In the Cube-Class I
Im trying to translate an object without using glTranslate(). I have searched through web
I'm contemplating the idea of implementing a XML translator using a compiler generator, based
I'm using Google Translation API to translate text from Chinese Simplified to English in
I'm using a LINQ query to translate data inside a DataTable object to be
I am using a predicate to find an object in core data. I can
I'm using Symfony 1.2 with Doctrine. I have an object with the i18n behaviour.
All, Working on a custom TCP/IP server for a text-based multiplayer game, using Ruby.

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.