Example #1
0
        internal static LocalizationFile MakeNew(DirectoryInfo directory, Func <string, Id <FileInProject>, ISerializer <LocalizerData> > serializer, Func <FileInfo, bool> pathOk, UpToDateFile.BackEnd backend, DirectoryInfo origin)
        {
            //Create a stream under an available filename
            FileInfo path = null;

            for (int i = 0; path == null; i++)
            {
                path = new FileInfo(directory.FullName + Path.DirectorySeparatorChar + "New Localization " + i + ".loc");
                if (!pathOk(path))
                {
                    path = null;
                }
            }

            LocalizerData data = new LocalizerData();

            using (var file = Util.LoadFileStream(path, FileMode.CreateNew, FileAccess.Write))
            {
            }
            using (var mem = new MemoryStream())
            {
                var id = Id <FileInProject> .New();

                LocalizationFile result = new LocalizationFile(id, mem, DocumentPath.FromPath(path, origin), data, serializer(path.FullName, id), backend); //Make a new localization file for an existing project
                result.File.Writable.Save();
                return(result);
            }
        }
Example #2
0
        /// <param name="context">Context used when localizing to reference current localization</param>
        /// <param name="usedGuids"></param>
        /// <param name="shouldClean"></param>
        /// <param name="shouldExpand"></param>
        /// <param name="pathOk">Path is an acceptable filename for a new localization file</param>
        /// <param name="fileLocationOk">Path is an acceptable location from which to import an existing localization file</param>
        public LocalizationEngine(GetFilePath getFilePath, IEnumerable <Project.TData.LocalizerSetData> sets, ILocalizationContext context, Func <HashSet <Id <LocalizedText> > > usedGuids, Func <string, bool> shouldClean, Func <FileInfo, bool> pathOk, Func <string, bool> fileLocationOk, UpToDateFile.BackEnd backEnd, DirectoryInfo origin)
        {
            m_context   = context;
            m_usedGuids = usedGuids;
            ShouldClean = shouldClean;

            Func <IEnumerable <Tuple <Id <FileInProject>, DocumentPath> >, IEnumerable <ILocalizationFile> > load = files =>
            {
                //return files.Select(file => LocalizationFile.Load(file, MakeSerializer(file.Name), backend));
                var filesAndSerializer = files.Select(f => new { Id = f.Item1, Path = f.Item2, Serializer = MakeSerializer(f.Item2.AbsolutePath, f.Item1) }).ToList();
                return(ParallelEnumerable.Select(filesAndSerializer.AsParallel(), fs => LocalizationFile.Load(fs.Path, fs.Id, fs.Serializer, backEnd)));
            };
            Func <DirectoryInfo, LocalizationFile> makeEmpty = path => LocalizationFile.MakeNew(path, MakeSerializer, pathOk, backEnd, origin);

            m_localizers       = new ProjectElementList <ILocalizationFile>(getFilePath, fileLocationOk.Bottleneck(), load, makeEmpty);
            m_localizationSets = sets.ToHashSet();
        }
Example #3
0
 internal static ILocalizationFile Load(DocumentPath path, Id <FileInProject> id, ISerializer <LocalizerData> serializer, UpToDateFile.BackEnd backend)
 {
     if (path.Exists)
     {
         try
         {
             LocalizerData data;
             using (FileStream file = Util.LoadFileStream(path.AbsolutePath, FileMode.Open, FileAccess.Read))
             {
                 using (MemoryStream m = new MemoryStream((int)file.Length))
                 {
                     file.CopyTo(m);
                     m.Position = 0;
                     XmlLocalization.Deserializer d = new XmlLocalization.Deserializer();
                     data       = d.Read(m);
                     m.Position = 0;
                     LocalizationFile result = new LocalizationFile(id, m, path, data, serializer, backend);
                     return(result);
                 }
             }
         }
         catch (MyFileLoadException e)
         {
             Console.Out.WriteLine(e.Message);
             Console.Out.WriteLine(e.StackTrace);
             Console.Out.WriteLine(e.InnerException.Message);
             Console.Out.WriteLine(e.InnerException.StackTrace);
             MessageBox.Show("File: " + path.AbsolutePath + " could not be accessed");
             return(new MissingLocalizationFile(id, path));
         }
     }
     else
     {
         return(new MissingLocalizationFile(id, path));
     }
 }