コード例 #1
0
ファイル: ResxParser.cs プロジェクト: lothrop/vernacular
        public override IEnumerable<ILocalizationUnit> Parse ()
        {
            foreach (var path in resx_paths) {
                var reader = new ResXResourceReader(path) { UseResXDataNodes = true };

                foreach (DictionaryEntry item in reader) {
                    var name = (string)item.Key;
                    var node = (ResXDataNode)item.Value;
                    var localized_string = new LocalizedString {
                        Name = name,
                        DeveloperComments = node.Comment,
                        UntranslatedSingularValue = name,
                        TranslatedValues = new string [] {(string)node.GetValue(null as ITypeResolutionService) }
                    };

                    for (int i = 0; i < localized_string.TranslatedValues.Length; i++)
                    {
                        var s = localized_string.TranslatedValues [i];
                        s = s.Replace ("\r", "");
                        localized_string.TranslatedValues[i] = s;
                    }

                    yield return localized_string;
                }
            }

            yield break;
        }
コード例 #2
0
        public static IEnumerable<ResourceString> Generate(ResourceIdType resourceIdType, LocalizedString localizedString)
        {
            string [] translated;

            if (localizedString.HasValidTranslations) {
                translated = localizedString.TranslatedValues;
            } else {
                translated = new [] {
                    localizedString.UntranslatedSingularValue,
                    localizedString.UntranslatedPluralValue
                };
            }

            for (int i = 0; i < translated.Length; i++) {
                if (translated [i] == null ||
                    (i == 0 && String.IsNullOrWhiteSpace (localizedString.UntranslatedSingularValue))) {
                    continue;
                }

                yield return new ResourceString {
                    Id = Catalog.GetResourceId (resourceIdType,
                        localizedString.Context, localizedString.UntranslatedSingularValue,
                        localizedString.Gender, i),
                    Untranslated = localizedString.UntranslatedSingularValue,
                    Translated = translated [i]
                };
            }
        }
コード例 #3
0
ファイル: StringAnalyzer.cs プロジェクト: jorik041/vernacular
        private bool Analyze(LocalizedString localizedString)
        {
            var warnings = new List<string> ();

            if (Check (CheckFormatArguments, localizedString) &&
                String.IsNullOrWhiteSpace (localizedString.DeveloperComments)) {
                warnings.Add ("String.Format without developer comment");
            } else if (Check (CheckEmpty, localizedString)) {
                warnings.Add ("Empty or whitespace");
            } else if (Check (CheckIllegalWords, localizedString)) {
                warnings.Add ("Possibly illegal words");
            }

            if (warnings.Count == 0) {
                return false;
            }

            if (localizedString.HasReferences) {
                foreach (var reference in localizedString.References) {
                    Console.WriteLine ("Warning: @{0}", reference);
                }
            } else {
                Console.WriteLine ("Warning: @<unknown source location>");
            }

            foreach (var warning in warnings) {
                Console.WriteLine ("  {0}", warning);
            }

            if (localizedString.DeveloperComments != null) {
                Console.WriteLine ("  Developer Comments  = {0}", localizedString.DeveloperComments);
            }

            if (localizedString.UntranslatedSingularValue != null) {
                Console.WriteLine ("  Singular = {0}", localizedString.UntranslatedSingularValue);
            }

            if (localizedString.UntranslatedPluralValue != null) {
                Console.WriteLine ("  Plural   = {0}", localizedString.UntranslatedPluralValue);
            }

            return true;
        }
コード例 #4
0
ファイル: Generator.cs プロジェクト: lothrop/vernacular
        private bool Merge (LocalizedString localizedString, bool stripMetadata = false)
        {
            for (int i = 0; i < strings.Count; i++) {
                if (strings [i].UntranslatedEquals (localizedString)) {
                    var merged = LocalizedString.Merge (strings [i], localizedString);
                    if (stripMetadata) {
                        merged.StripMetadata ();
                    }
                    strings [i] = merged;
                    return true;
                }
            }

            return false;
        }
コード例 #5
0
ファイル: Generator.cs プロジェクト: lothrop/vernacular
 protected IEnumerable<ResourceString> GetResourceStrings (LocalizedString localizedString)
 {
     return ResourceString.Generate (ResourceIdType, localizedString);
 }
