@@ -37,7 +37,7 @@ public LocalizationCodeTransformer(string content, ILocalizationFileReader reade
3737
3838 #region Language Key Interfaces
3939
40- public string ToInterfaceCodeText ( string rootNamespace ) => $ """
40+ public string ToInterfaceCodeText ( ) => $ """
4141#nullable enable
4242
4343using global::dotnetCampus.Localizations;
@@ -66,35 +66,37 @@ private string RecursiveConvertLocalizationTreeNodeToKeyInterfaceCode(Localizati
6666 {
6767 if ( x . Item . ValueArgumentTypes . Length is 0 )
6868 {
69- return $ """
69+ return $$ """
7070 /// <summary>
71- /// { ConvertValueToComment ( x . Item . SampleValue ) }
71+ /// {{ ConvertValueToComment ( x . Item . SampleValue ) } }
7272 /// </summary>
73- LocalizedString { x . IdentifierKey } => this.Get0(" { x . Item . Key } ");
73+ LocalizedString {{ x . IdentifierKey }} { get; }
7474""" ;
7575 }
7676 else
7777 {
7878 var genericTypes = string . Join ( ", " , x . Item . ValueArgumentTypes ) ;
79- return $ """
79+ return $$ """
8080 /// <summary>
81- /// { ConvertValueToComment ( x . Item . SampleValue ) }
81+ /// {{ ConvertValueToComment ( x . Item . SampleValue ) } }
8282 /// </summary>
83- LocalizedString<{ genericTypes } > { x . IdentifierKey } => this.Get { x . Item . ValueArgumentTypes . Length } < { genericTypes } >(" { x . Item . Key } ");
83+ LocalizedString<{{ genericTypes }} > {{ x . IdentifierKey }} { get; }
8484""" ;
8585 }
8686 }
8787 else
8888 {
89- return $ " ILocalizedValues_{ identifierKey } { x . IdentifierKey } => (ILocalizedValues_{ identifierKey } )this;";
89+ return $$ """
90+ ILocalizedValues_{{ identifierKey }} {{ x . IdentifierKey }} { get; }
91+ """ ;
9092 }
9193 } ) ;
9294 return $$ """
9395
9496[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
95- public{{ ( depth is 0 ? " partial" : "" ) }} interface ILocalizedValues{{ nodeTypeName }} : ILocalizedStringProvider
97+ public{{ ( depth is 0 ? " partial" : "" ) }} interface ILocalizedValues{{ nodeTypeName }}
9698{
97- {{ string . Join ( "\n " , propertyLines ) }}
99+ {{ string . Join ( "\n \n " , propertyLines ) }}
98100}
99101{{ string . Concat ( node . Children . Select ( x => RecursiveConvertLocalizationTreeNodeToKeyInterfaceCode ( x , depth + 1 ) ) ) }}
100102""" ;
@@ -115,19 +117,95 @@ private string ConvertValueToComment(string? value)
115117
116118 #region Language Value Implementations
117119
118- public string ToImplementationCodeText ( string rootNamespace , string ietfLanguageTag )
120+ public string ToImplementationCodeText ( string typeName ) => $ """
121+ #nullable enable
122+
123+ using global::dotnetCampus.Localizations;
124+
125+ using ILocalizedStringProvider = global::dotnetCampus.Localizations.ILocalizedStringProvider;
126+ using LocalizedString = global::dotnetCampus.Localizations.LocalizedString;
127+
128+ namespace { GeneratorInfo . RootNamespace } ;
129+ { RecursiveConvertLocalizationTreeNodeToKeyImplementationCode ( Tree , 0 , typeName ) }
130+ """ ;
131+
132+ private string RecursiveConvertLocalizationTreeNodeToKeyImplementationCode ( LocalizationTreeNode node , int depth , string typeName )
133+ {
134+ if ( node . Children . Count is 0 )
135+ {
136+ return "" ;
137+ }
138+
139+ var nodeKeyName = depth is 0
140+ ? ""
141+ : "." + string . Join ( "_" , node . IdentifierKey ) ;
142+ var nodeTypeName = depth is 0
143+ ? ""
144+ : "_" + string . Join ( "_" , node . FullIdentifierKey ) ;
145+ var propertyLines = node . Children . Select ( x =>
146+ {
147+ var identifierKey = string . Join ( "_" , x . FullIdentifierKey ) ;
148+ if ( x . Children . Count is 0 )
149+ {
150+ if ( x . Item . ValueArgumentTypes . Length is 0 )
151+ {
152+ return $ """
153+ /// <inheritdoc />
154+ public LocalizedString { x . IdentifierKey } => provider.Get0("{ x . Item . Key } ");
155+ """ ;
156+ }
157+ else
158+ {
159+ var genericTypes = string . Join ( ", " , x . Item . ValueArgumentTypes ) ;
160+ return $ """
161+ /// <inheritdoc />
162+ public LocalizedString<{ genericTypes } > { x . IdentifierKey } => provider.Get{ x . Item . ValueArgumentTypes . Length } <{ genericTypes } >("{ x . Item . Key } ");
163+ """ ;
164+ }
165+ }
166+ else
167+ {
168+ return $$ """
169+ public ILocalizedValues_{{ identifierKey }} {{ x . IdentifierKey }} { get; } = new LocalizedValues_{{ identifierKey }} (provider);
170+ """ ;
171+ }
172+ } ) ;
173+ return $$ """
174+
175+ [global::System.Diagnostics.DebuggerDisplay("[{LocalizedStringProvider.IetfLanguageTag}] {{ typeName }} {{ nodeKeyName }} .???")]
176+ [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
177+ public sealed class LocalizedValues{{ nodeTypeName }} (ILocalizedStringProvider provider) : ILocalizedValues{{ nodeTypeName }}
178+ {
179+ /// <summary>
180+ /// 获取本地化字符串提供器。
181+ /// </summary>
182+ public ILocalizedStringProvider LocalizedStringProvider => provider;
183+
184+ {{ string . Join ( "\n \n " , propertyLines ) }}
185+
186+ /// <summary>
187+ /// 获取非完整本地化字符串键的字符串表示。
188+ /// </summary>
189+ public override string ToString() => "{{ typeName }} {{ nodeKeyName }} .";
190+ }
191+ {{ string . Concat ( node . Children . Select ( x => RecursiveConvertLocalizationTreeNodeToKeyImplementationCode ( x , depth + 1 , typeName ) ) ) }}
192+ """ ;
193+ }
194+
195+ #endregion
196+
197+ #region Language Value Provider
198+
199+ public string ToProviderCodeText ( string rootNamespace , string ietfLanguageTag )
119200 {
120201 var typeName = IetfLanguageTagToIdentifier ( ietfLanguageTag ) ;
121- var template = GeneratorInfo . GetEmbeddedTemplateFile < LocalizationValues > ( ) ;
202+ var template = GeneratorInfo . GetEmbeddedTemplateFile < LocalizedStringProvider > ( ) ;
122203 var code = template . Content
123204 . Replace ( $ "namespace { template . Namespace } ;", $ "namespace { GeneratorInfo . RootNamespace } ;")
124- . Replace ( $ "class { nameof ( LocalizationValues ) } ", $ "class { nameof ( LocalizationValues ) } _{ typeName } ")
125- . Replace (
126- $ " : ILocalizedValues",
127- $ " : ILocalizedValues{ string . Concat ( EnumerateConvertTreeNodeToInterfaceNames ( Tree . Children ) . Select ( x => $ ",\n ILocalizedValues_{ x } ") ) } ")
205+ . Replace ( $ "class { nameof ( LocalizedStringProvider ) } ", $ "class { nameof ( LocalizedStringProvider ) } _{ typeName } ")
128206 . Replace ( """IetfLanguageTag => "default";""" , $ """ IetfLanguageTag => "{ ietfLanguageTag } ";""" ) ;
129- var lines = LocalizationItems . Select ( x => ConvertKeyValueToValueCodeLine ( x . Key , x . Value ) ) ;
130- code = TemplateRegexes . FlagRegex . Replace ( code , string . Concat ( lines ) ) ;
207+ var dictLines = LocalizationItems . Select ( x => ConvertKeyValueToValueCodeLine ( x . Key , x . Value ) ) ;
208+ code = TemplateRegexes . FlagRegex . Replace ( code , string . Concat ( dictLines ) ) ;
131209 return code ;
132210 }
133211
@@ -137,6 +215,11 @@ private string ConvertKeyValueToValueCodeLine(string key, string value)
137215 return $ "\n {{ \" { key } \" , { escapedValue } }},";
138216 }
139217
218+ private string ConvertKeyValueToProperty ( string key , string value )
219+ {
220+ return "" ;
221+ }
222+
140223 private IEnumerable < string > EnumerateConvertTreeNodeToInterfaceNames ( IEnumerable < LocalizationTreeNode > nodes )
141224 {
142225 foreach ( var node in nodes )
0 commit comments