3

Is there an easy way (maybe built in solution) to convert TimeSpan to localized string? For example new TimeSpan(3, 5, 0); would be converted to 3 hours, 5minutes (just in polish language).

I can of course create my own extension:

    public static string ConvertToReadable(this TimeSpan timeSpan) {
        int hours = timeSpan.Hours;
        int minutes = timeSpan.Minutes;
        int days = timeSpan.Days;
        if (days > 0) {
            return days + " dni " + hours + " godzin " + minutes + " minut";
        } else {
            return hours + " godzin " + minutes + " minut";
        }
    }

But this gets complicated if i want to have proper grammar involved.

MadBoy
  • 10,824
  • 24
  • 95
  • 156
  • you asked a question that was unanswerable by anyone not speaking polish. If you want someone to take polish grammar in account you will have to tell us what the grammar for this is – Oskar Kjellin Mar 20 '10 at 23:53
  • 1
    Kurresmack I understand that and I didn't intended to get working solution written for me. Thought there would be more general solution to this kind of problem (like with localized DateTime conversion to string). Since there isn't I followed the "harder" way and it's done. You will get the "tick" as your suggestion with code example made me do it the way I did it (posted it as answer too). Paulo got +1 for giving advice too. – MadBoy Mar 20 '10 at 23:59

3 Answers3

2

The easiest way to do this is to put the format string in a localized resource, and translate appropriately for each supported language.

Unfortunately there's no standard way to do such thing.

Nobody seems to agree in what should be the proper way.... :-\

And people like us that write software for multiple languages suffer.

Paulo Santos
  • 11,285
  • 4
  • 39
  • 65
1

I do not think this is possible. What you can do is something like this:

public static string ConvertToReadable(this TimeSpan timeSpan) { 
    return string.Format("{0} {1} {2} {3} {4} {5}",
        timeSpan.Days, (timeSpan.Days > 1 || timeSpan.Days == 0) ? "days" : "day",
        timeSpan.Hours, (timeSpan.Hours > 1 || timeSpan.Hours == 0) ? "hours" : "hour",
        timeSpan.Minutes, (timeSpan.Minutes > 1 || timeSpan.Minutes == 0) ? "minutes" : "minute");
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
1

Here's the code that I've cooked out:

public static string ConvertToReadable(this TimeSpan timeSpan) {
        int hours = timeSpan.Hours;
        int minutes = timeSpan.Minutes;
        int days = timeSpan.Days;
        string hoursType;
        string minutesType;
        string daysType;
        switch (minutes) {
            case 1:
                minutesType = "minuta";
                break;
            case 2:
            case 3:
            case 4:
                minutesType = "minuty";
                break;
            default:
                minutesType = "minut";
                break;
        }
        switch (hours) {
            case 1:
                hoursType = "godzina";
                break;
            case 2:
            case 3:
            case 4:
                hoursType = "godziny";
                break;
            default:
                hoursType = "godzin";
                break;
        }
        switch (days) {
            case 1:
                daysType = "dzień";
                break;
            default:
                daysType = "dni";
                break;
        }


        if (days > 0) {
            return days + " " + daysType + " " + hours + " " + hoursType + " " + minutes + " " + minutesType;
        }
        return hours + " " + hoursType + " " + minutes + " " + minutesType;
    }
MadBoy
  • 10,824
  • 24
  • 95
  • 156