コード例 #6
0
ファイル: StringAnalyzer.cs プロジェクト: jorik041/vernacular
 public void Add(LocalizedString localizedString)
 {
     localized_strings.Add (localizedString);
 }
コード例 #7
0
ファイル: Generator.cs プロジェクト: jorik041/vernacular
 public void Add(LocalizedString localizedString, bool stripMetadata = false)
 {
     if (!Merge (localizedString, stripMetadata: stripMetadata)) {
         if (stripMetadata) {
             localizedString.StripMetadata ();
         }
         strings.Add (localizedString);
     }
 }
コード例 #8
0
        public static LocalizedString Merge(LocalizedString a, LocalizedString b)
        {
            if (!a.UntranslatedEquals (b)) {
                throw new Exception ("Cannot merge two strings with different untranslated values");
            }

            var merged = new LocalizedString {
                Context = a.Context,
                UntranslatedSingularValue = a.UntranslatedSingularValue,
                UntranslatedPluralValue = a.UntranslatedPluralValue,
                Gender = a.Gender,
                StringFormatHint = a.StringFormatHint ?? b.StringFormatHint,
                DeveloperComments = Join (a.DeveloperComments, b.DeveloperComments),
                TranslatorComments = Join (a.TranslatorComments, b.TranslatorComments)
            };

            var a_translated = new List<string> (a.TranslatedValues ?? new string [0]);
            var b_translated = new List<string> (b.TranslatedValues ?? new string [0]);

            if (b_translated.Count >= a_translated.Count) {
                a_translated = b_translated;
            }

            if (a_translated.Count > 0) {
                merged.TranslatedValues = a_translated.ToArray ();
            }

            var a_references = new List<string> (a.References ?? new string [0]);
            var b_references = new List<string> (b.References ?? new string [0]);

            foreach (var reference in b_references) {
                if (!a_references.Contains (reference)) {
                    a_references.Add (reference);
                }
            }

            if (a_references.Count > 0) {
                merged.References = a_references.ToArray ();
            }

            return merged;
        }
コード例 #9
0
ファイル: XamlParser.cs プロジェクト: sam-lippert/vernacular
        private LocalizedString AddReference(LocalizedString localizedString, XmlTextReader reader, string xamlPath)
        {
            if (reader.HasLineInfo ()) {
                localizedString.AddReference (RelativeDocumentUrl (xamlPath), reader.LineNumber);
            }

            return localizedString;
        }
コード例 #10
0
        private LocalizedString CreateLocalizedString(string name, string value, string reference)
        {
            var ret = new LocalizedString {
                Name = name,
                References = new[] { reference }
            };

            if (RetainIds) {
                ret.UntranslatedSingularValue = name;
                ret.TranslatedValues = new string[] { value };
            }
            else {
                ret.UntranslatedSingularValue = value;
            }

            return ret;
        }
コード例 #11
0
        private bool Analyze(LocalizedString localizedString)
        {
            var warnings = new List<string> ();

            if (Check (CheckFormatArguments, localizedString) &&
                String.IsNullOrWhiteSpace (localizedString.DeveloperComments)) {
                warnings.Add ("String.Format without developer comment");
            }

            if (Check (CheckEmpty, localizedString)) {
                warnings.Add ("Empty or whitespace");
            }

            if (Check (CheckIllegalWords, localizedString)) {
                warnings.Add ("Possibly illegal words");
            }

            if (Check (CheckTags, localizedString)) {
                warnings.Add ("Possibly invalid tags");
            }

            string misspelledWord;
            if (CheckSpelling (localizedString.UntranslatedSingularValue, out misspelledWord) ||
                CheckSpelling (localizedString.UntranslatedPluralValue, out misspelledWord)) {
                warnings.Add ("Possibly misspelled word: " + misspelledWord);
            }

            warnings.AddRange (localizedString.Warnings);

            if (warnings.Count == 0) {
                return false;
            }

            ConsoleCrayon.ForegroundColor = ConsoleColor.DarkRed;
            var label = configuration.WarningsAsErrors ? "Error" : "Warning";

            if (localizedString.HasReferences) {
                foreach (var reference in localizedString.References) {
                    Console.WriteLine ("{0}: @{1}", label, reference);
                }
            } else {
                Console.WriteLine ("{0}: @<unknown source location>", label);
            }

            ConsoleCrayon.ForegroundColor = ConsoleColor.DarkYellow;

            foreach (var warning in warnings) {
                Console.WriteLine ("  {0}", warning);
            }

            ConsoleCrayon.Reset ();

            if (localizedString.DeveloperComments != null) {
                Console.WriteLine ("  Developer Comments  = {0}", localizedString.DeveloperComments);
            }

            if (localizedString.UntranslatedSingularValue != null) {
                Console.WriteLine ("  Singular = {0}", localizedString.UntranslatedSingularValue);
            }

            if (localizedString.UntranslatedPluralValue != null) {
                Console.WriteLine ("  Plural   = {0}", localizedString.UntranslatedPluralValue);
            }

            return true;
        }
