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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:01:31+00:00 2026-06-01T16:01:31+00:00

A lot of code have been deleted, but I really need to show only

  • 0

A lot of code have been deleted, but I really need to show only this part. The main idea: method Localize() must work speedy, so it mustn’t be locked by any other thread. It uses flag isLoaded to detect, which type of localization to use – Old or Current.

static class Localization
{
    static readonly object locker = new object();

    static string LocalizationDir;

    static bool isLoaded;
    static Tuple<string, string> OldLanguage;
    static Dictionary<string, string> OldLocalization;
    static Dictionary<string, string> CurrentLocalization;

    public static Tuple<string, string> CurrentLanguage {
        get;
        private set;
    }

    static Localization() {
        lock ( locker ) {
            OldLanguage = null;
            CurrentLanguage = new Tuple<string, string>("en", "English");
            isLoaded = true;

            OldLocalization = null;
            CurrentLocalization = null;
        }
    }

    public static bool SetLanguage(string languageShortName) {
        lock ( locker ) {
            string languagePath = Path.Combine(LocalizationDir, languageShortName + ".loc");

            // save localization, be ready to return it back
            OldLocalization = CurrentLocalization;
            OldLanguage = CurrentLanguage;
            isLoaded = false;

            try {
                using ( TextReader i = new StreamReader(languagePath) ) {
                    /*
                        Parse file, 
                        Modify CurrentLocalization, CurrentLocalization
                    */
                }
            }
            catch ( Exception e ) {
                // Just return back our good localization data

                CurrentLocalization = OldLocalization;
                CurrentLanguage = OldLanguage;
                isLoaded = true;

                OldLocalization = null;
                OldLanguage = null;

                return false;
            }

            // everything is good
            {
                OldLocalization = null;
                OldLanguage = null;
                isLoaded = true;

                UpdateControls();
            }

            return true;
        }
    }


    // <summary>
    // We think that there are no bugs in this method
    // No locking
    // </summary>
    public static string Localize(this string Text) {
        if ( CurrentLanguage.Item2 == "English" )
            return Text;

        Dictionary<string, string> ChoosedLocalization = null;
        if ( !isLoaded && OldLocalization != null )
            ChoosedLocalization = OldLocalization;
        else if ( isLoaded && CurrentLocalization != null )
            ChoosedLocalization = CurrentLocalization;

        if ( ChoosedLocalization != null ) {
            string Translate;

            if ( !ChoosedLocalization.TryGetValue(Text, out Translate) )
                return Text;
            else
                return Translate;
        }
        else
            return Text;
    }
} 
  • 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-01T16:01:32+00:00Added an answer on June 1, 2026 at 4:01 pm

    I suspect it is not. I also suspect that you’re overthinking things. In SetLanguage, all you should do it load up the new dictionary and then swap it in for the old. That assignment is atomic, so as long as your Translate code doesn’t use CurrentLanguage twice in a row and assume it’ll be the same and that UpdateControls call waits until a previous call is done (or some other race condition prevention) then it should be simpler, cleaner, and more reliable.

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

Sidebar

Related Questions

Now i know there have been a lot of question about this, but i
I have been reading a lot about QR codes and how the code itself
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I am have a lot of code that I need to optimize and make
In my project I have a lot of code like this: int[][] a =
DirectDraw 7 is very old APIs but we have a lot legacy code written
I have some code that at one part will get executed a lot, and
I have been downloading a lot of example code to help me gain a
I have just refactored a lot of code out of my AccountController into an
A lot of time I have to code from home. Normally I just remote

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.