title: How to localize or string format a list of words with commas and conjunctions in C#?
body: <p>In C# I use resource strings and <code>string.Format</code> for localization. Sometimes I need to generate a message from an arbitrarily-sized list of terms. (Example: "You must select one of Dog, Cat, or Monkey" from list ["Dog", "Cat", "Monkey"])</p>
  
  <p>I can write code to produce that message, but it undermines the <strong>flexibility</strong> of the format strings (the ability for translators to rearrange where arguments show up in a format string) and their <strong>clarity</strong> (example: "You must select one of {0}") . </p>
  
  <p>What better can I do in my code and resource files to avoid those drawbacks?</p>
  
  <p>Example of current code:</p>
  
  <pre><code>// assuming all string literals are in a Resource file somewhere...
  
  string message = string.Format(
    "You must select one of {0}.", 
    Conjoin(new []{ "Dog", "Cat", "Monkey" }));
    // produces "You must select one of Dog, Cat, or Monkey
  
  string Conjoin(string[] items)
  {
    if (items.Length == 1) return items[0];
    if (items.Length == 2) return string.Format("{0} or {1}", items[0], items[1]);
    if (items.Length &gt;= 3)
    {
      string result = items[0];
      for (int i = 1; i &lt; items.Length - 1; i++)
      {
        result = string.Format("{0}, {1}", result, items[i]);
      }
      result = string.Format("{0}, or {1}", result, items[items.Length - 1]);
      return result;
    }
    return "";
  }
  </code></pre>