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

Generate code using language fallback #236

Open
wants to merge 1 commit into
base: v8/dev
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
5 changes: 5 additions & 0 deletions src/Umbraco.ModelsBuilder/Building/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class PropertyModel
/// </summary>
public bool IsIgnored;

/// <summary>
/// Gets a value indicating whether this property allows varying by Culture.
/// </summary>
public bool IsCultureVariant;

/// <summary>
/// Gets the generation errors for the property.
/// </summary>
Expand Down
24 changes: 20 additions & 4 deletions src/Umbraco.ModelsBuilder/Building/TextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,16 @@ private void WriteProperty(StringBuilder sb, TypeModel type, PropertyModel prope
WriteClrType(sb, property.ClrTypeName);
sb.Append(">");
}
sb.AppendFormat("(\"{0}\");\n",
property.Alias);
if (property.IsCultureVariant && Config.RenderLanguageFallback)
{
sb.AppendFormat("(\"{0}\", fallback: Fallback.ToLanguage);\n",
property.Alias);
}
else
{
sb.AppendFormat("(\"{0}\");\n",
property.Alias);
}
}

if (property.Errors != null)
Expand Down Expand Up @@ -377,8 +385,16 @@ private void WriteProperty(StringBuilder sb, TypeModel type, PropertyModel prope
WriteClrType(sb, property.ClrTypeName);
sb.Append(">");
}
sb.AppendFormat("(\"{0}\");\n",
property.Alias);
if (property.IsCultureVariant && Config.RenderLanguageFallback)
{
sb.AppendFormat("(\"{0}\", fallback: Fallback.ToLanguage);\n",
property.Alias);
}
else
{
sb.AppendFormat("(\"{0}\");\n",
property.Alias);
}
}

private static IEnumerable<string> SplitError(string error)
Expand Down
11 changes: 11 additions & 0 deletions src/Umbraco.ModelsBuilder/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public Config()

// default: false
EnableApi = ConfigurationManager.AppSettings[prefix + "EnableApi"].InvariantEquals("true");
RenderLanguageFallback = ConfigurationManager.AppSettings[prefix + "RenderLanguageFallback"].InvariantEquals("true");
AcceptUnsafeModelsDirectory = ConfigurationManager.AppSettings[prefix + "AcceptUnsafeModelsDirectory"].InvariantEquals("true");

// default: true
Expand Down Expand Up @@ -163,6 +164,7 @@ public Config(
bool enable = false,
ModelsMode modelsMode = ModelsMode.Nothing,
bool enableApi = true,
bool renderLanguageFallback = false,
string modelsNamespace = null,
bool enableFactory = true,
LanguageVersion languageVersion = DefaultLanguageVersion,
Expand All @@ -178,6 +180,7 @@ public Config(
ModelsMode = modelsMode;

EnableApi = enableApi;
RenderLanguageFallback = renderLanguageFallback;
ModelsNamespace = string.IsNullOrWhiteSpace(modelsNamespace) ? DefaultModelsNamespace : modelsNamespace;
EnableFactory = enableFactory;
LanguageVersion = languageVersion;
Expand Down Expand Up @@ -250,6 +253,14 @@ internal static string GetModelsDirectory(string root, string config, bool accep
/// </remarks>
public bool EnableApi { get; }

/// <summary>
/// Gets a value indicating whether the generated code adds language fallback on variant properties.
/// </summary>
/// <remarks>
/// <para>Default value is <c>false</c>.</para>
/// </remarks>
public bool RenderLanguageFallback { get; }

/// <summary>
/// Gets a value indicating whether the API is installed.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Umbraco.ModelsBuilder/Umbraco/UmbracoServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ private IList<TypeModel> GetTypes(PublishedItemType itemType, IContentTypeCompos
ClrName = GetClrName(propertyType.Name, propertyType.Alias),

Name = propertyType.Name,
Description = propertyType.Description
Description = propertyType.Description,

IsCultureVariant = propertyType.Variations == ContentVariation.Culture
};

var publishedPropertyType = publishedContentType.GetPropertyType(propertyType.Alias);
Expand Down