Example #1
0
        /**
         * @brief Message from chat or IM to agent, whether logged in or not.
         *
         *        Messages should have [[[lc]]] tag on the front as inserted by
         *        ClientToWhatev() and so we know if we need to translate the
         *        message before passing it to the client.
         *
         *        We may get messages without [[[lc]]] (eg, script generated)
         *        in which case we assume they are the default language, then
         *        translate if client is other than the default language.
         *
         *        We may also get messages with [[[--]]] on the front, in which
         *        case we always pass them on without doing any translation.
         *
         *        If scripts generate messages in other than the default language,
         *        they can be prefixed with [[[lc]]] indicating the messages'
         *        actual language.
         *
         * @param agentID  = agent the message is headed to
         * @param langcode = language to translate message to
         * @param showorig = show original text
         * @param finished = what to call when translation is complete
         * @param message  = message to be translated, possibly with [[[lc]]]
         *                   prefix indicating the message's language
         */
        private void WhatevToAgent(string agentID, string langcode, bool showorig, ITranslatorFinished finished, string message)
        {
            // split [[[langcode]]] off front of message
            // if not there, assume it is default language
            string msglc = service.DefLangCode;
            int    i     = message.IndexOf("[[[");
            int    j     = message.IndexOf("]]]");

            if ((i == 0) && (j > 0))
            {
                msglc   = message.Substring(3, j - 3);
                message = message.Substring(j + 3);
            }

            // if message's language matches the agent's language, pass message to agent as is
            // also, no translation if message is marked notranslate or agent is maked notranslate
            // and no translation for null messages
            if ((msglc == langcode) || (msglc == NOTRANSLATE) || (langcode == NOTRANSLATE) || (message.Trim() == ""))
            {
                finished(message);
            }
            else
            {
                // otherwise, start translating then pass translation to agent
                Translate(msglc, langcode, showorig, message, agentID, finished);
            }
        }
Example #2
0
        /**
         * @brief Message from chat or IM to agent, whether logged in or not.
         */
        public void WhatevToAgent(string agentID, ITranslatorFinished finished, string message)
        {
            bool   showorig;
            string langcode = GetAgentLangCode(agentID, out showorig);

            WhatevToAgent(agentID, langcode, showorig, finished, message);
        }
Example #3
0
            /**
             * @Brief Message from client to chat or IM.
             *
             *        All we do is tag the message with the client's language code
             *        by putting [[[lc]]] on the front.  But if the message already
             *        has [[[lc]]] on the front, validate then leave it as is.
             *
             *        WhatevToClient() will strip the tag off and use it to determine
             *        if the message needs to be translated or not when passing it
             *        back out to the other client.
             */
            public void ClientToWhatev(ITranslatorFinished finished, string message, int channel)
            {
                // don't tag messages headed for scripts
                // they're probably something like a button from a menu
                if (channel == PUBLIC_CHANNEL)
                {
                    // if user typed message with a [[[lc]]] prefix,
                    // validate and convert to lower-case 2-letter code
                    int i = message.IndexOf("[[[");
                    int j = message.IndexOf("]]]");
                    if ((i == 0) && (j > 0))
                    {
                        string given = message.Substring(3, j - 3).Trim();
                        if (given != NOTRANSLATE)
                        {
                            string lclo = CheckLangCode(given);
                            if (lclo == null)
                            {
                                client.SendChatMessage("unknown language code " + given,
                                                       (byte)ChatTypeEnum.Owner, Vector3.Zero, "Translator", UUID.Zero,
                                                       UUID.Zero, (byte)ChatSourceType.System, (byte)ChatAudibleLevel.Fully);
                                message = null;  // don't pass bad message to sim
                            }
                            else
                            {
                                message = "[[[" + lclo + message.Substring(j);
                            }
                        }
                    }
                    else
                    {
                        // user didn't give an explicit [[[lc]]] prefix,
                        // use the client's langcode setting to tag message
                        message = "[[[" + langcode + "]]]" + message;
                    }
                }

                finished(message);
            }
Example #4
0
 /**
  * @brief Message from chat or IM to client.
  */
 public void WhatevToClient(ITranslatorFinished finished, string message)
 {
     module.WhatevToAgent(client.AgentId.ToString(), langcode, showorig, finished, message);
 }
Example #5
0
        /**
         * @brief Start translating the message, call finished when done.
         */
        private static void Translate(string srclc, string dstlc, bool showorig, string message, string agentID, ITranslatorFinished finished)
        {
            message = message.Trim();

            // key for the translations dictionary
            string key = srclc + ":" + dstlc + ":" + message;

            string xlation = null;

            lock (queuelock) {
                if (runthread)
                {
                    // see if we already have this translation either done or in progress
                    Translation val;
                    if (translations.TryGetValue(key, out val))
                    {
                        oldtranslations.Remove(val.uselink);
                    }
                    else
                    {
                        // no, queue the translation for processing
                        val               = new Translation();
                        val.agentID       = agentID;
                        val.key           = key;
                        translations[key] = val;
                        translationq.Enqueue(val);
                        numcachedchars += key.Length;
                        Monitor.PulseAll(queuelock);
                    }

                    // make it the newest translation around
                    val.lastuse = DateTime.UtcNow;
                    oldtranslations.AddLast(val.uselink);

                    // if translation in progress, queue 'finished' for processing when done
                    xlation = val.xlation;
                    if (xlation == null)
                    {
                        TranslatorFinished tf = new TranslatorFinished();
                        tf.nextfin     = val.onFinished;
                        tf.finished    = finished;
                        tf.showorig    = showorig;
                        val.onFinished = tf;
                    }
                }
                else
                {
                    // translation thread not running (crashed or whatever)
                    xlation  = "[[[" + srclc + "]]]" + message;
                    showorig = false;
                }
            }

            // if translation completed, call finished right now
            if (xlation != null)
            {
                if (showorig)
                {
                    xlation += "\n[[[" + srclc + "]]]" + message;
                }
                finished(xlation);
            }
        }