Beispiel #1
0
        void TranslationPairsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Remove:
            {
                TranslationPair deletedTranslationPair = (TranslationPair)e.OldItems[e.OldStartingIndex];

                //remove items from other collections
                foreach (TranslationFile translationFile in _controller.TranslationFiles)
                {
                    TranslationPair toBeRemovedTransPair = translationFile.TranslationPairs.FirstOrDefault(tp => tp.Key == deletedTranslationPair.Key);
                    if (toBeRemovedTransPair != null)
                    {
                        translationFile.TranslationPairs.Remove(toBeRemovedTransPair);
                    }
                }
            }
            break;

            case NotifyCollectionChangedAction.Add:
            {
                TranslationPair addedTranslationPair = (TranslationPair)e.NewItems[e.NewStartingIndex];

                //remove items from other collections
                foreach (TranslationFile translationFile in _controller.TranslationFiles)
                {
                    if (translationFile.TranslationPairs.All(tp => tp.Key != addedTranslationPair.Key))
                    {
                        translationFile.TranslationPairs.Add(new TranslationPair {
                                Generated = true, Key = addedTranslationPair.Key, Value = "", Score = 0
                            });
                    }
                }
            }
            break;
            }
        }
        public void RefreshScores()
        {
            //get unique translation keys of all files
            Dictionary <string, Tuple <string, string> > uniqueTranslationKeys = new Dictionary <string, Tuple <string, string> >();

            foreach (TranslationFile translationFile in TranslationFiles)
            {
                foreach (TranslationPair translationPair in translationFile.TranslationPairs)
                {
                    if (!uniqueTranslationKeys.ContainsKey(translationPair.Key))
                    {
                        uniqueTranslationKeys.Add(translationPair.Key, new Tuple <string, string>(translationFile.Name, translationPair.Value));
                    }
                }
            }

            //determine score for every key and add missing keys to respective files
            foreach (KeyValuePair <string, Tuple <string, string> > uniqueTranslationPair in uniqueTranslationKeys)
            {
                double score = (double)TranslationFiles.Count(tf => tf.TranslationPairs.Any(tp => tp.Key == uniqueTranslationPair.Key && !string.IsNullOrWhiteSpace(tp.Value))) / TranslationFiles.Count();
                foreach (TranslationFile translationFile in TranslationFiles)
                {
                    TranslationPair translationPair = translationFile.TranslationPairs.FirstOrDefault(tp => tp.Key == uniqueTranslationPair.Key);
                    if (translationPair == null)
                    {
                        //_languageServiceClient.GetAppIdToken(APP_ID, );
                        //string autoTranslateValue = _languageServiceClient.Translate(
                        //	"Bearer" + " " + APP_ID,
                        //	uniqueTranslationPair.Value.Item2,
                        //	uniqueTranslationPair.Value.Item1,
                        //	translationFile.Name,
                        //	"text/plain",
                        //	"general"
                        //	);
                        translationFile.TranslationPairs.Add(new TranslationPair {
                            Key = uniqueTranslationPair.Key, Value = "", Score = 0
                        });
                    }
                    else if (string.IsNullOrWhiteSpace(translationPair.Value))
                    {
                        translationPair.Score = 0;
                        //translationPair.Value = _languageServiceClient.Translate(
                        //	"Bearer" + " " + APP_ID,
                        //	uniqueTranslationPair.Value.Item2,
                        //	uniqueTranslationPair.Value.Item1,
                        //	translationFile.Name,
                        //	"text/plain",
                        //	"general"
                        //	);
                    }
                    else
                    {
                        translationPair.Score = score;
                    }
                }
            }

            foreach (TranslationFile translationFile in TranslationFiles)
            {
                translationFile.TranslationPairs = new ObservableCollection <TranslationPair>(translationFile.TranslationPairs.OrderBy(tp => tp.Score).ThenBy(tp => tp.Key));
            }
        }