How does Locbaml work?

2020-03-01 17:21发布

I created a WPF test project, single Window with a single button in it. I then unloaded the project and modified the csproj to contain the following <UICulture>en-US</UICulture>. After rebuilding the build folder now contains a subfolder named en-US and it contains a file called WpfLocalizationTest.resources.dll. All clear so far.

Then I downloaded the source code for Locbaml and built it. (Couldn't find a binary download anywhere, go figure.)

Then I copied the Locbaml.exe to the en-US folder and tried the following.

locbaml /parse WpfLocalizationTest.resources.dll /out:test.csv

This results in an error 'Could not load file or assembly WpfLocalizationTest.resources.dll or one of its dependencies. An attempt was made to load a program with an incorrect format.'

In the obj\x86\Debug folder there's a file called WpfLocalizationTest.g.en-US.resources. I tried running locbaml on that, but the result was the same.

How is locbaml supposed to be used? MSDN is full of cockamamie samples, none of which work. Is locbaml really how Microsoft intends WPF apps to be localized? Or have they come up with proper tools for the job?

标签: wpf locbaml
3条回答
Anthone
2楼-- · 2020-03-01 17:44

1) Unload project you want to localise

2) Edit Project .cproj file

3) Add property group at the end of the last property group

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>x86</PlatformTarget>   
</PropertyGroup>   

en-GB

4) Reload project and compile this would create a satellite assembly "yourlibrary.resources.dll" inside en-GB folder at bin\debug location, this assembly would be the default assembly.

5) Open Properties\AssemblyInfo.cs file and uncomment this line [assembly: NeutralResourcesLanguage("en-GB", UltimateResourceFallbackLocation.Satellite)] this is a fallback satellite assembly. And we need this entry otherwise for wpf application it throws exception around app.xaml.cs couldn't load

6) From command prompt run this command which uses msbuild to generate UID C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild /t:updateuid . \yourlibrary.csproj, open xaml file and check your controls there would be UID on all elements.

7) Download locabaml.exe source code tool from https://github.com/JeremyDurnell/locbaml

8) Copy locabaml.exe file to the \yourprojectname\bin\debug folder

9) we would now create an satellite assembly for french. First we need to parse the default satellite assembly and write out the contents to a csv file as shown here

10:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild /parse C:\yourprojectname\bin\Debug\yourlibrary.resources.dll /out:C:\yourprojectname\bin\Debug\yourlibrary.resources_FR.csv

11) Open yourlibrary.resources_FR.csv make necessary translations

12) Now we need to create a satellite assembly in french using command line

13) C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild /generate C:\yourprojectname\bin\Debug\yourlibrary.Resources.dll /trans: C:\yourprojectname\bin\Debug\yourlibrary.resources_FR.csv /out:C:\yourporjectname\bin\Debug\temp /cul:fr-FR

14) The command above would create a folder fr-FR at \bin\debug location

15) Switch your computer region settings to french(France)

16) In the code set localisation to Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;

17) Compile and run application you would see translated text on the controls

查看更多
乱世女痞
3楼-- · 2020-03-01 17:49

As I continue to search I found another link here on Stackoverflow. The compile errors we were originally receiving were related to an outdated LocBaml project not being set for .Net4.0. This is why I was unable to generate teh CSV from the DLL and had to go straight at the resource file. Follow this link for more details. Locbaml localization of .net wpf4 application

This link provides a link to .net 4 binaries which once compilied allow you to go straight at the dll to generate the CSV using LocBaml.

查看更多
劳资没心,怎么记你
4楼-- · 2020-03-01 18:04

Just an update I pulled my notes this morning. Hopefully this will help get you a bit further along.

Once the project has been compiled copy the LocBaml.exe to the project directory where the build has been generated : In my instance I copied the file to E:\localiztion_sample\localiztion_sample\obj\x86\Release

This is the tricky part in that the build did not contain all the DLL files from the bin directory (telerik controls and other assemblies). As a result I went to bin\release\ and copied all the DLL and resource files from there into the obj\x86\Release directory. For files where I was prompted to overwrite I looked to see if there was a difference in filesize or date created and if not I skipped the copy for these objects.

NOTE: In order for me to generate the CSV I had to copy dll and resource files from the bin directory and place them in the obj directory. Omitting this step will result in the CSV file being created but not populated with data.

Once you have copied the necessary files to the directory you will then parse the .resource file located within the Release directory.

In my project the resource file was located at:

E:\localiztion_sample\localiztion_sample\obj\x86\Release

And the file name was titled: localiztion_sample.g.en-US.resources

Note: this is different from the instruction on the Microsoft website. Microsoft states that you should run the LocBaml tool on the dll file located within the en-US directory. However after multiple attempts and research I found that this in fact caused a number of problems with compatibility between 32 and 64-bit builds as well as it just flat out not working.

In reading through wpf4 unleashed as well as online forums it is suggested to instead point to the *.gen.en-Us.resources file. * = the project name and gen.en-Us reflects the development language chosen

Within the VS command untility you will then need to enter the following

LocBaml /parse filename.resources /out: sample-en.csv

Notes: It is assumed that you have copied the LocBaml file to the root directory where this file exists and that you are running the command prompt as system administrator. For ease of use I changed the working directory within the command prompt to the VS project directory

For my sample project the command looked as:

*LocBaml /parse localization_sample.g.en-US.resources /out: sample-en.csv*

This command then generates the CSV file which acts as a definition of the current project

This was as far as I got I was able to modify teh file and regenerate the dll but I was not able to get the culture to change within the application so I am still working on this piece. I'll reposte once I get it working.

查看更多
登录 后发表回答