AndrewKeepCoding / WinUI3Localizer

The WinUI3Localizer is a NuGet package that helps you localize your WinUI 3 app.
MIT License
88 stars 6 forks source link

PublishTrimmed/TrimMode support? #44

Open Lawls91 opened 6 months ago

Lawls91 commented 6 months ago

Hey,

I am trying to use the library, which works fine untrimmed. Unfortunately we are having issues with TrimmedMode with .NET7/8, in which the app crashes at launch.

I have managed to resolve this by using TrimMode partial as suggested here https://github.com/microsoft/WindowsAppSDK/issues/2478#issuecomment-1485912900 which has the app running but none of the strings are being loaded in. Its tricky to debug as its occuring after a publish.

Does WinUI3Localizer support trimmed apps / partially trimmed apps with .NET 7/8?

AndrewKeepCoding commented 6 months ago

Hi! I'm away from my laptop for a few days. I've never tried it with trimming. I'll take a look when I'm back. ;)

AndrewKeepCoding commented 6 months ago

Can you create and share a reproducible project on GitHub?

Lawls91 commented 6 months ago

Hey sorry I've not got around to this yet, I will try and get something for you this week

Lawls91 commented 4 months ago

Here is an attached reproducible project, sorry for the delay!

LocalizerTest.zip

You should be able to run this sln, F5 to run locally which will load the nav bar content and the title bar.

Then in Developer Powershell in VisualStudio, run

dotnet publish .\LocalizerTestApp\LocalizerTestApp.csproj /p:PublishProfile=.\Properties/PublishProfiles/win-x64.pubxml

and run the exe from that directory. Result is none of the strings get loaded. You can see some trim warnings coming out of the publish step also. Turning off partial trim causes the app to crash instead of just not loading.

Let me know if I can provide anything else to assist

Lawls91 commented 3 months ago

@AndrewKeepCoding, is there anything else I can provide to help you out with this? Is the test project OK?

AndrewKeepCoding commented 3 months ago

Hi @Lawls91! I'm sorry for the late response. I must have missed this notification.

I tried to reproduce the issue but as you can see, the strings are loaded as expected.

image

Can you make sure that the Strings folder and its Resources.resw files exist aside the EXE?

Lawls91 commented 3 months ago

Hey @AndrewKeepCoding , no problem! I cant claim to understand the trim process fully, but I think that the assemblies in the /bin/Release folder are created before the trim?

Please after running the above dotnet publish step run the exe from the trimmed output directory. Should be:

SOLUTION_DIRECTORY/x64/LocalizerTestApp/LocalizerTestApp.exe

From there, when you run it, the strings dont display. The Strings directory is alongside it also

Let me know if you are able to see this behaviour also

AndrewKeepCoding commented 3 months ago

It works. Can you add a logger from the builder? Or add the following in the HomePage so we can see inside the localizer?

<Page
    x:Class="LocalizerTestApp.Pages.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="88"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Text="Home" FontSize="28" />
        <StackPanel Grid.Row="1">
            <TextBlock x:Name="StringsFolderPath"/>
            <TextBlock x:Name="CurrentLanguage"/>
            <ListView x:Name="AvailableLanguages"/>
            <ListView x:Name="LanguageDictionaries"/>
        </StackPanel>
    </Grid>
</Page>
public HomePage()
{
    InitializeComponent();
    Loaded += HomePage_Loaded;
}

private void HomePage_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    StringsFolderPath.Text = Path.Combine(AppContext.BaseDirectory, "Strings");
    CurrentLanguage.Text = Localizer.Get().GetCurrentLanguage();
    AvailableLanguages.ItemsSource = Localizer.Get().GetAvailableLanguages();
    LanguageDictionaries.ItemsSource = Localizer.Get().GetLanguageDictionaries().FirstOrDefault().GetItems().Select(item => item.Value);
}
Lawls91 commented 3 months ago

@AndrewKeepCoding accessing by code works (the "APP TITLE BAR" works for example and thats setup in the constructor of MainWindow.xaml.cs). It appears to just be strings that are setup through the XAML directly that dont work. I added that extra stuff for you, you can see it is outputting, but the strings for the nav bar still arent (when using the trimmed published exe, as mentioned the exe inside the project folder works, its the one off the solutions directory that doesnt)

image

I've attached a prebuilt copy, if you are launching it from SOLUTION_DIRECTORY/x64/LocalizerTestApp/LocalizerTestApp.exe already, then maybe its related to the compiler? Please can you run this and check behaviour? (Sorry, had to double compress it with 7z for github)

LocalizerTestApp.7z.gz

Lawls91 commented 3 months ago

Hey @AndrewKeepCoding just wondering if you missed my latest reply?

AndrewKeepCoding commented 3 months ago

In a couple of days, I'll have access to a pc for testing. The thing is that I just can't run files on the laptop I use for work. 😅

Lawls91 commented 3 months ago

Makes sense, no problem, thanks for the reply!

Let me know if theres anything I can provide to help. I've tested on a couple other machines with some of our other devs and get the same behaviour, whereby the output in SOLUTION_DIRECTORY/x64 doesnt work, but it does in SOLUTION_DIRECTORY/PROJECT_DIRECTORY/bin/Release (assume thats not Trimmed yet in there)

AndrewKeepCoding commented 3 months ago

@Lawls91 I finally managed to reproduce the issue. Even though the localizer has the resources loaded as expected, it’s failing to get the properties from the controls. It seems that this is because ‘Reflection’ doesn’t work well with trimming. See here. I'm not sure if we'll have a better way to handle this in the future or not.

I'm sorry I couldn't bring better news after all.

Lawls91 commented 3 months ago

@AndrewKeepCoding Is it worth investigating opting the localizer modules out of trimming? Reading this, it seems like partial trimming should work with that package just set to not trim (I've not really dug into that though)

https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-8-0#trimming-granularity

AndrewKeepCoding commented 2 months ago

I tried it but still didn't work...😵

AndrewKeepCoding commented 2 months ago

@Lawls91 Can you try trimming with the WinUI3Localizer sample app? It seems to be working.

AndrewKeepCoding commented 1 month ago

Hi @Lawls91 ! The WinUI3Localizer definitely has issues with trimming.