コード例 #12
0
        public static LocalizedString Merge(LocalizedString a, LocalizedString b)
        {
            if (!a.UntranslatedEquals (b)) {
                throw new Exception ("Cannot merge two strings with different untranslated values");
            }/*
                FIXME: don't enforce gender merging because gender isn't properly handled
                anywhere else. Need to figure out how to generate PO strings that differentiate
                gender... until then, don't enforce this.
                else if (a.Gender != b.Gender) {
                throw new Exception ("Cannot merge two strings with different genders");
            }*/

            var merged = new LocalizedString {
                UntranslatedSingularValue = a.UntranslatedSingularValue,
                UntranslatedPluralValue = a.UntranslatedPluralValue,
                Gender = a.Gender,
                StringFormatHint = a.StringFormatHint ?? b.StringFormatHint,
                DeveloperComments = Join (a.DeveloperComments, b.DeveloperComments),
                TranslatorComments = Join (a.TranslatorComments, b.TranslatorComments)
            };

            var a_translated = new List<string> (a.TranslatedValues ?? new string [0]);
            var b_translated = new List<string> (b.TranslatedValues ?? new string [0]);

            if (b_translated.Count >= a_translated.Count) {
                a_translated = b_translated;
            }

            if (a_translated.Count > 0) {
                merged.TranslatedValues = a_translated.ToArray ();
            }

            var a_references = new List<string> (a.References ?? new string [0]);
            var b_references = new List<string> (b.References ?? new string [0]);

            foreach (var reference in b_references) {
                if (!a_references.Contains (reference)) {
                    a_references.Add (reference);
                }
            }

            if (a_references.Count > 0) {
                merged.References = a_references.ToArray ();
            }

            return merged;
        }
コード例 #13
0
 public bool UntranslatedEquals(LocalizedString other)
 {
     return UntranslatedSingularValue == other.UntranslatedSingularValue &&
         UntranslatedPluralValue == other.UntranslatedPluralValue;
 }
コード例 #14
0
ファイル: PoParser.cs プロジェクト: sam-lippert/vernacular
        private LocalizedString ParsePoMessageUnit(Unit unit)
        {
            var developer_comments_builder = new StringBuilder ();
            var translator_comments_builder = new StringBuilder ();
            var references_builder = new StringBuilder ();
            var flags_builder = new StringBuilder ();
            var translated_values = new List<string> ();
            string untranslated_singular_value = null;
            string untranslated_plural_value = null;
            string context = null;

            foreach (var message in unit.Messages) {
                switch (message.Type) {
                    case MessageType.SingularIdentifier:
                        untranslated_singular_value = message.Value;
                        break;
                    case MessageType.PluralIdentifier:
                        untranslated_plural_value = message.Value;
                        break;
                    case MessageType.SingularString:
                    case MessageType.PluralString:
                        translated_values.Insert (message.PluralOrder, message.Value);
                        break;
                    case MessageType.Context:
                        context = message.Value.Trim ();
                        break;
                }
            }

            foreach (var comment in unit.Comments) {
                switch (comment.Type) {
                    case CommentType.Extracted:
                        developer_comments_builder.Append (comment.Value.Trim ());
                        developer_comments_builder.Append ('\n');
                        break;
                    case CommentType.Translator:
                        translator_comments_builder.Append (comment.Value.Trim ());
                        translator_comments_builder.Append ('\n');
                        break;
                    case CommentType.Reference:
                        references_builder.Append (comment.Value.Trim ());
                        references_builder.Append (' ');
                        break;
                    case CommentType.Flag:
                        flags_builder.Append (comment.Value.Trim ());
                        flags_builder.Append (',');
                        break;
                }
            }

            var developer_comments = developer_comments_builder.ToString ().Trim ();
            var translator_comments = translator_comments_builder.ToString ().Trim ();
            var references = references_builder.ToString ().Trim ();
            var flags = flags_builder.ToString ().Trim ();

            var localized_string = new LocalizedString ();

            if (!String.IsNullOrWhiteSpace (developer_comments)) {
                localized_string.DeveloperComments = developer_comments;
            }

            if (!String.IsNullOrWhiteSpace (translator_comments)) {
                localized_string.DeveloperComments = translator_comments;
            }

            if (!String.IsNullOrWhiteSpace (references)) {
                localized_string.References = references.Split (' ');
            }

            if (!String.IsNullOrWhiteSpace (flags)) {
                foreach (var flag in flags.Split (',')) {
                    if (flag.EndsWith ("-format")) {
                        localized_string.StringFormatHint = flag;
                    }
                }
            }

            if (context != null) {
                var context_lower = context.ToLower ();
                foreach (var gender_context in GenderContexts) {
                    if (context_lower == gender_context.Key) {
                        localized_string.Gender = gender_context.Value;
                        context = null;
                        break;
                    } else if (context.Contains (gender_context.Key)) {
                        localized_string.Gender = gender_context.Value;
                        break;
                    }
                }
            }

            localized_string.Context = context;
            localized_string.UntranslatedSingularValue = untranslated_singular_value;
            localized_string.UntranslatedPluralValue = untranslated_plural_value;

            if (translated_values.Count > 0) {
                localized_string.TranslatedValues = translated_values.ToArray ();
            }

            return localized_string;
        }
