Beispiel #1
0
        public async Task ExtractSourceText()
        {
            if (!Workspace.IsInProject)
            {
                await Workspace.CopyToSourceDirectory().ConfigureAwait(false);
            }
            if (!StringWithCodePage.ReadAllTextAutoDetect(Workspace.SourcePath, out StringWithCodePage sourceText))
            {
                // TODO: UI message
                return;
            }

            string txt = sourceText.Content;

            _config = DotConfig.GetConfiguration(Workspace.SourcePath);
            WriteIntermediates(_config.Matches(txt));
        }
Beispiel #2
0
        public void BuildTranslation()
        {
            string translation = GetAlternativeTranslationIfExists();
            string destPath    = Util.PrecreateDirectory(Workspace.DestinationPath);

            if (!StringWithCodePage.ReadAllTextAutoDetect(Workspace.SourcePath, out StringWithCodePage sourceText))
            {
                File.Copy(Workspace.SourcePath, destPath, true);
                return;
            }

            Encoding destinationEncoding = sourceText.Encoding.CodePage == 932 ? _cp949 : _utf8WithBom;

            using var meta           = new MetadataCsvReader(Workspace.MetadataPath);
            using StreamReader trans = File.OpenText(translation);
            using var source         = new StringReader(sourceText.Content);
            using var dest           = new StreamWriter(File.Create(destPath), destinationEncoding);
            CompileTranslation(meta, trans, source, dest);
        }
Beispiel #3
0
        public static bool ReadAllTextAutoDetect(string path, out StringWithCodePage guessed)
        {
            string[] encodingNames = new string[] {
                "utf-8",
                "shift_jis",
                "ks_c_5601-1987",
                "utf-16",
                "unicodeFFFE",
            };
            EncoderFallback efall = EncoderFallback.ExceptionFallback;
            DecoderFallback dfall = DecoderFallback.ExceptionFallback;

            guessed = null;
            foreach (string name in encodingNames)
            {
                try {
                    var enc = Encoding.GetEncoding(name, efall, dfall);
                    guessed = new StringWithCodePage(File.ReadAllText(path, enc), enc);
                    return(true);
                }
                catch (DecoderFallbackException) { }
            }
            return(false);
        }