Exemplo n.º 1
0
        public TranslationDefinitions(IResolveManager resolveManager,
                                      ITranslationPrimitives translationPrimitives,
                                      ITranslationUnitOfWork unitOfWork,
                                      TranslationDirection directionDefinition,
                                      IVersioningManager versioningManager,
                                      Type typeToInstantiate = null,
                                      TOut predefinedTarget  = null,
                                      Guid?requestlanguageId = null,
                                      TranslationPolicy translationPolicies = TranslationPolicy.Defaults)
        {
            this.resolveManager            = resolveManager;
            this.translationPrimitives     = translationPrimitives;
            this.directionDefinition       = directionDefinition;
            this.typeToInstantiate         = typeToInstantiate ?? typeof(TOut);
            this.unitOfWork                = unitOfWork;
            this.targetInstance            = predefinedTarget;
            this.requestlanguageIdAssigned = requestlanguageId;
            var cacheManager = resolveManager.Resolve <ICacheManager>();

            this.languageCache         = cacheManager.LanguageCache;
            this.publishingStatusCache = cacheManager.PublishingStatusCache;
            this.versioningManager     = versioningManager;
            this.translationPolicies   = translationPolicies;
            this.entityOutSet          = unitOfWork?.GetSet <TOut>() as IQueryable <TOut>;
            if (this.unitOfWork != null && this.targetInstance != null)
            {
                var cached = unitOfWork.TranslationCloneCache.GetFromCachedSet <TOut>();
                clonedTargetApplied = cached.Any(i => i.ClonedEntity == this.targetInstance);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the translator for this configuration.
        /// </summary>
        private INodePacketTranslator GetConfigurationTranslator(TranslationDirection direction)
        {
            string cacheFile = GetCacheFile();

            try
            {
                if (direction == TranslationDirection.WriteToStream)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
                    return(NodePacketTranslator.GetWriteTranslator(File.Create(cacheFile)));
                }
                else
                {
                    // Not using sharedReadBuffer because this is not a memory stream and so the buffer won't be used anyway.
                    return(NodePacketTranslator.GetReadTranslator(File.OpenRead(cacheFile), null));
                }
            }
            catch (Exception e)
            {
                if (e is DirectoryNotFoundException || e is UnauthorizedAccessException)
                {
                    ErrorUtilities.ThrowInvalidOperation("CacheFileInaccessible", cacheFile, e);
                }

                // UNREACHABLE
                throw;
            }
        }
Exemplo n.º 3
0
        private void SetTranslationDirection(TranslationDirection translationDirection)
        {
            _morseCoderSettings.Direction = translationDirection;

            CurrentTranslationDirection = _morseCoderSettings.Direction;

            var settingsChangedMessage = new SettingsChangedMessage(CurrentTranslationDirection);
            MessengerInstance.Send(settingsChangedMessage);
        }
Exemplo n.º 4
0
        public SettingsViewModel(IMorseCoderSettings morseCoderSettings, INavigationService navigationService)
        {
            _morseCoderSettings = morseCoderSettings;
            _navigationService = navigationService;

            _currentTranslationDirection = morseCoderSettings.Direction;

            MorseToAlphabetCommand = new RelayCommand(MorseToAlphabetCommandAction);
            AlphabetToMorseCommand = new RelayCommand(AlphabetToMorseCommandAction);
            BackNavigationCommand = new RelayCommand(BackNavigationCommandAction);
        }
Exemplo n.º 5
0
    public bool TryResolve(ObjectGeneration obj, TranslationDirection dir, Context context, out APIResult line)
    {
        var get = When(obj, dir);

        if (!get)
        {
            line = default;
            return(false);
        }
        line = Resolver(obj, context);
        return(true);
    }
Exemplo n.º 6
0
    public bool TryGetParameterName(ObjectGeneration obj, TranslationDirection dir, Context context, out APIResult result)
    {
        var get = When(obj, dir);

        if (!get)
        {
            result = default;
            return(false);
        }
        result = this.GetParameterName(obj, context);
        return(true);
    }
        /// <summary>
        /// Tests serializing enums.
        /// </summary>
        public void TestSerializeEnum()
        {
            TranslationDirection value = TranslationDirection.ReadFromStream;

            TranslationHelpers.GetWriteTranslator().TranslateEnum(ref value, (int)value);

            TranslationDirection deserializedValue = TranslationDirection.WriteToStream;

            TranslationHelpers.GetReadTranslator().TranslateEnum(ref deserializedValue, (int)deserializedValue);

            Assert.Equal(value, deserializedValue);
        }
Exemplo n.º 8
0
        private string GetLang(TranslationDirection direction)
        {
            switch (direction)
            {
            case TranslationDirection.RuEng:
                return("ru-en");

            case TranslationDirection.EngRu:
                return("en-ru");

            default:
                throw new ArgumentException(nameof(direction));
            }
        }
Exemplo n.º 9
0
    public bool TryGetPassthrough(ObjectGeneration obj, TranslationDirection dir, Context context, out string result)
    {
        var get = When(obj, dir);

        if (!get)
        {
            result = default;
            return(false);
        }
        var name = this.GetParameterName(obj, context);

        result = $"{name}: {name}";
        return(true);
    }
Exemplo n.º 10
0
    public bool TryGetPassthrough(ObjectGeneration baseGen, ObjectGeneration obj, TranslationDirection dir, Context context, out string result)
    {
        var get = API.When(obj, dir);

        if (!get)
        {
            result = default;
            return(false);
        }
        var name = API.GetParameterName(obj, context);

        if (API.When(baseGen, dir))
        {
            result = $"{name}: {name}";
        }
        else
        {
            result = $"{name}: {DefaultFallback}";
        }
        return(true);
    }
Exemplo n.º 11
0
        public MainViewModel(IMorseCoderSettings morseCoderSettings, INavigationService navigationService)
        {
            _morseCoderSettings = morseCoderSettings;
            _navigationService = navigationService;

            _input = _morseCoderSettings.Input;
            _direction = morseCoderSettings.Direction;

            DotCommand = new RelayCommand(DotCommandAction);
            DashCommand = new RelayCommand(DashCommandAction);
            SpaceCommand = new RelayCommand(SpaceCommandAction);

            AboutNavigateCommand = new RelayCommand(AboutNavigateCommandAction);
            SettingsNavigateCommand = new RelayCommand(SettingsNavigateCommandAction);

            ConfigureTranslator(_direction);

            Translation = _translator.Translate(Input);

            MessengerInstance.Register<SettingsChangedMessage>(this, SettingsChangedMessageHandler);
        }
Exemplo n.º 12
0
        public async Task <YandexAnswer> GetDictionaryAnswerAsync(string text, TranslationDirection direction)
        {
            var answer = new YandexAnswer
            {
                Lang             = GetLang(direction),
                DictionaryAnswer = new Dictionary()
            };

            if (String.IsNullOrWhiteSpace(text))
            {
                answer.Text = "Нет слова(ов) для перевода";
                return(answer);
            }

            var address = String.Concat(_addressDictionary,
                                        "&lang=", answer.Lang, "&text=", text);

            answer = await GetAnswerAsync(address, answer);

            return(answer);
        }
Exemplo n.º 13
0
        public async Task <YandexAnswer> GetTranslateAnswerAsync(string text, TranslationDirection direction)
        {
            var answer = new YandexAnswer
            {
                Lang             = GetLang(direction),
                TranslatorAnswer = new Translator()
            };

            if (String.IsNullOrWhiteSpace(text))
            {
                answer.Text = "Нет текста для перевода";
                return(answer);
            }

            var address = String.Concat(_addressTranslator,
                                        "&lang=", answer.Lang, "&text=", text);

            answer = await GetAnswerAsync(address, answer);

            return(answer);
        }
Exemplo n.º 14
0
 public IEnumerable <CustomMethodAPI> InternalMembers(ObjectGeneration obj, TranslationDirection dir) => Get(dir).CustomAPI.Where((a) => !a.Public).Where(o => o.API.When(obj, dir));
Exemplo n.º 15
0
 public string[] PassArgs(ObjectGeneration obj, TranslationDirection dir, Context lhsContext, Context rhsContext) =>
 PublicMembers(obj, dir)
 .Select(api =>
         CreatePassArgs(obj, api, lhsContext, rhsContext))
 .ToArray();
Exemplo n.º 16
0
 public IEnumerable <APILine> PublicMembers(ObjectGeneration obj, TranslationDirection dir) => Get(dir).IterateAPILines(obj, dir).Where((a) => a.Public).Select((r) => r.API);
Exemplo n.º 17
0
 private MethodAPI Get(TranslationDirection dir) => dir == TranslationDirection.Reader ? ReaderAPI : WriterAPI;
Exemplo n.º 18
0
        private TranslationDefinitions <TIn, TOut> CreateDefinition <TIn, TOut, TInstantiate>(TIn entity,
                                                                                              ITranslationUnitOfWork unitofwork, TranslationDirection translationDirection, TOut predefinedTarget) where TIn : class where TOut : class
        {
            var languageId = requestLanguage ?? languageCache.Get(RequestLanguageCode);
            var definition = new TranslationDefinitions <TIn, TOut>(resolveManager, translationPrimitives, unitofwork, translationDirection, VersioningManager,
                                                                    typeof(TInstantiate), predefinedTarget, languageId, translationPolicies);

            definition.SetTnit(entity);
            var internalAssignement = definition as ITranslationDefinitionAccessor <TIn, TOut>;

            internalAssignement?.SetLanguageInternaly(languageId, languageIsSet);
            return(definition);
        }
Exemplo n.º 19
0
 public bool Applies(ObjectGeneration obj, TranslationDirection dir, Context context)
 {
     return(API?.TryResolve(obj, dir, context, out var line) ?? true);
 }
Exemplo n.º 20
0
 public string[] InternalFallbackArgs(ObjectGeneration obj, TranslationDirection dir, Context context) =>
 InternalMembers(obj, dir).Select(custom =>
                                  CombineResults(
                                      custom.API.GetParameterName(obj, context),
                                      custom.DefaultFallback))
 .ToArray();
Exemplo n.º 21
0
 public IEnumerable <(APIResult API, bool Public)> IterateAPI(ObjectGeneration obj, TranslationDirection dir, Context context, params APILine[] customLines)
 {
     foreach (var item in IterateRawAPILines(obj, dir, customLines))
     {
         if (item.API.TryResolve(obj, dir, context, out var line))
         {
             yield return(line, item.Public);
         }
     }
 }
Exemplo n.º 22
0
 private void ConfigureTranslator(TranslationDirection translationDirection)
 {
     switch (translationDirection)
     {
         case TranslationDirection.AlphabetToMorse:
             _translator = new AlphabetToMorseTranslator();
             BackgroundBrush = new SolidColorBrush(Colors.CadetBlue);
             break;
         case TranslationDirection.MorseToAlphabet:
             _translator = new MorseToAlphabetTranslator();
             BackgroundBrush = new SolidColorBrush(Colors.Crimson);
             break;
     }
 }
Exemplo n.º 23
0
 public IEnumerable <(APILine API, bool Public)> IterateRawAPILines(ObjectGeneration obj, TranslationDirection dir, params APILine[] customLines)
 {
     foreach (var item in MajorAPI)
     {
         yield return(item, true);
     }
     foreach (var item in CustomAPI)
     {
         yield return(item.API, item.Public);
     }
     foreach (var item in customLines)
     {
         if (item == null)
         {
             continue;
         }
         yield return(item, true);
     }
     foreach (var item in OptionalAPI)
     {
         yield return(item, true);
     }
 }
Exemplo n.º 24
0
 public IEnumerable <(APILine API, bool Public)> IterateAPILines(ObjectGeneration obj, TranslationDirection dir, params APILine[] customLines)
 {
     foreach (var item in IterateRawAPILines(obj, dir, customLines))
     {
         if (item.API.When(obj, dir))
         {
             yield return(item);
         }
     }
 }
Exemplo n.º 25
0
 public string[] InternalPassArgs(ObjectGeneration obj, TranslationDirection dir, Context lhsContext, Context rhsContext) =>
 InternalMembers(obj, dir).Select(custom =>
                                  CreatePassArgs(obj, custom.API, lhsContext, rhsContext))
 .ToArray();
Exemplo n.º 26
0
        /// <summary>
        /// Gets the translator for this configuration.
        /// </summary>
        private INodePacketTranslator GetResultsCacheTranslator(int configId, string targetToCache, TranslationDirection direction)
        {
            string cacheFile = GetCacheFile(configId, targetToCache);

            if (direction == TranslationDirection.WriteToStream)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
                if (File.Exists(cacheFile))
                {
                    // If the file already exists, then we have cached this once before.  No need to cache it again since it cannot have changed.
                    return(null);
                }

                return(NodePacketTranslator.GetWriteTranslator(File.Create(cacheFile)));
            }
            else
            {
                return(NodePacketTranslator.GetReadTranslator(File.OpenRead(cacheFile), null));
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Gets the translator for this configuration.
        /// </summary>
        private INodePacketTranslator GetResultsCacheTranslator(int configId, string targetToCache, TranslationDirection direction)
        {
            string cacheFile = GetCacheFile(configId, targetToCache);
            if (direction == TranslationDirection.WriteToStream)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
                if (File.Exists(cacheFile))
                {
                    // If the file already exists, then we have cached this once before.  No need to cache it again since it cannot have changed.
                    return null;
                }

                return NodePacketTranslator.GetWriteTranslator(File.Create(cacheFile));
            }
            else
            {
                return NodePacketTranslator.GetReadTranslator(File.OpenRead(cacheFile), null);
            }
        }
Exemplo n.º 28
0
 public SettingsChangedMessage(TranslationDirection translationDirection)
 {
     TranslationDirection = translationDirection;
 }