コード例 #15
0
ファイル: XamlParser.cs プロジェクト: jorik041/vernacular
        private IEnumerable<LocalizedString> Parse(string xamlPath)
        {
            using (var reader = new XmlTextReader (xamlPath)) {
                while (reader.Read ()) {
                    if (reader.NodeType != XmlNodeType.Element || !reader.HasAttributes) {
                        continue;
                    }

                    var in_app_bar = false;

                    if (reader.NamespaceURI.Contains ("clr-namespace:Microsoft.Phone.Shell")) {
                        var name = reader.Name.Substring (reader.Prefix.Length + 1);
                        in_app_bar = name == "ApplicationBarIconButton" || name == "ApplicationBarMenuItem";
                    }

                    var localized_string = new LocalizedString ();

                    while (reader.MoveToNextAttribute ()) {
                        if (!in_app_bar && !reader.NamespaceURI.Contains ("clr-namespace:Vernacular.Xaml")) {
                            continue;
                        }

                        var name = String.IsNullOrEmpty (reader.Prefix)
                            ? reader.Name
                            : reader.Name.Substring (reader.Prefix.Length + 1);

                        switch (name) {
                            case "Text": // only valid when in_app_bar is true
                            case "Catalog.Message":
                                localized_string.UntranslatedSingularValue = reader.Value;
                                if (reader.HasLineInfo ()) {
                                    localized_string.AddReference (RelativeDocumentUrl (xamlPath), reader.LineNumber);
                                }
                                break;
                            case "Catalog.PluralMessage":
                                localized_string.UntranslatedPluralValue = reader.Value;
                                break;
                            case "Catalog.Comment":
                                localized_string.DeveloperComments = reader.Value;
                                break;
                        }
                    }

                    if (localized_string.IsDefined) {
                        yield return localized_string;
                    }

                    reader.MoveToElement ();
                }
            }
        }
