Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Localisation

Bartłomiej Dach edited this pageAug 7, 2023 ·7 revisions

LocalisableString

Localisation inosu-framework revolves around theLocalisableString class, which UI controls and text destinations accept in place ofstring. This page explains the provided methods of interacting withLocalisableString, but further customisation is also possible by implementingILocalisableStringData directly.

LocalisableFormattableString

Represents a string which can be formatted based on the currently selected locale, exposed in two methods and one extension method:

  • LocalisableString.Format, accepting a format string and a list of arguments designed in a similar fashion tostring.Format:

    // Text = string.Format("{0} + {1} = {2}", localisable1, localisable2, localisable3);Text=LocalisableString.Format("{0} + {1} = {2}",localisable1,localisable2,localisable3);
  • LocalisableString.Interpolate, accepting an interpolated string ($"..."):

    // Text = $"{localisable1} + {localisable2} = {localisable3}";Text=LocalisableString.Interpolate($"{localisable1} +{localisable2} ={localisable3}");
  • LocalisableStringExtensions.ToLocalisableString, accepting anIFormattable object (e.g. number types,DateTimes,TimeSpans, etc.) and a format string, in a similar fashion toIFomrattable.ToString:

    // Text = formattable.ToString("N0");Text=formattable.ToLocalisableString("N0");

TranslatableString

Represents a string which can be translated across different languages via a specific "key" that is used to look up on theILocalisationStore associated with the locale.

Text=newTranslatableString("music_string_key","music");stringGet(stringlookup)// ILocalisationStore.Get(string){if(lookup=="music_string_key"){switch(EffectiveCulture.Name){case"en":return"music";case"ja":return"音楽";            ...}}}

In addition,TranslatableStrings support formatting, allowing for the translated strings to specify placeholder items which are replaced with the given arguments list, in a similar fashion tostring.Format/LocalisableString.Format.

Text=newTranslatableString("music_string_key","{0} music",number.ToLocalisableString("0.00%"));stringGet(stringlookup)// ILocalisationStore.Get(string){if(lookup=="music_string_key"){switch(EffectiveCulture.Name){case"en":return"{0} music";case"ja":return"音楽 {0}";            ...}}}

RomanisableString

Represents a unicode string that has a romanised variant, in which can be toggled on/off by theFrameworkSetting.ShowUnicode setting available in the config manager.

Text=newRomanisableString("音楽","ongaku");// FrameworkSetting.ShowUnicode == true: "音楽" is shown// FrameworkSetting.ShowUnicode == false: "ongaku" is shown

CaseTransformableString

Represents a string which accepts aLocalisableString and transforms it to the specified casing, exposed in a set of extension methods which accept eitherLocalisableString orILocalisableStringData (string implementations):

  • LocalisableStringExtensions.ToUpper, which converts all characters of the input string to uppercase, usingTextInfo.ToUpper.
  • LocalisableStringExtensions.ToLower, which converts all characters of the input string to lowercase, usingTextInfo.ToLower.
  • LocalisableStringExtensions.ToTitle, which converts the first character of every word to uppercase while the rest to lowercase, usingTextInfo.ToTitleCase.

It's implemented in a way that allows it to build itself over aLocalisableString or any string implementation:

Text=LocalisableString.Format($"{localisable1} ->{localisable2}").ToUpper();Text=newRomanisableString("音楽","ongaku").ToTitle();...

Custom string implementations

LocalisableString accepts any implementation that confronts to theILocalisableStringData interface, in which it can retrieve the final localised string with.

publicclassCustomLocalisableString:IEquatable<CustomLocalisableString>,ILocalisableStringData{publicstringGetLocalised(LocalisationParametersparameters){return/* localisation logic */;}publicboolEquals(CustomLocalisableStringother){if(ReferenceEquals(null,other))returnfalse;if(ReferenceEquals(this,other))returntrue;return/* equality */;}publicboolEquals(ILocalisableStringDataother)=>otherisCustomLocalisableStringcustom&&Equals(custom);publicoverrideboolEquals(object?obj)=>objisCustomLocalisableStringcustom&&Equals(custom);publicoverrideintGetHashCode()=>/* hash code */;}

LocalisationParameters

String implementations require context to be able to localise the string.LocalisationParameters provide enough context to fit the need for the framework's own string implementations, but it can be built upon for custom string implementations.

This can be achieved by creating aLocalisationManager subclass that overrides theLocalisationParameters factory method and callsUpdateLocalisationParameters to update the parameters:

publicclassCustomLocalisationManager:LocalisationManager{privatereadonlyIBindable<int>custom=newBindableInt();publicCustomLocalisationManager(FrameworkConfigManagerframeworkConfig,CustomConfigManagerconfig):base(frameworkConfig){config.BindWith(CustomSetting.Custom,custom);custom.BindValueChanged(_=>UpdateLocalisationParameters(),true);}protectedoverrideLocalisationParametersCreateLocalisationParameters()=>newCustomLocalisationParameters(base.CreateLocalisationParameters(),custom.Value);protectedclassCustomLocalisationParameters:LocalisationParameters{publicreadonlyintCustom;protectedCustomLocalisationParameters(CustomLocalisationParametersparameters):base(parameters,parameters.Custom){}publicCustomLocalisationParameters(LocalisationParametersparameters,intcustom):base(parameters){Custom=custom;}}}
publicstringGetLocalised(LocalisationParametersparameters){varcustomParameters=(CustomLocalisationParameters)parameters;return/* localisation logic */;}

LocalisableDescriptionAttribute

Represents an attribute for assigning localised description strings to classes/enums, similar toDescriptionAttribute.

Since attributes in general only allow constant input, theLocalisableStrings to use for the attribute must be stored statically in a public field or a property:

publicstaticclassStrings{publicstaticLocalisableStringClass=> ...;publicstaticLocalisableStringEnumOne=> ...;publicstaticLocalisableStringEnumTwo=> ...;publicstaticLocalisableStringEnumThree=> ...;}

The wayLocalisableDescriptionAttribute works is by specifying theType of the class containing theLocalisableString member, followed by the name of said member for lookup:

// [Description("Class")][LocalisableDescription(typeof(Strings),nameof(Strings.Class))]publicclassClassWithLocalisableDescription{}publicenumEnumWithLocalisableDescription{// [Description("One")][LocalisableDescription(typeof(Strings),nameof(Strings.EnumOne))]One,// [Description("Two")][LocalisableDescription(typeof(Strings),nameof(Strings.EnumTwo))]Two,// [Description("Three")][LocalisableDescription(typeof(Strings),nameof(Strings.EnumThree))]Three,}

And in order to retrieve the description, use the extension methodExtensionMethods.GetLocalisableDescription:

// Text = classInstance.GetDescription();Text=classInstance.GetLocalisableDescription();// Text = enumValue.GetDescription();Text=enumValue.GetLocalisableDescription();
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp