Translation

Started by Aragas, June 10, 2015, 02:41:59 PM

Previous topic - Next topic

Aragas

Hello. I saw that many mods that have dynamically created items (e.g. Seeds Please) don't have traslation options.
There are many paths to fix it. References and other stuff.
One of the easiest, in my opinion, is to override a bit our abstract class (that implements Thing or similar class), that is used for creating items.


For localizable label use this:

        public override string Label
        {
            get
            {
        // def.defName is our ClassName from Xml stuff, see below.
        // We check if def.defName(ClassName) is declared in our translation.
        // If it is declared, translated version will be in label.
        // If not, the name will be ClassName
                var label = def.defName.Translate();
        // If there isn't any translated version, use the default version,
// Declared in Xml stuff where items are declared.
                if (def.defName == def.defName.Translate() || string.IsNullOrEmpty(label)))
                    return base.Label;
                else
                    return label;
            }
        }
        public override string LabelBase
        {
            get
            {
                var label = def.defName.Translate();
                if (def.defName == def.defName.Translate() || string.IsNullOrEmpty(label)))
                    return base.LabelBase;
                else
                    return label;
            }
        }

        public override string LabelCap
        {
            get
            {
                var label = def.defName.Translate();
                if (def.defName == def.defName.Translate() || string.IsNullOrEmpty(label)))
                    return base.LabelCap;
                else
                    return label;
            }
        }


If you want implenemt localizable description, use this:

        public override string GetDescription()
        {
    // Same. Check if translated version is available. We add Desc so there wasn't naming conflict.
            var name = def.defName + "Desc";
            var description = (name).Translate();
            if (description == name || string.IsNullOrEmpty(description))
                description = base.GetDescription();

            //description += "Add Something More If You Wanna"

            return description;
        }


The assembly code is done. So, what to do next? Xml time!

Dynamic stuff declaration:

<ThingDef Name="AbstractClass" Abstract="True">
    <thingClass>DllAssembly.ClassThatImplementsThingOrSimilar</thingClass>
    <label>unspecified resource</label>
    <description>Description</description>
</ThingDef>

<ThingDef Class ="DllAssembly.ClassThatImplementsThingOrSimilar" ParentName="AbstractClass">
    <defName>ClassName</defName>
    <label>class name</label>       
</ThingDef>


Translation:

<?xml version="1.0" encoding="utf-8" ?>
<LanguageData>
  <ClassName>TranslatedClassName</ClassName>
  <ClassNameDesc>TranslatedDescription</ClassNameDesc>
</LanguageData>


Hope my tutoial isn't hard to understand. I'm patched Seeds Please already, just need to make some clarifications.