The documentation comes from the Markdown files in the source code, so is always up-to-date but available only in English. Enjoy!
This class is the responsible for localization of Properties
, Types
(singular and plural) and Enums
.
By localizing members and types, instead of plain strings in resource files, we can reuse more aggressively the same localized strings, and ensure consistency.
Simple messages that do not belong to any of this groups are also implemented as enums witch type-name ends with Message
termination. We can use the different enum types for different controls/screens/reports that need to be localized.
The enum DescriptionOptions
determines, for each type, witch elements should be translated.
[Flags]
public enum DescriptionOptions
{
None = 0,
Members = 1,
Description = 2,
PluralDescription = 4,
Gender = 8,
All = Members | Description | PluralDescription | Gender,
}
The the event DefaultDescriptionOptions
(int DescriptionManager
) functions can attached to give default values for DescriptionOptions
.
public static event Func<Type, DescriptionOptions?> DefaultDescriptionOptions
By default this are the rules applied:
Additionally, this behavior can be overridden using DescriptionOptionsAttribute
.
For example ModifiableEntity
has it, and is inherited for all the entities:
[DescriptionOptions(DescriptionOptions.Members | DescriptionOptions.Description)]
public abstract class ModifiableEntity
{
}
Once we know a piece of metadata is localized DescriptionManager
exposes simple extension methods to get the string that should be shown to the user.
Returns the user-friendly string representation of a enum value.
public static string NiceToString(this Enum a)
It tries to find the value in the following order:
NiceName
of the field_
, just replace them by spaceSpacePascal
)Additionally, there's an overload for the common case of calling FormatWith
after NiceToString
public static string NiceToString(this Enum a, params object[] args)
{
return a.NiceToString().FormatWith(args);
}
Example:
public enum RegisterUserMessage
{
[Description("Username '{0}' is already in use"]
Username0IsAlreadyInUse,
}
(...)
RegisterUserMessage.Username0IsAlreadyInUse.NiceToString(userName)
Properties and Types can also be localized, using the same order as NiceToString does.
public static string NiceName(this PropertyInfo pi)
public static string NiceName(this Type type)
Types can also have a plural description, useful for query names and messages ('There are already invoices...')
public static string NicePluralName(this Type type)
A similar order as NiceToString is taken to get the plural name, with some variations:
PluralDescription
attribute for rare cases (i.e. child, children)PluralDescriptionAttribute
can be used to encode the rare cases in the the default assembly language.NaturalLanguageTools.Pluralizers
otherwise.Even if English is fortunate enough not to have grammatical gender, many languages vary articles, pronouns or adjectives depending the gender of a related noun.
public static char? GetGender(this Type type)
A similar order as NiceToString is taken to get the gender, with some variations:
Gender
attribute for rare cases (i.e. child, children)GenderAttribute
can be used to encode the rare cases in the the default assembly language.NaturalLanguageTools.GenderDetectors
otherwise.Once you know the gender (and number) of the principal nouns of a sentence you can use this information to vary a string message that has already been prepared to be polymorphic:
public static string ForGenderAndNumber(this string genderAwareText, char? gender = null, int? number = null)
Example
int count = Database.Query<T>().Count();
"Se ha[n] encontrado [1m:un|1f:una|m:unos|f:unas] {0} eliminad[1m:o|1f:a|m:os|f:as]"
.ForGenderAndNumber( typeof(T).GetGender(), count)
.FormatWith(count == 1 ? typeof(T).NiceName(), typeof(T).NicePluralName());
Signum.Utilities only contains the basic features to make an application localizable, you can write create and translate the XML files yourself but using Translation module in Signum.Extensions it's easier to translate them (with the help of Bing) and synchronize them.
© Signum Software. All Rights Reserved.
Powered by Signum Framework