Translation System

Started by Seyon, May 01, 2019, 11:38:35 AM

Seyon

Hello, me again.

I have a little problem with translations.
I wrote a mod to upgrade walls.

Unfortunately, I have the problem that I do not get some translations.

I have the following class:



    class UpgradingSystem : ProtoSystem<UpgradingSystem>
    {

        public const string NotificationUpgrade_Title = "Cannot upgrade";

         public const string NotificationMissingTech_Message = "You lack the necessary technology.";

.....

https://github.com/seyon/cryofall_mods/blob/master/SeyonWallUpgrade/Scripts/Systems/Upgrading/UpgradingSystem.cs#L30

In the same class I have the following methods:


        public static void SharedShowCannotUpgradeNotification(
            ICharacter character,
            string errorMessage,
            IProtoStaticWorldObject proto)
        {
            if (IsClient)
            {
                Instance.ClientRemote_ClientShowNotificationCannotUpgrade(errorMessage, proto);
            }
            else
            {
                Instance.CallClient(
                    character,
                    _ => _.ClientRemote_ClientShowNotificationCannotUpgrade(errorMessage, proto));
            }
        }

        private void ClientRemote_ClientShowNotificationCannotUpgrade(string errorMessage, IProtoStaticWorldObject proto)
        {
            NotificationSystem.ClientShowNotification(
                NotificationUpgrade_Title,
                errorMessage,
                color: NotificationColor.Bad,
                icon: proto.Icon);
        }

https://github.com/seyon/cryofall_mods/blob/master/SeyonWallUpgrade/Scripts/Systems/Upgrading/UpgradingSystem.cs#L284

(I copied the methods from the Core.)

In my "ActionState" class, I call the method as follows:


            if (!this.CheckHasNeededTech(character, protoStructure))
            {
                UpgradingSystem.SharedShowCannotUpgradeNotification(character, UpgradingSystem.NotificationMissingTech_Message, protoStructure);
                return false;
            }

https://github.com/seyon/cryofall_mods/blob/master/SeyonWallUpgrade/Scripts/Systems/Upgrading/UpgradingActionState.cs#L375

and I have the following translation file


Scripts/Systems/Upgrading/UpgradingSystem@NotificationUpgrade_Title:
Upgrade fehlgeschlagen

Scripts/Systems/Upgrading/UpgradingSystem@NotificationMissingTech_Message:
Dir fehlt die n├╢tige Technologie.

.....

https://github.com/seyon/cryofall_mods/blob/master/SeyonWallUpgrade/Localization/de_de/SeyonWallUpgrade.de_de.txt


My problem now is that the "title" is translated correctly.
But the "message" remains untranslated.

In the translation file I used the class as namespace in which the constant is defined. (the same for both cases)

At first I thought the namespace might have to match the class in which the method is called and not where the constant is defined. However, it did not work out when I changed the message namespace to ActionState where the call happens.

Did I miss anything? Or how exactly do the translations work?

It's probably just a small mistake but I have no idea and would be happy about a little tip.

Thanks in advance.

ai_enabled

Hi!


Instance.CallClient(
    character,
    _ => _.ClientRemote_ClientShowNotificationCannotUpgrade(errorMessage, proto));

This code is executed on the server side so the error message is presented in the server language which is defined in SettingsServer.xml file.

In such cases we recommend sending a enum instead of a error message text and getting the error message on the client side so it's always properly localized.
But as I can see in your case, it's not actually required as there is only one possible error message.
In that case it makes sense to remove errorMessage arg from SharedShowCannotUpgradeNotification and use NotificationMissingTech_Message in ClientRemote_ClientShowNotificationCannotUpgrade.
Regards!

Seyon

great thank you!
I did not think of that. But of course it makes sense.

I will try it as suggested with an enum, since I already have 3 error messages and probably more to come  ;D

ai_enabled

In case of enum ΓÇö it will break compatibility with the existing save unless you mark it with an [NotPersistent] attribute like this:
[NotPersistent]
public enum MyErrorCodes ...


But in that case you could much more easily add error messages like this:
[NotPersistent]
public enum MyErrorCodes : byte
{
    [Description("Error 1 message")]
    Error1 = 1,
   
    [Description("Error 2 message")]
    Error2 = 2
}


To localize it you can use the same translation text file approach, just need to write the enum entry name and our localization system will swap [Description(...)] text with localized version during the compilation.

Regards!

Seyon