Conditional String Formatting - C# / MVC. List, String res, ? A : B operator
/*Use Case : Logic to determine what string to use. This is common for localization,
where some words might use the feminine article e.g., la, una and others the masculine article e.g., el, un.
Reuse : Anywhere you need change a string based on some condition.
A. Setup the helper class
B. Define the string resource with parameters
C. Write the conditional, referencing the helper class
For this, assume you have a set list of places in spanish and you need to append el or la based on on the gender of the word.
The string that proceeds the location is "Build One", but either needs to be Construir uno or Construir una depending on the gender of the word.
This code checks the masculine locations list. If the the masculine noun is on the list - uno. If not on the male list it is assumed feminine - una.
It is also assumed that a model - "Model" - has been setup prior for the location element
helper class LocationNameHelper is not covered here either. Assume it is just a helper class for setting capitalization, punctuation etc.
*/
/*helper class*/
Define a list and a method that checks whether a value is in that list
Here, _maleSeriesNames identifies a list of values that some element will be checked against.
method IsLocationDisplayNameMale does the check and is the method that will be called later in the code.
*/
using System;
using System.Collections.Generic;
using Intermark.Framework.Helpers.Web;
namespace MyApp.Helpers
{
public static class SpanishFormatting {
private static readonly List<String> _masculineLocations = new List<string> { "aeropuerto", "banco", "mercado", "estadio", "restaurante", "parque", "museo", "cine", "hogar", "cafe"};
/// <summary>
/// Takes a location name (i.e. spaces and first letter capitalization) and compares it agains a list of known male location names to determine gender.
/// </summary>
/// <param name="locationName">The location name that's gender needs to be determined</param>
/// <returns>True if the location name is found in the list, false otherwise.</returns>
public static bool IsLocationDisplayNameMale(string locationName) {
var formatted = LocationNameHelper.ConvertDisplayNameToRoute(locationName);
return _masculineLocations.Contains(formatted);
}
}
}
//Define the parameterized string in the resource file
<data name="BuildOne" xml:space="preserve">
<value>Construir {0}</value>
</data>
/*Write the conditional, referencing the helper class
Call the String.Format method, reference the parameterized string, then call the IsLocationDisplayNameMale. This method returns a bool -- true or false. the ? : conditional will input into the parameter string a or string b.
*/
<div class="build-actions">
<div class="actions"><a href="/<%: Html.ConvertDisplayNameToRoute(Model.LocationName) %><%: Resources.Strings.Url_Build %>"><%= String.Format(Resources.Strings.BuildOne, SpanishFormatting.IsLocationDisplayNameMale(Model.LocationName) ? "uno" : "una") %></a></div>
</div>