コード例 #16
0
ファイル: XamlParser.cs プロジェクト: sam-lippert/vernacular
        private IEnumerable<LocalizedString> Parse(XmlTextReader reader, string xamlPath)
        {
            while (reader.Read ()) {
                if (reader.NodeType != XmlNodeType.Element || !reader.HasAttributes) {
                    continue;
                }

                var in_app_bar = false;

                if (reader.NamespaceURI.Contains ("clr-namespace:Microsoft.Phone.Shell")) {
                    var name = reader.Name.Substring (reader.Prefix.Length + 1);
                    in_app_bar = name == "ApplicationBarIconButton" || name == "ApplicationBarMenuItem";
                }

                var localized_string = new LocalizedString ();
                var is_binding = false;

                while (reader.MoveToNextAttribute ()) {
                    if (!in_app_bar && !reader.NamespaceURI.Contains ("clr-namespace:Vernacular.Xaml")) {
                        continue;
                    }

                    var name = String.IsNullOrEmpty (reader.Prefix)
                        ? reader.Name
                        : reader.Name.Substring (reader.Prefix.Length + 1);

                    switch (name) {
                        case "Text": // only valid when in_app_bar is true
                        case "Catalog.Message":
                            var value = reader.Value;
                            is_binding = IsBinding (value);
                            localized_string.UntranslatedSingularValue = Unescape (value);
                            AddReference (localized_string, reader, xamlPath);
                            break;
                        case "Catalog.PluralMessage":
                            localized_string.UntranslatedPluralValue = Unescape (reader.Value);
                            break;
                        case "Catalog.Comment":
                            localized_string.DeveloperComments = reader.Value;
                            break;
                        case "Catalog.ToolTip":
                            // Here we want to yield a new string directly since
                            // this could be mixed with other Catalog attributes
                            // on the element (e.g. two separate localized strings
                            // could be returned for the element)
                            yield return AddReference (new LocalizedString {
                                UntranslatedSingularValue = Unescape (reader.Value)
                            }, reader, xamlPath);
                            break;
                    }
                }

                if (localized_string.IsDefined && !is_binding) {
                    yield return localized_string;
                }

                reader.MoveToElement ();
            }
        }
コード例 #17
0
ファイル: MoGenerator.cs プロジェクト: lothrop/vernacular
        string GetContextAndGender (LocalizedString localized_string)
        {
            var context = localized_string.Context;
            if (localized_string.Gender != LanguageGender.Neutral) {
                var add_gender_context = true;
                if (context != null) {
                    var context_lower = context.ToLower ();
                    foreach (var gender_context in PoParser.GenderContexts) {
                        if (context_lower.Contains (gender_context.Key)) {
                            add_gender_context = false;
                            break;
                        }
                    }
                }

                if (add_gender_context) {
                    var gender_context = localized_string.Gender.ToString () + " form";
                    if (String.IsNullOrEmpty (context)) {
                        context = gender_context;
                    } else {
                        context += ", " + gender_context;
                    }
                }
            }
            return context;
        }
コード例 #18
0
ファイル: AssemblyParser.cs プロジェクト: lothrop/vernacular
        private LocalizedString CreateLocalizedString (LanguageGender gender, SequencePoint sequencePoint)
        {
            var localized_string = new LocalizedString { Gender = gender };

            if (sequencePoint != null) {
                localized_string.AddReference (RelativeDocumentUrl (sequencePoint.Document.Url), sequencePoint.StartLine);
            }

            if (StringAnalyzer.CheckFormatArguments (localized_string.UntranslatedSingularValue) ||
                StringAnalyzer.CheckFormatArguments (localized_string.UntranslatedPluralValue)) {
                localized_string.StringFormatHint = "csharp-format";
            }

            return localized_string;
        }
コード例 #19
0
ファイル: StringAnalyzer.cs プロジェクト: jorik041/vernacular
 private bool Check(Func<string, bool> func, LocalizedString localizedString)
 {
     return func (localizedString.UntranslatedSingularValue) || func (localizedString.UntranslatedPluralValue);
 }
コード例 #20
0
 public bool UntranslatedEquals(LocalizedString other)
 {
     return
         Context == other.Context &&
         UntranslatedSingularValue == other.UntranslatedSingularValue &&
         UntranslatedPluralValue == other.UntranslatedPluralValue &&
         Gender == other.Gender;
 }
コード例 #21
0
ファイル: Generator.cs プロジェクト: jorik041/vernacular
        protected IEnumerable<ResourceString> GetResourceStrings(LocalizedString localizedString)
        {
            var translated = localizedString.TranslatedValues;
            if (translated == null) {
                translated = new [] {
                    localizedString.UntranslatedSingularValue,
                    localizedString.UntranslatedPluralValue
                };
            }

            for (int i = 0; i < translated.Length; i++) {
                if (translated [i] == null) {
                    continue;
                }

                var untranslated = i == 0
                    ? localizedString.UntranslatedSingularValue
                    : localizedString.UntranslatedPluralValue;

                yield return new ResourceString {
                    Id = Catalog.GetResourceId (untranslated, localizedString.Gender, i),
                    Untranslated = untranslated,
                    Translated = translated [i]
                };
            }
        }