windows service unable to find resource file of other language

Pacoss 21 Reputation points
2021-08-31T09:15:43.697+00:00

Hi,

I'm am developing windows service project in VS 2017 which inludes 2 resx files: RsysRes.resx (default language) and RsysRes.en.resx. Both files are compiled as embedded resource.
Service does some job and when the job is done an email is sent to user or if some error occurs, error is logged into log text file.
Problem is when the service is started and CurrentUICulture = "en-US" (different from default language) and the job is done or error occurs, email message text or error message text should be in english but it is not, message is in default language.
I have LoadSettings() method in which Thread.CurrentThread.CurrentUICulture is set and it works ok. LoadSettings() is called in OnStart():

private string _lang;
System.Timers.Timer _timer = new System.Timers.Timer();


protected override void OnStart(string[] args)
        {
            try
            {
                LoadSettings();

                string msg = string.Format("******* Reportsystem service started at: {0} *******", DateTimeExt());
                CreateLogFile(msg, null, null, true);
            }
            catch (Exception ex)
            {
                _fatalError = true;
                CreateLogFile(ex.Message, null, null, true);
                Util.SendEmail_Status(ex.Message, _serviceSendsNotificationEmailTo);
            }


            if (_fatalError)
                return;

            _timer.Elapsed += new ElapsedEventHandler(this.OnElapsedTime);
            _timer.Interval = 50000; //miliseconds
            _timer.Enabled = true;
        }



private void LoadSettings()
{
    try
    {
        _lang = GetSettingsValue("LANG_BASE");

        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(_lang);

        // some other code
    }
    catch (Exception)
    {
        throw;
    }
}

Values from resx files are retrieved as follows:

RSysRes.ResourceManager.GetString("summary");

In case of "en-US" ResourceManager should retrieve a value from RSysRes.en.resx file but the value is retrieved from RSysRes.resx.

Does anyone know what i'm doing wrong and how to make it work?
I appreciate any help.

Windows development | Windows API - Win32
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-09-01T02:54:28.477+00:00

    How did you add resource files?

    I right-clicked the project=>Properties=>Resources to add a default resource file, and then right-clicked the project=> Add => New Item => Resources File to add a second resource file.

    Then I encountered the same problem, the code will only get the information in the default file.

    But if I use the second method to add both resource files, my problem is solved.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.