public MessageTranslationEntity AddTranslation(string srcLang, string tgtLang, string original, string translated)
 {
     MessageTranslationEntity entity = new MessageTranslationEntity(srcLang, tgtLang, original, translated) { IsBing = false };
     mContext.AddObject(TranslationTable, entity);
     mContext.SaveChangesWithRetries();
     return entity;
 }
 private MessageTranslationEntity[] getAlternativeTranslations(string srcLang, string tgtLang, string original)
 {
     MessageTranslationEntity entity = new MessageTranslationEntity(srcLang, tgtLang, original, "");
     return mClient.GetAlternativeTranslations(entity.PartitionKey);
 }
 public void SaveTranslation(MessageTranslationEntity entity)
 {
     mClient.SaveTranslation(entity);
 }
 public MessageTranslationEntity[] GetAlternativeTranslations(MessageTranslationEntity entity)
 {
     return mClient.GetAlternativeTranslations(entity.PartitionKey);
 }
 public void SaveTranslation(MessageTranslationEntity entity)
 {
     mContext.AddObject(TranslationTable, entity);
     mContext.SaveChangesWithRetries();
 }
 public MessageTranslationEntity[] GetAlternativeTranslations(string partitionKey)
 {
     CloudTableQuery<MessageTranslationEntity> query =
        (from e in mContext.CreateQuery<MessageTranslationEntity>(TranslationTable)
         where e.PartitionKey == partitionKey
         select e).AsTableServiceQuery<MessageTranslationEntity>();
     var list = query.ToList();
     list.Sort();
     MessageTranslationEntity[] ret = new MessageTranslationEntity[Math.Min(list.Count, 5)];
     list.CopyTo(0, ret, 0, ret.Length);
     return ret;
 }
Ejemplo n.º 7
0
        public List<ChatMessageModel> makeMessages(string room, string id, string text)
        {
            var roomRec = mCache.GetRoomEntity(room);
            if (roomRec == null)
                return null;
            var userRec = mCache.GetUserEntity(id);
            if (userRec == null)
                return null;
            var originalMessage = new ChatMessage { Sender = userRec.Id, SenderLanguage = userRec.Language, Text = text };
            List<ChatMessageModel> ret = new List<ChatMessageModel>();

            var list = mCache.GetRoomParticipants(room);

            if (list == null || list.Count() == 0)
                return null;

            foreach (var value in list)
            {
                var receiver = mCache.GetUser(value.Key);
                if (receiver != null)
                {
                    var message = ret.FirstOrDefault(m => m.TgtLanguage == receiver.Language);
                    if (message == null)
                    {
                        if (originalMessage.Sender != receiver.Id && originalMessage.SenderLanguage != receiver.Language)
                        {
                            MessageTranslationEntity entity = new MessageTranslationEntity(originalMessage.SenderLanguage, receiver.Language, text, "");
                            var array = mRepo.GetAlternativeTranslations(entity);
                            if (array != null && array.Length > 0)
                            {
                                originalMessage.TranslatedText = array[0].TgtText;
                            }
                            else
                            {
                                originalMessage.TranslatedText = mBing.Translate(originalMessage.Text, originalMessage.SenderLanguage, receiver.Language);
                                entity.TgtText = originalMessage.TranslatedText;
                                entity.IsBing = true;
                                entity.Rank = 10;
                                mRepo.SaveTranslation(entity);
                            }
                        }
                        else
                            originalMessage.TranslatedText = originalMessage.Text;

                        ret.Add(new ChatMessageModel
                        {
                            Sender = originalMessage.Sender,
                            SrcLanguage = originalMessage.SenderLanguage,
                            TgtLanguage = receiver.Language,
                            SrclText = originalMessage.Text,
                            TgtText = originalMessage.TranslatedText
                        });
                    }
                }
            }
            return ret;
        }