├── .editorconfig ├── .gitignore ├── AvaloniaExtensions.sln ├── Directory.Build.props ├── LICENSE ├── README.md ├── README.zh-CN.md ├── avalonia-using-xml-files-for-internationalization.md ├── doc └── imgs │ ├── ResourceConfig.png │ └── ResourceDir.png ├── logo.ico ├── logo.png └── src ├── Avalonia.XmlTranslator.Demo ├── App.axaml ├── App.axaml.cs ├── Avalonia.XmlTranslator.Demo.csproj ├── I18n │ ├── Language.cs │ ├── Language.tt │ ├── en-US.xml │ ├── ja-JP.xml │ ├── zh-CN.xml │ └── zh-Hant.xml ├── Program.cs ├── Roots.xml ├── ViewModels │ ├── MainWindowViewModel.cs │ └── ViewModelBase.cs ├── Views │ ├── MainWindow.axaml │ └── MainWindow.axaml.cs └── app.manifest ├── AvaloniaExtensions.Axaml.Demo ├── App.axaml ├── App.axaml.cs ├── Assets │ ├── cloud.png │ ├── logo.ico │ └── logo.png ├── AvaloniaExtensions.Axaml.Demo.csproj ├── I18n │ ├── Language.cs │ ├── Language.tt │ ├── Resources.Designer.cs │ ├── Resources.ja-JP.resx │ ├── Resources.resx │ ├── Resources.zh-CN.resx │ └── Resources.zh-Hant.resx ├── Models │ ├── AlarmStatus.cs │ └── RunStatusKind.cs ├── Program.cs ├── Styles │ └── Brushes.axaml ├── ViewLocator.cs ├── ViewModels │ ├── MainWindowViewModel.cs │ └── ViewModelBase.cs ├── Views │ ├── MainWindow.axaml │ └── MainWindow.axaml.cs └── app.manifest ├── AvaloniaExtensions.Axaml ├── AssemblyInfo.cs ├── AvaloniaExtensions.Axaml.csproj ├── Converters │ ├── EnumToBooleanConverter.cs │ ├── I18n │ │ ├── I18nConverter.cs │ │ ├── I18nKeyConverter.cs │ │ └── I18nValueConverter.cs │ ├── If │ │ ├── IfConditionConverter.cs │ │ └── IfConverter.cs │ └── IsCollectionEmptyConverter.cs ├── ExtensionMethods │ └── ObjectExtensions.cs └── Markup │ ├── Constants.cs │ ├── I18n │ ├── ArgCollection.cs │ ├── I18nBinding.cs │ ├── I18nExtension.cs │ └── I18nManager.cs │ ├── If │ ├── IfBinding.cs │ └── IfExtension.cs │ └── MultiBindingExtensionBase.cs ├── AvaloniaExtensions.Tests ├── AvaloniaExtensions.Tests.csproj ├── LanguageXmlReadTest.cs ├── MSTestSettings.cs └── en-US.xml └── AvaloniaXmlTranslator ├── AssemblyInfo.cs ├── AvaloniaXmlTranslator.csproj ├── Consts.cs ├── Converters ├── I18nConverter.cs ├── I18nKeyConverter.cs └── I18nValueConverter.cs ├── I18nManager.cs ├── Markup ├── ArgCollection.cs ├── I18nBinding.cs └── I18nExtension.cs ├── Models └── LocalizationLanguage.cs └── MultiBindingExtensionBase.cs /.editorconfig: -------------------------------------------------------------------------------- 1 | # To learn more about .editorconfig see https://aka.ms/editorconfigdocs 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Default settings: 7 | # A newline ending every file 8 | # Use 4 spaces as indentation 9 | [*] 10 | insert_final_newline = true 11 | indent_style = space 12 | indent_size = 4 13 | trim_trailing_whitespace = true 14 | 15 | [*.json] 16 | indent_size = 2 17 | 18 | [*.{cs,xaml}] 19 | charset = utf-8 20 | end_of_line = crlf 21 | 22 | # C# files 23 | [*.cs] 24 | # New line preferences 25 | csharp_new_line_before_open_brace = all 26 | csharp_new_line_before_else = true 27 | csharp_new_line_before_catch = true 28 | csharp_new_line_before_finally = true 29 | csharp_new_line_before_members_in_object_initializers = true 30 | csharp_new_line_before_members_in_anonymous_types = true 31 | csharp_new_line_between_query_expression_clauses = true 32 | 33 | # Indentation preferences 34 | csharp_indent_block_contents = true 35 | csharp_indent_braces = false 36 | csharp_indent_case_contents = true 37 | csharp_indent_case_contents_when_block = true 38 | csharp_indent_switch_labels = true 39 | csharp_indent_labels = one_less_than_current 40 | 41 | # Parentheses preferences 42 | dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary : silent 43 | dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary : silent 44 | dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary : silent 45 | dotnet_style_parentheses_in_other_operators = never_if_unnecessary : silent 46 | 47 | # Modifier preferences 48 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async : suggestion 49 | dotnet_style_require_accessibility_modifiers = for_non_interface_members : warning 50 | 51 | # avoid this. unless absolutely necessary 52 | dotnet_style_qualification_for_field = false : suggestion 53 | dotnet_style_qualification_for_property = false : suggestion 54 | dotnet_style_qualification_for_method = false : suggestion 55 | dotnet_style_qualification_for_event = false : suggestion 56 | 57 | # Types: use keywords instead of BCL types, and permit var only when the type is clear 58 | csharp_style_var_for_built_in_types = false : suggestion 59 | csharp_style_var_when_type_is_apparent = false : none 60 | csharp_style_var_elsewhere = false : suggestion 61 | dotnet_style_predefined_type_for_locals_parameters_members = true : suggestion 62 | dotnet_style_predefined_type_for_member_access = true : suggestion 63 | 64 | # Naming Conventions 65 | 66 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case 67 | 68 | dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case 69 | dotnet_naming_style.camel_case_underscore_style.required_prefix = _ 70 | 71 | dotnet_naming_style.interfaces_style.capitalization = pascal_case 72 | dotnet_naming_style.interfaces_style.required_prefix = I 73 | 74 | # name all constant fields using PascalCase 75 | dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion 76 | dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields 77 | dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style 78 | dotnet_naming_symbols.constant_fields.applicable_kinds = field 79 | dotnet_naming_symbols.constant_fields.required_modifiers = const 80 | 81 | # static fields using PascalCase 82 | dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion 83 | dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields 84 | dotnet_naming_rule.static_fields_should_have_prefix.style = pascal_case_style 85 | dotnet_naming_symbols.static_fields.applicable_kinds = field 86 | dotnet_naming_symbols.static_fields.required_modifiers = static 87 | dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected 88 | 89 | # internal and private fields should be _camelCase 90 | dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion 91 | dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields 92 | dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style 93 | dotnet_naming_symbols.private_internal_fields.applicable_kinds = field 94 | dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal 95 | 96 | # interfaces must have an I prefix 97 | dotnet_naming_rule.interfaces_must_be_interfaces_style.severity = suggestion 98 | dotnet_naming_rule.interfaces_must_be_interfaces_style.symbols = interfaces 99 | dotnet_naming_rule.interfaces_must_be_interfaces_style.style = interfaces_style 100 | dotnet_naming_symbols.interfaces.applicable_kinds = interface 101 | dotnet_naming_symbols.interfaces.applicable_accessibilities = * 102 | 103 | # Code style defaults 104 | csharp_using_directive_placement = outside_namespace : suggestion 105 | dotnet_sort_system_directives_first = true 106 | csharp_prefer_braces = true : suggestion 107 | csharp_preserve_single_line_blocks = true : none 108 | csharp_preserve_single_line_statements = false : none 109 | csharp_prefer_static_local_function = true : suggestion 110 | csharp_prefer_simple_using_statement = false : none 111 | csharp_style_prefer_switch_expression = true : suggestion 112 | 113 | # Code quality 114 | dotnet_style_readonly_field = true : suggestion 115 | dotnet_code_quality_unused_parameters = non_public : suggestion 116 | 117 | # Expression-level preferences 118 | dotnet_style_object_initializer = true : suggestion 119 | dotnet_style_collection_initializer = true : suggestion 120 | dotnet_style_explicit_tuple_names = true : suggestion 121 | dotnet_style_coalesce_expression = true : suggestion 122 | dotnet_style_null_propagation = true : suggestion 123 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true : suggestion 124 | dotnet_style_prefer_inferred_tuple_names = true : suggestion 125 | dotnet_style_prefer_inferred_anonymous_type_member_names = true : suggestion 126 | dotnet_style_prefer_auto_properties = true : suggestion 127 | dotnet_style_prefer_conditional_expression_over_assignment = true : refactoring 128 | dotnet_style_prefer_conditional_expression_over_return = true : refactoring 129 | csharp_prefer_simple_default_expression = true : suggestion 130 | csharp_style_deconstructed_variable_declaration = true : suggestion 131 | 132 | # Expression-bodied members 133 | csharp_style_expression_bodied_methods = true : refactoring 134 | csharp_style_expression_bodied_constructors = true : refactoring 135 | csharp_style_expression_bodied_operators = true : refactoring 136 | csharp_style_expression_bodied_properties = true : refactoring 137 | csharp_style_expression_bodied_indexers = true : refactoring 138 | csharp_style_expression_bodied_accessors = true : refactoring 139 | csharp_style_expression_bodied_lambdas = true : refactoring 140 | csharp_style_expression_bodied_local_functions = true : refactoring 141 | 142 | # Pattern matching 143 | csharp_style_pattern_matching_over_is_with_cast_check = true : suggestion 144 | csharp_style_pattern_matching_over_as_with_null_check = true : suggestion 145 | csharp_style_inlined_variable_declaration = true : suggestion 146 | 147 | # Null checking preferences 148 | csharp_style_throw_expression = true : suggestion 149 | csharp_style_conditional_delegate_call = true : suggestion 150 | 151 | # Other features 152 | csharp_style_prefer_index_operator = false : none 153 | csharp_style_prefer_range_operator = false : none 154 | csharp_style_pattern_local_over_anonymous_function = false : none 155 | 156 | # Space preferences 157 | csharp_space_after_cast = false 158 | csharp_space_after_colon_in_inheritance_clause = true 159 | csharp_space_after_comma = true 160 | csharp_space_after_dot = false 161 | csharp_space_after_keywords_in_control_flow_statements = true 162 | csharp_space_after_semicolon_in_for_statement = true 163 | csharp_space_around_binary_operators = before_and_after 164 | csharp_space_around_declaration_statements = do_not_ignore 165 | csharp_space_before_colon_in_inheritance_clause = true 166 | csharp_space_before_comma = false 167 | csharp_space_before_dot = false 168 | csharp_space_before_open_square_brackets = false 169 | csharp_space_before_semicolon_in_for_statement = false 170 | csharp_space_between_empty_square_brackets = false 171 | csharp_space_between_method_call_empty_parameter_list_parentheses = false 172 | csharp_space_between_method_call_name_and_opening_parenthesis = false 173 | csharp_space_between_method_call_parameter_list_parentheses = false 174 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false 175 | csharp_space_between_method_declaration_name_and_open_parenthesis = false 176 | csharp_space_between_method_declaration_parameter_list_parentheses = false 177 | csharp_space_between_parentheses = false 178 | csharp_space_between_square_brackets = false 179 | 180 | # Analyzers 181 | dotnet_code_quality.ca1802.api_surface = private, internal 182 | 183 | # C++ Files 184 | [*.{cpp,h,in}] 185 | curly_bracket_next_line = true 186 | indent_brace_style = Allman 187 | 188 | # Xml project files 189 | [*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}] 190 | indent_size = 2 191 | 192 | # Xml build files 193 | [*.builds] 194 | indent_size = 2 195 | 196 | # Xml files 197 | [*.{xml,stylecop,resx,ruleset}] 198 | indent_size = 2 199 | 200 | # Xml config files 201 | [*.{props,targets,config,nuspec}] 202 | indent_size = 2 203 | 204 | # Shell scripts 205 | [*.sh] 206 | end_of_line = lf 207 | [*.{cmd, bat}] 208 | end_of_line = crlf 209 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /AvaloniaExtensions.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.12.35209.166 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{86C45239-6676-46FA-8FAD-4093EAD45427}" 7 | ProjectSection(SolutionItems) = preProject 8 | .editorconfig = .editorconfig 9 | .gitignore = .gitignore 10 | Directory.Build.props = Directory.Build.props 11 | LICENSE = LICENSE 12 | README.md = README.md 13 | README.zh-CN.md = README.zh-CN.md 14 | EndProjectSection 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E20B2331-FE69-414C-B706-11EF41B09D8B}" 17 | EndProject 18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaExtensions.Axaml", "src\AvaloniaExtensions.Axaml\AvaloniaExtensions.Axaml.csproj", "{FBEBA881-CA18-4B65-9F32-D9C0BFF9AD7F}" 19 | EndProject 20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{07FEB8AC-F72D-469E-8AE6-A5518919D2FD}" 21 | EndProject 22 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaExtensions.Axaml.Demo", "src\AvaloniaExtensions.Axaml.Demo\AvaloniaExtensions.Axaml.Demo.csproj", "{9354640E-E0A0-43DD-9CFC-FDDF624305DA}" 23 | EndProject 24 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avalonia.XmlTranslator.Demo", "src\Avalonia.XmlTranslator.Demo\Avalonia.XmlTranslator.Demo.csproj", "{72C8A993-523D-458F-9F1B-9487CE99222B}" 25 | EndProject 26 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaExtensions.Tests", "src\AvaloniaExtensions.Tests\AvaloniaExtensions.Tests.csproj", "{7360E627-D807-4CBD-B258-56E0BD9701C6}" 27 | EndProject 28 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaXmlTranslator", "src\AvaloniaXmlTranslator\AvaloniaXmlTranslator.csproj", "{848856E2-00C2-0F3D-6CA5-FFDF0D0FB3BE}" 29 | EndProject 30 | Global 31 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 32 | Debug|Any CPU = Debug|Any CPU 33 | Release|Any CPU = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 36 | {FBEBA881-CA18-4B65-9F32-D9C0BFF9AD7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {FBEBA881-CA18-4B65-9F32-D9C0BFF9AD7F}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {FBEBA881-CA18-4B65-9F32-D9C0BFF9AD7F}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {FBEBA881-CA18-4B65-9F32-D9C0BFF9AD7F}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {9354640E-E0A0-43DD-9CFC-FDDF624305DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {9354640E-E0A0-43DD-9CFC-FDDF624305DA}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {9354640E-E0A0-43DD-9CFC-FDDF624305DA}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {9354640E-E0A0-43DD-9CFC-FDDF624305DA}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {72C8A993-523D-458F-9F1B-9487CE99222B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {72C8A993-523D-458F-9F1B-9487CE99222B}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {72C8A993-523D-458F-9F1B-9487CE99222B}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {72C8A993-523D-458F-9F1B-9487CE99222B}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {7360E627-D807-4CBD-B258-56E0BD9701C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {7360E627-D807-4CBD-B258-56E0BD9701C6}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {7360E627-D807-4CBD-B258-56E0BD9701C6}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {7360E627-D807-4CBD-B258-56E0BD9701C6}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {848856E2-00C2-0F3D-6CA5-FFDF0D0FB3BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {848856E2-00C2-0F3D-6CA5-FFDF0D0FB3BE}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {848856E2-00C2-0F3D-6CA5-FFDF0D0FB3BE}.Release|Any CPU.ActiveCfg = Release|Any CPU 55 | {848856E2-00C2-0F3D-6CA5-FFDF0D0FB3BE}.Release|Any CPU.Build.0 = Release|Any CPU 56 | EndGlobalSection 57 | GlobalSection(SolutionProperties) = preSolution 58 | HideSolutionNode = FALSE 59 | EndGlobalSection 60 | GlobalSection(NestedProjects) = preSolution 61 | {FBEBA881-CA18-4B65-9F32-D9C0BFF9AD7F} = {E20B2331-FE69-414C-B706-11EF41B09D8B} 62 | {9354640E-E0A0-43DD-9CFC-FDDF624305DA} = {07FEB8AC-F72D-469E-8AE6-A5518919D2FD} 63 | {72C8A993-523D-458F-9F1B-9487CE99222B} = {07FEB8AC-F72D-469E-8AE6-A5518919D2FD} 64 | {7360E627-D807-4CBD-B258-56E0BD9701C6} = {07FEB8AC-F72D-469E-8AE6-A5518919D2FD} 65 | {848856E2-00C2-0F3D-6CA5-FFDF0D0FB3BE} = {E20B2331-FE69-414C-B706-11EF41B09D8B} 66 | EndGlobalSection 67 | GlobalSection(ExtensibilityGlobals) = postSolution 68 | SolutionGuid = {D3B18EBC-D9A2-4625-8DD5-1BE213D6DEEA} 69 | EndGlobalSection 70 | EndGlobal 71 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 沙漠尽头的狼 5 | Copyright (c) 2024 https://codewf.com 6 | true 7 | MIT 8 | preview 9 | https://github.com/dotnet9/AvaloniaExtensions 10 | https://github.com/dotnet9/AvaloniaExtensions 11 | true 12 | true 13 | enable 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Dotnet9(dotnet9.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AvaloniaExtensions 2 | 3 | English | [简体中文](README.zh-CN.md) 4 | 5 | A syntactic sugar library that brings convenience to Avalonia UI development, aiming to enhance development efficiency and experience by providing additional functionality and simplifying common operations. 6 | 7 | ## Install 8 | 9 | ```bash 10 | Install-Package AvaloniaExtensions.Axaml 11 | ``` 12 | 13 | ## I18n 14 | 15 | **Format Requirements**: Create an `I18n` directory in your project and create multilingual resource files `Resources[.xx].resx` within it as needed. For example, create `Resources.resx`, `Resources.zh-CN.resx`, and `Resources.ja-JP.resx` for English, Simplified Chinese, and Japanese. The demo structure is as follows: 16 | 17 | ![](doc/imgs/ResourceDir.png) 18 | 19 | It is recommended to use ResX Manager to edit resource files for easier management of multilingual resources: 20 | 21 | ![](doc/imgs/ResourceConfig.png) 22 | 23 | You can use a T4 template file (such as `Language.tt` in the demo) to automatically generate C# code for resource keys for easy referencing in your project: 24 | 25 | ```tt 26 | <#@ template debug="false" hostspecific="true" language="C#" #> 27 | <#@ assembly name="System.Core" #> 28 | <#@ assembly name="System.Xml" #> 29 | <#@ assembly name="System.Xml.Linq" #> 30 | <#@ import namespace="System.Linq" #> 31 | <#@ import namespace="System.Text" #> 32 | <#@ import namespace="System.Collections.Generic" #> 33 | <#@ import namespace="System.Xml.Linq" #> 34 | <#@ import namespace="System.IO" #> 35 | <#@ output extension=".cs" #> 36 | //------------------------------------------------------------------------------ 37 | // 38 | // This code was generated by a tool. 39 | // Changes to this file may cause incorrect behavior and will be lost if 40 | // the code is regenerated. 41 | // 42 | //------------------------------------------------------------------------------ 43 | <# 44 | const string ResourceFileName = "Resources.resx"; 45 | #> 46 | 47 | namespace <#=System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString()#>; 48 | 49 | public static class Language 50 | { 51 | <# 52 | var resourceKeys = XElement.Load(this.Host.ResolvePath(ResourceFileName)) 53 | .Elements("data") 54 | .Select(item => item.Attribute("name")?.Value) 55 | .Where(item => item != null); 56 | 57 | var resourceDesignerName = Path.GetFileNameWithoutExtension(ResourceFileName); 58 | 59 | foreach (string resourceKey in resourceKeys) 60 | { 61 | #> 62 | public static readonly string <#= resourceKey #> = "<#= resourceKey #>"; 63 | <# 64 | } 65 | #> 66 | } 67 | ``` 68 | 69 | Generated C# code example: 70 | 71 | ```csharp 72 | public static class Language 73 | { 74 | public static readonly string AppName = "AppName"; 75 | public static readonly string Welcome = "Welcome"; 76 | } 77 | ``` 78 | 79 | **Usage**: 80 | 81 | - Static binding of resource keys: 82 | 83 | ```axaml 84 | 85 | 86 | ``` 87 | 88 | - Dynamic binding of resource keys: 89 | 90 | ```axaml 91 | 92 | ``` 93 | 94 | - Switching languages: 95 | 96 | ```csharp 97 | I18nManager.Instance.Culture = new CultureInfo(language); 98 | ``` 99 | 100 | ## If 101 | 102 | Using the Conditional expression in AXAML. 103 | 104 | ```axaml 105 | 106 | 107 | ``` 108 | 109 | ## Converter Extensions 110 | 111 | ### IfConditionConverter 112 | 113 | ```axaml 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | ``` 123 | 124 | ```axaml 125 | 126 | ``` 127 | 128 | ## Thanks 129 | 130 | Taken from open source, contributed to open source. The development of this library was inspired and aided by the following open-source projects or articles: 131 | 132 | - WpfExtensions: [https://github.com/DingpingZhang/WpfExtensions](https://github.com/DingpingZhang/WpfExtensions) 133 | - I18N:[https://github.com/Antelcat/I18N](https://github.com/Antelcat/I18N) 134 | - WPF或Avalonia使用tt模板和resx文件实现国际化:[https://blog.csdn.net/eyupaopao/article/details/136638194](https://blog.csdn.net/eyupaopao/article/details/136638194) 135 | 136 | Special thanks to the contributors of these projects. Their efforts have provided valuable references and assistance for the development of AvaloniaExtensions. 137 | -------------------------------------------------------------------------------- /README.zh-CN.md: -------------------------------------------------------------------------------- 1 | # AvaloniaExtensions 2 | 3 | 简体中文 | [English](README.md) 4 | 5 | 为Avalonia UI开发带来便利的语法糖库,旨在通过提供额外的功能和简化常用操作,提升开发效率与体验。 6 | 7 | ## 安装 8 | 9 | ```bash 10 | Install-Package AvaloniaExtensions.Axaml 11 | ``` 12 | 13 | ## I18n 14 | 15 | **格式要求**: 在项目中创建`I18n`目录,并在其中创建需要支持的多语言资源文件`Resources[.xx].resx`。例如,为英语、简体中文、日语创建`Resources.resx`和`Resources.zh-CN.resx`、`Resources.ja-JP.resx`。参考Demo结构如下: 16 | 17 | ![](doc/imgs/ResourceDir.png) 18 | 19 | 推荐使用ResX Manager进行资源文件编辑,以更便捷地管理多语言资源: 20 | 21 | ![](doc/imgs/ResourceConfig.png) 22 | 23 | 可使用T4模板文件(如Demo中的`Language.tt`)自动生成资源键的C#代码,以便在项目中引用: 24 | 25 | ```tt 26 | <#@ template debug="false" hostspecific="true" language="C#" #> 27 | <#@ assembly name="System.Core" #> 28 | <#@ assembly name="System.Xml" #> 29 | <#@ assembly name="System.Xml.Linq" #> 30 | <#@ import namespace="System.Linq" #> 31 | <#@ import namespace="System.Text" #> 32 | <#@ import namespace="System.Collections.Generic" #> 33 | <#@ import namespace="System.Xml.Linq" #> 34 | <#@ import namespace="System.IO" #> 35 | <#@ output extension=".cs" #> 36 | //------------------------------------------------------------------------------ 37 | // 38 | // This code was generated by a tool. 39 | // Changes to this file may cause incorrect behavior and will be lost if 40 | // the code is regenerated. 41 | // 42 | //------------------------------------------------------------------------------ 43 | <# 44 | const string ResourceFileName = "Resources.resx"; 45 | #> 46 | 47 | namespace <#=System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString()#>; 48 | 49 | public static class Language 50 | { 51 | <# 52 | var resourceKeys = XElement.Load(this.Host.ResolvePath(ResourceFileName)) 53 | .Elements("data") 54 | .Select(item => item.Attribute("name")?.Value) 55 | .Where(item => item != null); 56 | 57 | var resourceDesignerName = Path.GetFileNameWithoutExtension(ResourceFileName); 58 | 59 | foreach (string resourceKey in resourceKeys) 60 | { 61 | #> 62 | public static readonly string <#= resourceKey #> = "<#= resourceKey #>"; 63 | <# 64 | } 65 | #> 66 | } 67 | ``` 68 | 69 | 生成的C#代码示例: 70 | 71 | ```csharp 72 | public static class Language 73 | { 74 | public static readonly string AppName = "AppName"; 75 | public static readonly string Welcome = "Welcome"; 76 | } 77 | ``` 78 | 79 | **使用方式**: 80 | 81 | - 静态绑定资源键: 82 | 83 | ```axaml 84 | 85 | 86 | 87 | ``` 88 | 89 | - 动态绑定资源键: 90 | 91 | ```axaml 92 | 93 | ``` 94 | 95 | - 切换语言: 96 | 97 | ```csharp 98 | I18nManager.Instance.Culture = new CultureInfo(language); 99 | ``` 100 | 101 | ## If 102 | 103 | 在AXAML中使用条件表达式 104 | 105 | ```axaml 106 | 107 | 108 | ``` 109 | 110 | ## 扩展转换器 111 | 112 | ### IfConditionConverter 113 | 114 | ```axaml 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | ``` 124 | 125 | ```axaml 126 | 127 | ``` 128 | 129 | ## 感谢 130 | 131 | 取之于开源,献之于开源。本库的开发受到了以下开源项目或文章的启发与帮助: 132 | 133 | - WpfExtensions: [https://github.com/DingpingZhang/WpfExtensions](https://github.com/DingpingZhang/WpfExtensions) 134 | - I18N:[https://github.com/Antelcat/I18N](https://github.com/Antelcat/I18N) 135 | - WPF或Avalonia使用tt模板和resx文件实现国际化:[https://blog.csdn.net/eyupaopao/article/details/136638194](https://blog.csdn.net/eyupaopao/article/details/136638194) 136 | 137 | 特别感谢这些项目的贡献者,他们的努力为AvaloniaExtensions的开发提供了宝贵的参考与帮助。 138 | -------------------------------------------------------------------------------- /avalonia-using-xml-files-for-internationalization.md: -------------------------------------------------------------------------------- 1 | ## 1. XML 实现 Avalonia UI 国际化的优势剖析 2 | 3 | 在软件开发日益全球化的今天,Avalonia 的国际化实现策略成为了众多开发者关注的焦点。继上一篇 [Avalonia 国际化之路:Resx 资源文件的深度应用与探索](https://dotnet9.com/bbs/post/2024/12/avalonia-internationalization-depth-application-and-exploration-of-resx-resource-files)之后,本文将引领大家深入探究如何运用自定义 XML 文件来达成 Avalonia 国际化的目标,开启一段全新的技术探索之旅。 4 | 5 | 6 | ### 1.1. 突破维护局限,拥抱用户定制 7 | 8 | Resx 资源文件往往将维护权限局限于开发端,而自定义 XML 语言文件则以其独特的灵活性脱颖而出。它能够与可执行程序一同输出,这一特性为用户侧的语言文件修改开辟了广阔的空间。用户不仅可以根据自身需求对已有语言内容进行调整,还能够轻松地扩展更多语言种类,使软件的国际化适应能力得到极大的提升,真正实现了从开发主导到用户参与的转变。 9 | 10 | ### 1.2. 命名空间加持,结构清晰有序 11 | 12 | 自定义 XML 文件采用命名空间的方式来组织语言内容,这一设计理念与类的结构形成了精准的对应关系。通过这种方式,整个翻译文件的架构变得清晰明了,易于管理与维护。无论是在大型项目中进行团队协作开发,还是在后期的代码维护与升级过程中,都能够显著提高工作效率,减少因结构混乱而可能引发的错误与困扰。 13 | 14 | ### 1.3. AI 翻译便捷,助力语言转换 15 | 16 | 在当今人工智能蓬勃发展的时代,XML 文件在 AI 翻译方面展现出了得天独厚的优势。借助特定的工具或平台,我们可以方便地利用 AI 技术对 XML 翻译文件进行处理。例如,只需提供简单的提示词,就能快速获取多种语言版本的翻译结果,为跨语言交流与软件全球化推广提供了强有力的支持。 17 | 18 | 下图为输出的XML语言文件: 19 | 20 | ![](https://img1.dotnet9.com/2024/12/0201.png) 21 | 22 | ## 2. XML 文件的创建与架构设计 23 | ### 2.1. 精心规划语言文件夹 24 | 25 | 首先,我们需要创建一个专门用于存放语言文件的文件夹,例如命名为 “i18n”。在这个过程中,需要特别注意的是,相同输出路径下不同模块的 XML 文件名必须保持唯一性。因为在程序编译输出时,如果存在同名的 XML 文件,将会导致文件替换,进而造成语言信息的丢失,这无疑会给国际化进程带来严重的阻碍。 26 | 27 | 文件前缀可以工程名命名(方便区分),后缀为语言文化名 28 | 29 | 以下是创建语言文件夹后的工程结构示例图: 30 | 31 | ![](https://img1.dotnet9.com/2024/12/0202.png) 32 | 33 | 编译输出后文件列表如下: 34 | 35 | ```shell 36 | AIModule.en-US.xml 37 | AIModule.ja-JP.xml 38 | AIModule.zh-CN.xml 39 | AIModule.zh-Hant.xml 40 | ConverterModule.en-US.xml 41 | ConverterModule.ja-JP.xml 42 | ConverterModule.zh-CN.xml 43 | ConverterModule.zh-Hant.xml 44 | DevelopmentModule.en-US.xml 45 | DevelopmentModule.ja-JP.xml 46 | DevelopmentModule.zh-CN.xml 47 | DevelopmentModule.zh-Hant.xml 48 | MainModule.en-US.xml 49 | MainModule.ja-JP.xml 50 | MainModule.zh-CN.xml 51 | MainModule.zh-Hant.xml 52 | XmlTranslatorManagerModule.en-US.xml 53 | XmlTranslatorManagerModule.ja-JP.xml 54 | XmlTranslatorManagerModule.zh-CN.xml 55 | XmlTranslatorManagerModule.zh-Hant.xml 56 | ``` 57 | 58 | ### 2.2. 严谨构建 XML 文件内容 59 | 60 | 以下是上面一个XML文件内容(`AIModule.zh-CN.xml`): 61 | 62 | ```xml 63 | 64 | 65 | 66 | 67 | AI 68 | 69 | 70 | 智能问答助手 71 | 一键提问,即刻获取答案,智能问答助手为您解惑。 72 | 73 | 74 | AI一键多语种翻译神器 75 | 轻松实现一键翻译,支持多种语言互译,让沟通无界限! 76 | 77 | 78 | AI一键转URL Slug 79 | 轻松将中文、英文等文章标题一键转换成英文URL Slug。 80 | 81 | 82 | 语言 83 | 可选择的 84 | 已选择 85 | 86 | 87 | ``` 88 | 89 | XML 文件的内容结构遵循一定的规范与层次。推荐采用三层结构来组织信息: 90 | 91 | #### 2.2.1. 第一层:Localization 节点 92 | 93 | 此节点包含三个重要的子属性: 94 | - **language**:用于明确指定语言的名称,例如 “Chinese (Simplified)” 表示简体中文。 95 | - **description**:对该语言的简要描述,如 “中文简体”,以便于开发者和用户快速了解语言的基本特征。 96 | - **cultureName**:语言的文化名,这是一个在国际化处理中非常关键的标识,例如 “zh-CN” 代表简体中文的文化区域。如果对文化名不太清楚,可以通过百度搜索等方式获取准确信息。 97 | 98 | 以下是一个基本的 XML 文件框架示例: 99 | 100 | ```xml 101 | 102 | 103 | 104 | 105 | 106 | ``` 107 | 108 | #### 2.2.2. 第二层:功能名或类名节点 109 | 110 | 这一层以功能名或类名来命名节点,其目的在于对翻译文件进行有效的功能归类。例如,在一个包含 AI 模块、转换模块等的项目中,可以分别创建 “AIModule”、“ConverterModule” 等节点,将与各个模块相关的翻译内容分别放置在对应的节点下,使整个文件的结构更加清晰,便于管理与查找。 111 | 112 | 以下是一个包含多个模块节点的 XML 文件示例: 113 | 114 | ```xml 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | ``` 126 | 127 | #### 2.2.3. 第三层:语言 Key 节点 128 | 129 | 最后一层为语言 Key 节点,这些节点直接存储了具体的翻译文本内容。例如: 130 | 131 | ```xml 132 | 133 | 134 | 135 | 136 | AI 137 | 138 | 139 | 智能问答助手 140 | 一键提问,即刻获取答案,智能问答助手为您解惑。 141 | 142 | 143 | ``` 144 | 145 | 在实际应用中,虽然推荐采用三层结构,但 XML 节点的嵌套层数并没有严格的限制,开发者可以根据项目的实际需求和复杂程度进行灵活调整。例如: 146 | 147 | ```xml 148 | 149 | 150 | 151 | 152 | AI 153 | 154 | 155 | 156 | 智能问答助手 157 | 一键提问,即刻获取答案,智能问答助手为您解惑。 158 | 159 | 160 | 161 | 162 | 163 | AI一键多语种翻译神器 164 | 轻松实现一键翻译,支持多种语言互译,让沟通无界限! 165 | 166 | 167 | 168 | 169 | AI一键多语种翻译神器 170 | 轻松实现一键翻译,支持多种语言互译,让沟通无界限! 171 | 172 | 173 | 174 | 175 | ``` 176 | 177 | ## 3. 语言文件的强类型生成策略 178 | 179 | 为了在代码中更加方便、高效地使用 XML 翻译文件,我们采用 T4 文件将 XML 转换为 C# 的强类型。具体操作如下: 180 | 181 | 在 “i18n” 目录下创建一个 T4 文件,例如命名为 “Language.tt”,并在其中填入以下代码: 182 | 183 | ```csharp 184 | <#@ template debug="false" hostspecific="true" language="C#" #> 185 | <#@ assembly name="System.Core" #> 186 | <#@ assembly name="System.Xml" #> 187 | <#@ assembly name="System.Xml.Linq" #> 188 | <#@ import namespace="System.Linq" #> 189 | <#@ import namespace="System.Text" #> 190 | <#@ import namespace="System.Collections.Generic" #> 191 | <#@ import namespace="System.Xml.Linq" #> 192 | <#@ import namespace="System.IO" #> 193 | <#@ output extension=".cs" #> 194 | //------------------------------------------------------------------------------ 195 | // 196 | // This code was generated by a tool. 197 | // Changes to this file may cause incorrect behavior and will be lost if 198 | // the code is regenerated. 199 | // 200 | //------------------------------------------------------------------------------ 201 | <# 202 | string templateDirectory = Path.GetDirectoryName(Host.TemplateFile); 203 | string xmlFilePath = Directory.GetFiles(templateDirectory, "*.xml").FirstOrDefault(); 204 | if (xmlFilePath!= null) 205 | { 206 | XDocument xdoc = XDocument.Load(xmlFilePath); 207 | var classNodes = xdoc.Nodes().OfType().DescendantsAndSelf().Where(e => e.Descendants().Count() == 0).Select(e => e.Parent).Distinct().ToList(); 208 | foreach (var classNode in classNodes) 209 | { 210 | var namespaceSegments = classNode.Ancestors().Reverse().Select(node => node.Name.LocalName); 211 | string namespaceName = string.Join(".", namespaceSegments); 212 | GenerateClasses(classNode, namespaceName); 213 | } 214 | } 215 | else 216 | { 217 | Write("XML file not found, please ensure that there is an XML file in the current directory"); 218 | } 219 | 220 | void GenerateClasses(XElement element, string namespaceName) 221 | { 222 | string className = element.Name.LocalName; 223 | StringBuilder classBuilder = new StringBuilder(); 224 | classBuilder.AppendLine($"namespace {namespaceName}"); 225 | classBuilder.AppendLine("{"); 226 | classBuilder.AppendLine($" public static class {className}"); 227 | classBuilder.AppendLine(" {"); 228 | var fieldNodes = element.Elements(); 229 | foreach (var fieldNode in fieldNodes) 230 | { 231 | var propertyName = fieldNode.Name.LocalName; 232 | var languageKey = $"{namespaceName}.{className}.{propertyName}"; 233 | classBuilder.AppendLine($" public static readonly string {propertyName} = \"{languageKey}\";"); 234 | } 235 | classBuilder.AppendLine(" }"); 236 | classBuilder.AppendLine("}"); 237 | Write(classBuilder.ToString()); 238 | } 239 | #> 240 | ``` 241 | 242 | 每次对 XML 文件进行修改后,只需打开该 T4 文件并执行一次保存操作,系统将会自动生成或更新对应的 C# 类。例如: 243 | 244 | ```csharp 245 | //... 246 | namespace Localization 247 | { 248 | public static class AIModule 249 | { 250 | public static readonly string Title = "Localization.AIModule.Title"; 251 | } 252 | } 253 | namespace Localization 254 | { 255 | public static class AskBotView 256 | { 257 | public static readonly string Title = "Localization.AskBotView.Title"; 258 | public static readonly string Description = "Localization.AskBotView.Description"; 259 | } 260 | } 261 | //... 262 | ``` 263 | 264 | 这些生成的强类型类将为我们在后续的代码编写中提供极大的便利,使我们能够更加准确、便捷地获取和使用翻译文本。 265 | 266 | ## 4. XML 文件在 Avalonia 中的具体应用实践 267 | 268 | ### 4.1. 安装必备 NuGet 包 269 | 270 | ```shell 271 | Install-Package AvaloniaXmlTranslator 272 | ``` 273 | 274 | 这一步骤将为我们的项目引入必要的功能组件,为后续的国际化操作奠定基础。 275 | 276 | ### 4.2. 动态获取语言列表 277 | 278 | 在 Avalonia 应用中,动态获取程序配置的语言列表是实现国际化界面切换的关键步骤之一。通过以下代码,我们可以轻松地获取语言列表: 279 | 280 | ```csharp 281 | List languages = I18nManager.Instance.Resources.Select(kvp => kvp.Value).ToList(); 282 | ``` 283 | 284 | 其中,“LocalizationLanguage” 类定义如下: 285 | 286 | ```csharp 287 | public class LocalizationLanguage 288 | { 289 | public string Language { get; set; } = (string) null; 290 | 291 | public string Description { get; set; } = (string) null; 292 | 293 | public string CultureName { get; set; } = (string) null; 294 | 295 | //... 296 | } 297 | ``` 298 | 299 | 获取到语言列表后,我们可以将其用于界面绑定,例如在下拉菜单中显示可供用户选择的语言选项,或者在其他需要展示语言信息的界面元素中进行数据绑定。 300 | 301 | ### 4.3. 动态切换语言 302 | 303 | 当用户在界面中选择了不同的语言后,我们需要在代码中实现语言的动态切换。以下是实现语言切换的代码示例: 304 | 305 | ```csharp 306 | var culture = new CultureInfo(language); 307 | I18nManager.Instance.Culture = culture; 308 | ``` 309 | 310 | 这里的 “language” 参数为 “LocalizationLanguage” 类的 “CultureName” 属性值,通过设置当前线程的文化信息,我们可以实现界面语言的即时切换,为用户提供无缝的国际化体验。 311 | 312 | ### 4.4. 代码中使用翻译字符串 313 | 314 | 在代码中,我们可以根据强类型 Key 方便地获取当前语言文化的翻译字符串。例如: 315 | 316 | ```csharp 317 | var title = I18nManager.GetString(Localization.AskBotView.Title); // 获取当前语言 318 | var titleZhCN = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "zh-CN"); // 获取中文简体 319 | var titleEnUS = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "en-US"); // 获取英文 320 | ``` 321 | 322 | 通过这种方式,我们可以在代码的任何地方灵活地使用翻译文本,确保界面显示的内容与用户选择的语言相匹配。 323 | 324 | 下面是代码中绑定使用: 325 | 326 | ```csharp 327 | //... 328 | var header = item is UserControl { DataContext: ITabItemBase tabItem } 329 | ? tabItem.TitleKey 330 | : item?.GetType().ToString(); 331 | var newTabItem = new TabItem { Content = item }; 332 | newTabItem.Bind(TabItem.HeaderProperty, new I18nBinding(header)); 333 | regionTarget.Items.Add(newTabItem); 334 | //... 335 | ``` 336 | 337 | ### 4.5. Axaml 界面中的应用 338 | 339 | 在 Axaml 界面中使用 XML 翻译文件也非常便捷。首先,需要引入相应的命名空间: 340 | 341 | ```xaml 342 | xmlns:language="clr-namespace:Localization" 343 | xmlns:markup="https://codewf.com" 344 | ``` 345 | 346 | 其中,“markup” 为前面安装的辅助库命名空间,它提供了 “I18n” 标记扩展帮助类,用于在界面中绑定翻译文本;“language” 为 T4 文件生成的 C# 强类型语言 Key 关联类命名空间,通过它可以与 XML 语言文件的语言 Key 进行关联。 347 | 348 | 以下是在控件中使用翻译文本的示例: 349 | 350 | ```xaml 351 |