Skip to main content

Class CultureDataFormatter

Namespace: SuperOffice.WebApi.Data
Assembly: SuperOffice.WebApi.dll
This is a helper class designed to encode, parse and reformat culturally sensitive data types between an application server without culture knowledge, and a front-end with such knowledge.

Inheritance

objectCultureDataFormatter

Inherited Members

object.ToString(), object.Equals(object), object.Equals(object, object), object.ReferenceEquals(object, object), object.GetHashCode(), object.GetType(), object.MemberwiseClone()

Remarks

<p></p> The problem: The application server does not know the culture of the current client. At the same time, we need to be able to include culturally sensitive data (such as a date) as part of a tooltip or other server-side generated text. Before displaying this text to the user, culturally sensitive elements need to be parsed and reformatted to the correct culture. <p></p> The methods in this class take care of both sides of this process. The Encode methods, such as <xref href=“SuperOffice.WebApi.Data.CultureDataFormatter.EncodeDate(System.DateTime)” data-throw-if-not-resolved=“false”></xref>, take a native data type as input and return a string that contains both a specification of what it is (a date) and the value formatted in the invariant culture. <p></p> The <xref href=“SuperOffice.WebApi.Data.CultureDataFormatter.LocalizeEncoded(System.String)” data-throw-if-not-resolved=“false”></xref> method will take such a formatted element, and by calling on the proper LocalFormat method (such as <xref href=“SuperOffice.WebApi.Data.CultureDataFormatter.LocalFormatDate(System.DateTime)” data-throw-if-not-resolved=“false”></xref>, return it in its correct form according to the current culture settings for the thread.

Fields

BeginTag

Field Value

string

BinaryMarker

Field Value

string

DateMarker

Field Value

string

DateTimeMarker

Field Value

string

DoubleMarker

Field Value

string

EndTag

Field Value

string

IntArrMarker

Field Value

string

IntMarker

Field Value

string

MarkerSeparator

Field Value

char

MoneyMarker

Field Value

string

TimeMarker

Field Value

string

TimeSpanMarker

Field Value

string

Methods

AddColon(string)

Add a colon to a string if it was not already there, strip any whitespace

Parameters

label string String to process

Returns

string Original string, now guaranteed to end in exactly one colon

AddTerminator(string, string)

Add a terminator to a string. The string is first stripped, then the terminator is added at the end with no whitespace.

Parameters

label string String to process terminator string Terminator to add

Returns

string String, now ending with terminator and no white space

CompareEncoded(string, string)

Compare two encoded strings, by parsing them into their underlying data datatype and comparing those

Parameters

x string Formatted string y string Formatted string

Returns

int -1, 0 or 1 depending on the ordering relation between x and y

Encode(object)

Encode a object based on type. Types supported: DateTime, Double, Integer, String and Boolean

Parameters

value object Object to be encoded

Returns

string Encoded object

EncodeBinary(Stream)

Encode a block of binary data, such as an image

Parameters

data Stream DateTime whose date component is to be encoded

Returns

string Encoded date

EncodeBinary(Stream, bool)

Encode a block of binary data, such as an image

Parameters

data Stream Stream be encoded performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded date

EncodeBinaryNoTags(Stream)

Parameters

data Stream

Returns

string

EncodeDate(DateTime)

Encode a date, without a time part

Parameters

dateTime DateTime DateTime whose date component is to be encoded

Returns

string Encoded date

EncodeDate(DateTime, bool)

Encode a date, without a time part. Optionally skip encoding and return an empty string instead.

Parameters

dateTime DateTime DateTime whose date component is to be encoded performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded date

EncodeDateTime(DateTime)

Encode a date, with a time part.

Parameters

dateTime DateTime DateTime whose date and time components are to be encoded

Returns

string Encoded date/time

EncodeDateTime(DateTime, bool)

Encode a date, with a time part. Optionally skip encoding and return an empty string instead.

Parameters

dateTime DateTime DateTime whose date and time components are to be encoded performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded date/time

EncodeDouble(double, int)

Encode a double (decimal value), with the given number of decimals. Note that the full precision is included here, but rounded to the given number of decimals on (re)localization. Also note that currency amounts should use the EncodeMoney method, if the cultural settings for money are to be taken into account (though the currency symbol is NOT used in localization, because we may be displaying non-native currencies, saying $ when it’s actually € or NOK is silly).

Parameters

val double Value to be encoded decimals int Final desired number of decimals

Returns

string Encoded value

EncodeDouble(double, int, bool)

Encode a double (decimal value), with the given number of decimals. Note that the full precision is included here, but rounded to the given number of decimals on (re)localization. Also note that currency amounts should use the EncodeMoney method, if the cultural settings for money are to be taken into account (though the currency symbol is NOT used in localization, because we may be displaying non-native currencies, saying $ when it’s actually € or NOK is silly). Optionally skip encoding and return an empty string instead.

Parameters

val double Value to be encoded decimals int Final desired number of decimals performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded value

EncodeDouble(double)

Encode a double (decimal value). On localization, the default number of decimal digits will be used. Also note that currency amounts should use the EncodeMoney method, if the cultural settings for money are to be taken into account (though the currency symbol is NOT used in localization, because we may be displaying non-native currencies).

Parameters

val double Value to be encoded

Returns

string Encoded value

EncodeDouble(double, bool)

Encode a double (decimal value). On localization, the default number of decimal digits will be used. Also note that currency amounts should use the EncodeMoney method, if the cultural settings for money are to be taken into account (though the currency symbol is NOT used in localization, because we may be displaying non-native currencies). Optionally skip encoding and return an empty string instead.

Parameters

val double Value to be encoded performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded value

EncodeInt(int)

Encode an integer. Integers are usually not culturally sensitive… but who knows.

Parameters

val int Value to encode

Returns

string Encoded value

EncodeInt(int, bool)

Encode an integer. Integers are usually not culturally sensitive… but who knows. Optionally skip encoding and return an empty string instead.

Parameters

val int Value to encode performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded value

EncodeIntArr(params int[])

Parameters

intArr int[]

Returns

string

EncodeMoney(double)

Encode a double, for displaying it as money. The local user settings for currency, EXCEPT the currency symbol, will be used for display. No currency symbol will be display regardless of settings.

Parameters

val double Value to encode

Returns

string Encoded value

EncodeMoney(double, bool)

Encode a double, for displaying it as money. The local user settings for currency, EXCEPT the currency symbol, will be used for display. No currency symbol will be display regardless of settings. Optionally skip encoding and return an empty string instead.

Parameters

val double Value to encode performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded value

EncodeTime(DateTime)

Encode a time, without a date part

Parameters

dateTime DateTime DateTime whose time component is to be encoded

Returns

string Encoded time

EncodeTime(DateTime, bool)

Encode a time, without a date part. Optionally skip encoding and return an empty string instead.

Parameters

dateTime DateTime DateTime whose time component is to be encoded performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded time

EncodeTimeSpan(TimeSpan, bool)

Encode a timespan, encodes total number of seconds Optionally skip encoding and return an empty string instead.

Parameters

timeSpan TimeSpan TimeSpan to be encoded. performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded time

EncodeTimeSpan(int, bool)

Encode a timespan Optionally skip encoding and return an empty string instead.

Parameters

seconds int Seconds to be encoded. performEncode bool If true, the input parameter is encoded to string. If false, an empty string is returned instead. This parameter is implemented to streamline coding of optimized field population in the archive providers, where a mode switch may eliminate the need for string-encoded display values.

Returns

string Encoded time

GetCultureDisplayName(CultureInfo)

Parameters

cultureInfo CultureInfo

Returns

string

GetReadableTimeSpan(int, bool, string, string, string, string, string)

Convert the time/duration/timespan to a “readable” text - either seconds, or day/hour/minute/second

Parameters

seconds int Total seconds to convert mostSignificant bool If true, then only show the most-significant unit; otherwise show d/h/m/s daySignifier string The string to use for the day component suffix hourSignifier string The string to use for the hour component suffix minuteSignifier string The string to use for the minute component suffix secondSignifier string The string to use for the second component suffix zeroSignifier string The suffix string to use when 0 seconds were passed. Empty string might be returned if this parameter was missing

Returns

string Formatted string

GetStringType(string, out string, out int)

Parameters

encodedSomething string val string decimals int

Returns

string

GetSuperOfficeLanguages()

Get all current supported superoffice languages, by superoffice language code (win legacy)

Returns

IEnumerable<string&gt;

LocalFormatDate(DateTime)

Format a date (without time) according to current settings

Parameters

dateTime DateTime DateTime to be formatted

Returns

string Formatted date

LocalFormatDateTime(DateTime)

Format a date with time according to current settings

Parameters

dateTime DateTime Datetime to be formatted

Returns

string Formatted date and time

LocalFormatDouble(double, int)

Format a double, with the given number of decimal digits; otherwise the current settings will be followed

Parameters

val double Double to be formatted decimals int Desired number of decimals

Returns

string Formatted number

LocalFormatInt(int)

Format an integer according to current settings

Parameters

val int int to be formatted

Returns

string Formatted integer

LocalFormatLongDate(DateTime)

Format a long date (without time) according to current settings

Parameters

dateTime DateTime DateTime to be formatted

Returns

string Formatted long date

LocalFormatLongDateTime(DateTime)

Format a long date with a short time according to current settings

Parameters

dateTime DateTime Datetime to be formatted

Returns

string Formatted long date and short time

LocalFormatMoney(double)

Format a double as a currency value, though without a currency symbol. Other local settings for currencies will be followed.

Parameters

val double Double to be formatted

Returns

string Formatted amount

LocalFormatTime(DateTime)

Format a time without date according to current settings

Parameters

dateTime DateTime DateTime to be formatted

Returns

string Formatted time, without date

LocalizeEncoded(string)

Take an encoded value of any type, parse it, and reformat it to its localized (current culture) string form

Parameters

encodedSomething string Output from one of the Encode methods

Returns

string Equivalent, localized value

MapCsLanguageToSuperOfficeLanguage(string)

Convert a language code from cs to SuperOffice language code.

Parameters

csLang string

Returns

string SuperOffice languagecode or string.empty if none found

MapCultureToSuperOfficeLanguage(string)

Converts .net Culture name (“sv-SE”) to SuperOffice language code (“SW”).

Parameters

cultureName string Either a culture name or a SuperOffice language code.

Returns

string A SuperOffice language code. (US, NO, SW, DA, FI, FR, GE, IT, NL, SP, PL, RU, CN, CZ, JP, UK, KO, VI, BR, CH)

Remarks

Converting to a SuperOffice language code twice is harmless. <p></p> If unable to determine a mapping, the default is US (English). <p></p> See also <a href=“http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx”&gt;MSDN on culture codes</a>.

See Also

CultureDataFormatter.MapSuperOfficeLanguageToCulture(string)

MapLCIDToSuperOfficeLanguage(int)

Converts a .net culture LCID to SuperOffice Language Code

Parameters

lcid int The LCID to convert

Returns

string The SuperOffice language code if defined - the two letter iso name if undefined

MapLanguageToCsLanguage(string)

Convert a language code to the language code expected by CS.

Parameters

languageCode string

Returns

string

MapSuperOfficeLanguageToCulture(string)

Converts a SuperOffice language code (“SW”) to a neutral .net culture name (“sv”)

Parameters

superOfficeLanguageCode string SuperOffice language code. (US, NO, SW, DA, FI, FR, GE, IT, NL, SP, PL, RU, CN, CZ, JP, UK, KO, VI, BR, CH)

Returns

string .net neutral Culture name, or empty string (invariant culture) if no mapping found

Remarks

If unable to determine a mapping, the default is US (English). <p></p> See also <a href=“http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx”&gt;MSDN on culture codes</a>.

See Also

CultureDataFormatter.MapCultureToSuperOfficeLanguage(string)

ParseEncoded(string)

Inspect the marker and parse the content, returning a strongly typed object (double, int, byte[], DateTime etc)

Parameters

encodedSomething string

Returns

object

ParseEncodedBinary(string)

Parse/decode an encoded binary data block - either with a marker, or just a Base64 encoded string

Parameters

encodedBinary string

Returns

byte[]

ParseEncodedDate(string)

Parameters

encodedDate string

Returns

DateTime

ParseEncodedDecimal(string)

Parse “123.4” and “[F:123.4]” into 123.4

Parameters

encodedDecimal string “123.4” or “[F:123.4]“

Returns

decimal The value or an exception if not a decimal

ParseEncodedDouble(string)

Parse “123.4” and “[F:123.4]” into 123.4

Parameters

encodedDouble string “123.4” or “[F:123.4]“

Returns

double The value or an exception if not an double

ParseEncodedInt(string)

Parse “123” and “[I:123]” into 123. Parse “abc” into 0. Does not throw, but returns 0.

Parameters

encodedInt string “123” or “[I:123]“

Returns

int The value as int, or 0 if not an integer

ParseEncodedInt(string, bool)

Parse “123” and “[I:123]” into 123.

Parameters

encodedInt string “123” or “[I:123]” strictly bool If true, then throws instead of returning 0 for non-numbers

Returns

int The value or an exception if not an integer

ParseEncodedIntArr(string)

Parameters

encodedIntArr string

Returns

int[]

ParseEncodedTimeSpan(string, bool)

Parameters

encodedTimeSpan string strictly bool

Returns

int

ParseInlineElements(string, string)

Parses a resource string with inline culture-sensitive data in the text, for instance dates… provided they have been encoded using the SuperOffice.CRM.Globalization.CultureDataFormatter in the first place. Multi-language elements are also handled, before any culture-sensitive elements.

Parameters

resourceString string The string to parse cultureName string The cultureName, typically en-US, nb-NO, but also US, NO is accepted

Returns

string Parsed resource string.

ParseInlineMultiLanguageString(string, string, int)

Parses the string for multilang strings and uses <xref href=“SuperOffice.WebApi.Data.CultureDataFormatter.ParseMultiLanguageString(System.String%2cSystem.String)” data-throw-if-not-resolved=“false”></xref> to translate the fragment to a culture specific string part

Parameters

multilangString string The multilanguage string to parse: <pre><code class=“lang-csharp”>US:“Location and size”;GE:“Position und Größe”;NO:“Posisjon og størrelse”</code></pre> cultureName string The cultureName, typically en-US, nb-NO, but also US, NO is accepted startingIndex int The starting index for the parsing

Returns

string All culture specific string parts translated, or original string if there is a problem

ParseMultiLanguageString(string, string)

Returns the string part of the specified culture from the multi language string These strings are typically used in SuperOffice list and description data <example> String example from PrefDesc table: US:“Location and size”;GE:“Position und Größe”;NO:“Posisjon og størrelse” </example>

Parameters

multilangString string The multilanguage string to parse: <pre><code class=“lang-csharp”>US:“Location and size”;GE:“Position und Größe”;NO:“Posisjon og størrelse”</code></pre> cultureName string The cultureName, typically en-US, nb-NO, but also US, NO is accepted

Returns

string Culture specific string part, or original string if there is a problem

Remarks

This version will also accept strings that have non-multilanguage text before and after the multilanguage block, and even multiple multilanguage blocks (provided there is at least one non-conforming character between them), isolating them and parsing each one separately. Non-conforming text is kept in place in the output.

StripColon(string)

Strip a terminating colon from a string, and any whitespace

Parameters

label string String to process

Returns

string String, without colon and without leading/trailing whitespace

StripCountrySpecificsFromLanguageName(string)

Converts “English (Zimbabwe)” into plain “English” by removing anything in parenthesis and stripping spaces.

Parameters

languageName string Human readable language name: “Norsk (Bokmål)” or “Francaise”

Returns

string Simplified language name with initial capital: “Norsk” or “Francaise”

StripTerminator(string, string)

Strip a terminator from a string. Whitespace is first trimmed from both sides of the string, the terminator is removed if present, and any whitespace now at the end of the string is also removed

Parameters

label string String to process terminator string Terminator to remove

Returns

string Original string minus terminator

TryParse(string, out int)

Try to parse a string as an encoded or bare InvariantCulture Integer32

Parameters

encoded string String, either marked with [I: ] or just an integer result int Int value, if return value is true; otherwise 0

Returns

bool true if parsing succeeded

TryParse(string, out int[])

Parameters

encodedIntArr string result int[]

Returns

bool

TryParse(string, out double)

Try to parse a string as an encoded or bare InvariantCulture Double

Parameters

encoded string String, either marked with [F: ] or just a date result double Double value, if return value is true; otherwise 0

Returns

bool true if parsing succeeded

TryParse(string, out DateTime)

Try to parse a string as an encoded or bare InvariantCulture DateTime

Parameters

encoded string String, either marked with [DT: ] or just a datetime result DateTime DateTime value, if return value is true; otherwise <xref href=“System.DateTime.MinValue” data-throw-if-not-resolved=“false”></xref>

Returns

bool true if parsing succeeded

Related topics

CultureDataFormatter