Git Product home page Git Product logo

Comments (7)

 avatar commented on September 28, 2024

After reading it, I actually think solution 2 is rather elegant.

from bepisplugins.

 avatar commented on September 28, 2024

To make a generic solution, I would like if it is possible if there was a hook for each available text framework that you use.

For instance, you could implement two events on DynamicTranslator:

public event Action<object, string> OnUnableToTranslateUGUI;
public event Action<object, string> OnUnableToTranslateTextMeshPro;

In this way, I can still handle IMGUI and NGUI text events, yeah? :)

from bepisplugins.

 avatar commented on September 28, 2024

I've implemented my part on this branch:

https://github.com/XUnity-Plugins/XUnity.AutoTranslator/tree/feature/plugin_integration

from bepisplugins.

 avatar commented on September 28, 2024

Hm. I am trying to create a pull request, but I am absolutely unable to build the solution, even after adding all the missing DLLs in the lib folder manually.

Do you use something special to build this project? I've tried with both VS2017 and VS2015.

from bepisplugins.

 avatar commented on September 28, 2024

Meh, it's not like I have changed a lot. Here's my changes:

Field Section:

        private static HashSet<string> untranslated = new HashSet<string>();

        private static event Action<object, string> OnUnableToTranslateUGUI;

        private static event Action<object, string> OnUnableToTranslateTextMeshPro;

Method Section:

        public static string Translate(string input, object obj)
        {
            if(string.IsNullOrEmpty(input) || !ContainsJapaneseSymbols(input)) return input;

            // Consider changing this! You have a dictionary, but you iterate instead of making a lookup. Why do you not use the WeakKeyDictionary, you have instead? 
            if (!originalTranslations.Any(x => x.Key.Target == obj)) //check if we don't have the object in the dictionary
            {
                //add to untranslated list
                originalTranslations.Add(new WeakReference(obj), input);
            }

            string translation;
            if (translations.TryGetValue(input, out translation))
            { 
                return translation;
            }
            else if(obj is Text)
            {
                OnUnableToTranslateUGUI?.Invoke( obj, input );
            }
            else if(obj is TMP_Text)
            {
                OnUnableToTranslateTextMeshPro?.Invoke( obj, input );
            }
            
            // Consider changing this! You make a value lookup in a dictionary, which is really bad performance
            if (!untranslated.Contains(input) && !translations.ContainsValue(input))
                untranslated.Add(input);

            return input;
        }

        public static bool ContainsJapaneseSymbols( string text )
        {
           // Japenese regex: [\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]
           foreach( var c in text )
           {
              if( ( c >= '\u3040' && c <= '\u30ff' ) || ( c >= '\uff00' && c <= '\uff9f' ) || ( c >= '\u4e00' && c <= '\u9faf' ) || ( c >= '\u3400' && c <= '\u4dbf' ) )
              {
                 return true;
              }
           }
           return false;
        }

I changed some unrelated code, and added some comments:

  • I changed List untranslated to HashSet untranslated
  • During the Translate method, you use the "originalTranslations" with linq instead of a lookup. This scales really poorly. Consider using a WeakDictionary. I have one of these in my repositories. That is a dictionary I adapted from a weak KEYed and VALUEed dictionary to only be weak KEYed. But you will need to make sure you prune it now and then for collected key references.
  • At the end of the Translate method you make a value lookup in a dictionary, this also scales really poorly. Consider removing it. If you want to make sure you have not already translated consider having a HashSet with translated values where you can look it up in instead. You could also use a Dictionary with it as a key.

from bepisplugins.

 avatar commented on September 28, 2024

Finally figured out what I was doing wrong. :) Testing it now.

from bepisplugins.

 avatar commented on September 28, 2024

Good news, everyone! I managed to make it work.

Two notes:

  • I had to do something fairly awkward though. I had to use an event with a return value, to handle the situation where I call SetText from my own plugin immediately, if it was cached up. That was a bug that was caused by me setting a translated text, and afterwards your plugin setting the untranslated text. :) I could not think of any other way of solving this issue, if none of the two plugins has a references to eachother.
  • You should probably consider hooking into more of the text change events. If I use my plugin alone, it is better at figuring out what needs to be translated. Consider looking into which events I hook into.

I will see if I can make a pull request in a moment.

from bepisplugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.