Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry: new resx-template option #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Vernacular.Tool/Vernacular.Generators/ResourceString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public struct ResourceString
public string Untranslated { get; set; }
public string Translated { get; set; }

public static bool UseNamesAsIds { get; set; }

public string SortKey {
get {
// We want to chop off the Vernacular_P0_M_ style
Expand All @@ -46,6 +48,8 @@ public string SortKey {
}
}



public static IEnumerable<ResourceString> Generate (ResourceIdType resourceIdType, LocalizedString localizedString)
{
string [] translated;
Expand All @@ -65,13 +69,16 @@ public static IEnumerable<ResourceString> Generate (ResourceIdType resourceIdTyp
continue;
}

yield return new ResourceString {
Id = Catalog.GetResourceId (resourceIdType,
localizedString.Context, localizedString.UntranslatedSingularValue,
localizedString.Gender, i),
Untranslated = localizedString.UntranslatedSingularValue,
Translated = translated [i]
};
yield return new ResourceString
{
Id = UseNamesAsIds
? localizedString.Name
: Catalog.GetResourceId(resourceIdType, localizedString.Context,
localizedString.UntranslatedSingularValue,
localizedString.Gender, i),
Untranslated = localizedString.UntranslatedSingularValue,
Translated = translated[i]
};
}
}
}
Expand Down
32 changes: 30 additions & 2 deletions Vernacular.Tool/Vernacular.Tool/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
using System;
using System.IO;
using System.Collections.Generic;

using System.Linq;
using Mono.Options;

using Vernacular.Parsers;
Expand All @@ -49,6 +49,7 @@ public static int Main (string[] args)
string android_input_strings_xml = null;
string android_output_strings_xml = null;
string analyer_config_path = null;
string resx_template_path = null;
LocalizationMetadata metadata = null;
bool generate_pot = false;
bool exclude_po_header = false;
Expand Down Expand Up @@ -80,6 +81,7 @@ public static int Main (string[] args)
"for preserving hand-maintained string resources", v => android_input_strings_xml = v },
{ "android-output-strings-xml=", "Output file of localized Android Strings.xml " +
"for preserving hand-maintained string resources", v => android_output_strings_xml = v },
{ "resx-template=", "Use Ids from the resx template", v => resx_template_path = v},
{ "pot", v => generate_pot = v != null },
{ "exclude-po-header", v => exclude_po_header = v != null },
{ "l|log", "Display logging", v => log = v != null },
Expand Down Expand Up @@ -145,6 +147,14 @@ public static int Main (string[] args)

return 0;
}

if (resx_template_path != null && !(generator is ResxGenerator)) {
throw new OptionException ("resx-template require the generator to be resx", "resx-template");
}

if(resx_template_path != null) {
ResourceString.UseNamesAsIds = true;
}
} catch (OptionException e) {
Console.WriteLine ("vernacular: {0}", e.Message);
Console.WriteLine ("Try `vernacular --help` for more information.");
Expand Down Expand Up @@ -197,10 +207,28 @@ public static int Main (string[] args)
generator.Add (metadata);
}

List<ILocalizationUnit> id_replacements = null;
if (resx_template_path != null) {
var resx_template_parser = new ResxParser ();
resx_template_parser.Add (resx_template_path);
id_replacements = resx_template_parser.Parse().ToList();
}

foreach (var localization_unit in parser.Parse ()) {
var localized_string = localization_unit as LocalizedString;
if (id_replacements != null && localized_string!=null)
{
var replacement = (from lu in id_replacements
where
(lu as LocalizedString).UntranslatedSingularValue ==
localized_string.UntranslatedSingularValue
select lu).FirstOrDefault();
if (replacement == null)
continue;
localized_string.Name = (replacement as LocalizedString).Name;
}
generator.Add (localization_unit);

var localized_string = localization_unit as LocalizedString;
if (analyzer != null) {
analyzer.Add (localized_string);
}
Expand Down