kl1mm / localize

MIT License
33 stars 3 forks source link

Add Support for Nested Classes #8

Closed Maskoe closed 1 month ago

Maskoe commented 1 month ago

Hello, very cool library.

I would like to have just one Locale.json file per language. I would structure it like this

Messages/T.json

{
  "Password": {
    "Uppercase": "Password needs at least one uppercase letter.",
    "Digit": "Password needs at least one number.",
    "Length": "Password must be at least 8 characters long."
  },
  "CreateUser": {
    "EmailInUse": "Email is already in use.",
    "InvalidRole": "Invalid role. Allowed roles are {roles}."
  }
}

I would like that to lead to

var err = T.Password.Uppercase;

Is that doable? If you dont want to do it, can you point me in a direction so I can give it a try? Obviously while at it, it should probably work with any level of nesting. So I can really go ham and do T.Errors.CreateUser.InvalidRole.

stefc commented 1 month ago

Very good suggestion !

kl1mm commented 1 month ago

Hey @Maskoe, sounds like a great improvement. I am currently on vacation. I'll try to find time afterwards and see if I can incorporate your suggestion. Please don't be angry if it takes longer.

Best regards mMilk

kl1mm commented 1 month ago

Hey @Maskoe, @stefc please try v1.0.4 and see if its fit your requiererments.

see the docs for more informations.

Best regards mMilk

Maskoe commented 1 month ago

I am constantly getting

The type or namespace name 'Locales' does not exist in the namespace 'LocTest' (are you missing an assembly reference?)

If I copy the generated class into my project and disable your source generator, it works. I can also import the namespace just fine. It just wont let me build for some reason.

I got it to work once, but I have no idea what I did. I tried different projects, solutions, setting namespace and class name manually, restarting rider 10 times.

I also have this warning in my console

0>CSC: Warning CS9057 : The analyzer assembly 'C:\Users\Mirco\.nuget\packages\kli.localize\1.0.4\analyzers\dotnet\cs\kli.Localize.Generator.dll' references version '4.11.0.0' of the compiler, which is newer than the currently running version '4.9.0.0'.

using LocTest.Locales;

namespace LocTest;

public class Testtt
{
    public void DoSomething()
    {
        var msg = T.Uppercase;
        Console.WriteLine(msg);
    }
}

Edit: Ahh found the difference. If I turn on <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> in version 1.0.4 nothing happens. If I turn it on on version 0.8.x I see my files in obj/debug/net8.0/generated

kl1mm commented 1 month ago

@Maskoe, thanks for your feedback, i will have a look

kl1mm commented 1 month ago

@Maskoe, I have reset the expected compiler/sdk dependencies. I hope this helps you for now => v1.0.6

Maskoe commented 1 month ago

Yea works for me now. Its awesome.

Thats pretty much how every i18n library in the javascript world does it.

Was it a bug in your code or was it the weird compiler version thing?

kl1mm commented 1 month ago

I'm glad to hear that it works. I'm still not 100% sure what is causing the problem. It is under investigation :)

If you think the issue is resolved - feel free to close it.

best regards mMilk