How to really change MSBuild output/messages language?
Asked Answered
A

1

6

TL;DR : Is it possible with Visual Studio 2015's MSBuild to change the messages language of MSBuild on the command line using a direct approach instead of messing with system settings? And how?


We have a question from "way back then" that is essentially a duplicate, but IMHO the answers show the question needs to be refined.

I need this to work for VS2015 and up, so I'm not overly concerned with older versions.

So, here are some factoids:

  • Googling doesn't show up any MSBuild command line option that would change the language of the output messages.
  • Using MSBuild from the command line on a german system will give me german error messages.
  • Using an english visual studio on the same system to invoke MSBuild will make MSBuild (the actual MSBuild.exe as confirmed from process explorer process tree) output english messages on a german system!
    • This implies that devenv.exeis able to drive MSBuild.exe to use a different languange.

To wit:

c:\temp\TestApp>msbuild TestApp.sln /t:rebuild /v:normal

Microsoft (R)-Buildmodul, Version 14.0.25420.1
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.

Die Projekte in dieser Projektmappe werden nacheinander erstellt. Um eine parallele Erstellung zu ermöglichen, müssen Sie den Schalter "/m"
hinzufügen.
Der Buildvorgang wurde am 18.10.2016 11:06:33 gestartet.
Projekt "c:\temp\TestApp\TestApp.sln" auf Knoten "1", rebuild Ziel(e).
ValidateSolutionConfiguration:
  Die Projektmappenkonfiguration "Debug|X64" wird erstellt.
Das Projekt "c:\temp\TestApp\TestApp.sln" (1) erstellt "c:\temp\TestApp\TestApp\TestApp.csproj" (2) auf Knoten "1", Rebuild Ziel(e).
CoreClean:
  Die Datei "C:\temp\TestApp\TestApp\bin\x64\Debug\TestApp.exe.config" wird gelöscht.
  ...
GenerateBindingRedirects:
  Keine vorgeschlagenen BindingRedirect-Einträge von ResolveAssemblyReferences.
GenerateTargetFrameworkMonikerAttribute:
Das Ziel "GenerateTargetFrameworkMonikerAttribute" wird übersprungen, da alle Ausgabedateien hinsichtlich der Eingabedateien aktuell sind.
CoreCompile:
...

and from devenv:

c:\temp\TestApp>devenv.com /Rebuild "Debug|x64" TestApp.sln

Microsoft Visual Studio 2015 Version 14.0.25420.1.
Copyright (C) Microsoft Corp. All rights reserved.
1>------ Rebuild All started: Project: TestApp, Configuration: Debug x64 ------
1>Build started 18.10.2016 11:10:27.
1>CoreClean:
1>  Deleting file "C:\temp\TestApp\TestApp\bin\x64\Debug\TestApp.exe.config".
...
1>CoreCompile:
...

Clearly, devenv drives the output here, but the actual msbuild messages are the same, and devenv will invoke MSBuild.exe as an actual child process.

I have checked with Process explorer: The invoked MSBuild.exe doesn't have any command line parameters indicating the project in use nor is there any apparent environment variable set. So it seems devenv is "telling" MSBBuild to use english as the language by some other means.

Side note MSBuild is invoked from VS like this: C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe /nologo /nodemode:1 /nodeReuse:true

So, how do I tell MSBuild.exe that it should english output? I'm fine with any wrapper approach or maybe some powershell magic (though that seems unlikely), but I'm not fine with having to rely on machine/user locale settings for this.

Also, I've found an MSDN thread where the answer was -- 8 years ago -- that VS can do this because it sets the UI culture - but if devenv can do this, for it's invoked msbuild.exe I'd have though I probably can too: I just have no clue how.

Attenweiler answered 18/10, 2016 at 9:22 Comment(6)
From the document, we know, msbuild doesn’t have any argument to set the display language. We need to witch your machine's system locale to English (#22529964)Pilaf
@starain - Yeah, HansPassant wrote that two years ago. I even mention it in the question that there doesn't seem to be a switch. Doesn't mean there's a better way, because this question details that VS can drive MSBuild to output another language.Attenweiler
Leave a comment here for someone search for devenv's language of output message. (Google search leads to here). /LCID id or /L id is the way in msdnProximal
@LouisGo - the /LCID switch you link to seems to be for devenv.exe -- this question is about msbuild.exe outside of visual studio. (The again maybe you meant that by ".. a comment here for someone search for devenv ..")Attenweiler
Hi Martin, I did mean for devenv. Since I search devenv but search engine leads to here, I decide to leave a comment here. Though itis not relative to your main question, your post convince me devenv could change output language. Do you suggest toremove my comment or make it more clear?Proximal
@LouisGo - your comment is fine. It's valuable input.Attenweiler
R
2

I've ended up with the solution below. Don't really know if code is absolutely correct (I'm not .NET developer) but it runs msbuild with "en-us" locale being set.

class Program : MarshalByRefObject
{
    static void Main(string[] args)
    {
        var newDomain = AppDomain.CreateDomain("enUSDomain");
        Program newProgram = (Program)newDomain.CreateInstanceAndUnwrap(
                typeof(Program).Assembly.FullName,
                typeof(Program).FullName);

        newProgram.Execute(args);
    }

    public void Execute(string[] args)
    {
        System.Globalization.CultureInfo.DefaultThreadCurrentUICulture =
            new System.Globalization.CultureInfo("en-US");

        byte[] bytes = File.ReadAllBytes("C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe");
        Assembly assembly = Assembly.Load(bytes);
        MethodInfo main = assembly.EntryPoint;

        main.Invoke(null, new object[] { });
    }
}
Redroot answered 19/12, 2016 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.