-1

I have the following code that generates an invoice line and includes the month a company has used it's coupons:

invoice.InvoiceLine += CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month.Month) + " (" + usedThisMonth + "), ";

My problem is that companies have a company language (in the DB it's saved under the values 1 or 2).

1 means the Dutch Language, 2 is the French language.

How would I edit my line to let the months appear in the corresponding languages?

I guess I need to start by:

if(invoice.CompanyLanguage == 1)
{
  invoice.InvoiceLine += CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month.Month) + " (" + usedThisMonth + "), ";
}
else
{
    invoice.InvoiceLine += CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month.Month) + " (" + usedThisMonth + "), ";
}

I have only found samples of creating new CultureInfo objects but not how to assign it programmatically. How would my code need to look like?

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
user3127554
  • 521
  • 1
  • 7
  • 28
  • 1
    I realize you are working with an existing code base **but** where possible it is recommended to persist the ISO culture code instead of assigning your own culture identifiers. So the persisted culture code for a user could be `nl-NL` or `fr-BE` for example. This would be preferred because you do not have to add any lookups throughout your code AND the values are universally accepted meaning regardless of the programming language used the persisted value should be understood. – Igor Aug 15 '18 at 14:22
  • @Igor , are you recommending that I should use the ISO-codes instead of integers to save the language of an object in a DB? – user3127554 Aug 15 '18 at 14:24
  • 1
    That is precisely what I am recommending. – Igor Aug 15 '18 at 14:26

1 Answers1

5

Are you asking how to assign a culture to a variable?

int foo = 1; // change as required
var cultureName = foo == 1 ? "nl-NL" : "fr-FR";
var culture = new CultureInfo(cultureName);

Console.WriteLine(culture.DateTimeFormat.GetMonthName(DateTime.Now.Month));

Prints "augustus" for foo = 1 and "août" for all other values of foo.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272