Skip to content

Commit a495310

Browse files
committed
[Core.Translation] Modernize code
1 parent 9e5373e commit a495310

9 files changed

Lines changed: 348 additions & 373 deletions

File tree

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3-
using System;
4-
using Stride.Core.Annotations;
53

6-
namespace Stride.Core.Translation.Annotations
4+
namespace Stride.Core.Translation.Annotations;
5+
6+
/// <summary>
7+
/// Specifies a translatable name, with support for context and plural.
8+
/// </summary>
9+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum |
10+
AttributeTargets.Property | AttributeTargets.Field, Inherited = false)]
11+
public class TranslationAttribute : Attribute
712
{
813
/// <summary>
9-
/// Specifies a translatable name, with support for context and plural.
14+
/// Initializes a new instance of the <see cref="TranslationAttribute"/> class.
1015
/// </summary>
11-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum |
12-
AttributeTargets.Property | AttributeTargets.Field, Inherited = false)]
13-
public class TranslationAttribute : Attribute
16+
public TranslationAttribute(string text)
1417
{
15-
/// <summary>
16-
/// Initializes a new instance of the <see cref="TranslationAttribute"/> class.
17-
/// </summary>
18-
public TranslationAttribute([NotNull] string text)
19-
{
20-
Text = text ?? throw new ArgumentNullException(nameof(text));
21-
}
18+
Text = text ?? throw new ArgumentNullException(nameof(text));
19+
}
2220

23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="TranslationAttribute"/> class.
25-
/// </summary>
26-
public TranslationAttribute([NotNull] string text, [NotNull] string textPlural)
27-
{
28-
Text = text ?? throw new ArgumentNullException(nameof(text));
29-
TextPlural = textPlural ?? throw new ArgumentNullException(nameof(textPlural));
30-
}
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="TranslationAttribute"/> class.
23+
/// </summary>
24+
public TranslationAttribute(string text, string textPlural)
25+
{
26+
Text = text ?? throw new ArgumentNullException(nameof(text));
27+
TextPlural = textPlural ?? throw new ArgumentNullException(nameof(textPlural));
28+
}
3129

32-
public string Context { get; set; }
30+
public string? Context { get; set; }
3331

34-
[NotNull]
35-
public string Text { get; }
32+
public string Text { get; }
3633

37-
public string TextPlural { get; }
38-
}
34+
public string? TextPlural { get; }
3935
}
Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,59 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3-
using System;
3+
44
using System.Globalization;
55
using System.Reflection;
6-
using Stride.Core.Annotations;
76

8-
namespace Stride.Core.Translation
7+
namespace Stride.Core.Translation;
8+
9+
public interface ITranslationManager : ITranslationProvider
910
{
10-
public interface ITranslationManager : ITranslationProvider
11-
{
12-
/// <summary>
13-
/// Gets or sets the current culture used by this Translation Manager to look up culture-specific resources at run time.
14-
/// </summary>
15-
[NotNull]
16-
CultureInfo CurrentLanguage { get; set; }
11+
/// <summary>
12+
/// Gets or sets the current culture used by this Translation Manager to look up culture-specific resources at run time.
13+
/// </summary>
14+
CultureInfo CurrentLanguage { get; set; }
1715

18-
event EventHandler LanguageChanged;
16+
event EventHandler LanguageChanged;
1917

20-
/// <summary>
21-
/// Gets the translation of <paramref name="text"/> in the current culture.
22-
/// </summary>
23-
/// <param name="text">The text to translate.</param>
24-
/// <param name="assembly">The main assembly to lookup the translation.</param>
25-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
26-
[NotNull]
27-
string GetString([NotNull] string text, [NotNull] Assembly assembly);
18+
/// <summary>
19+
/// Gets the translation of <paramref name="text"/> in the current culture.
20+
/// </summary>
21+
/// <param name="text">The text to translate.</param>
22+
/// <param name="assembly">The main assembly to lookup the translation.</param>
23+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
24+
string GetString(string text, Assembly assembly);
2825

29-
/// <summary>
30-
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the current culture,
31-
/// choosing the right plural form depending on the <paramref name="count"/>.
32-
/// </summary>
33-
/// <param name="text">The text to translate.</param>
34-
/// <param name="textPlural">The plural version of the text to translate.</param>
35-
/// <param name="count">An integer used to determine the plural form.</param>
36-
/// <param name="assembly">The main assembly to lookup the translation.</param>
37-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
38-
[NotNull]
39-
string GetPluralString([NotNull] string text, string textPlural, long count, [NotNull] Assembly assembly);
26+
/// <summary>
27+
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the current culture,
28+
/// choosing the right plural form depending on the <paramref name="count"/>.
29+
/// </summary>
30+
/// <param name="text">The text to translate.</param>
31+
/// <param name="textPlural">The plural version of the text to translate.</param>
32+
/// <param name="count">An integer used to determine the plural form.</param>
33+
/// <param name="assembly">The main assembly to lookup the translation.</param>
34+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
35+
string GetPluralString(string text, string textPlural, long count, Assembly assembly);
4036

41-
/// <summary>
42-
/// Gets the translation of <paramref name="text"/> in the provided <paramref name="context"/> in the current culture.
43-
/// </summary>
44-
/// <param name="context">The particular context for the translation.</param>
45-
/// <param name="text">The text to translate.</param>
46-
/// <param name="assembly">The main assembly to lookup the translation.</param>
47-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
48-
[NotNull]
49-
string GetParticularString(string context, [NotNull] string text, [NotNull] Assembly assembly);
37+
/// <summary>
38+
/// Gets the translation of <paramref name="text"/> in the provided <paramref name="context"/> in the current culture.
39+
/// </summary>
40+
/// <param name="context">The particular context for the translation.</param>
41+
/// <param name="text">The text to translate.</param>
42+
/// <param name="assembly">The main assembly to lookup the translation.</param>
43+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
44+
string GetParticularString(string context, string text, Assembly assembly);
5045

51-
/// <summary>
52-
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the provided <paramref name="context"/> in the current culture,
53-
/// choosing the right plural form depending on the <paramref name="count"/>.
54-
/// </summary>
55-
/// <param name="context">The particular context for the translation.</param>
56-
/// <param name="text">The text to translate.</param>
57-
/// <param name="textPlural">The plural version of the text to translate.</param>
58-
/// <param name="count">An integer used to determine the plural form.</param>
59-
/// <param name="assembly">The main assembly to lookup the translation.</param>
60-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
61-
[NotNull]
62-
string GetParticularPluralString(string context, [NotNull] string text, string textPlural, long count, [NotNull] Assembly assembly);
46+
/// <summary>
47+
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the provided <paramref name="context"/> in the current culture,
48+
/// choosing the right plural form depending on the <paramref name="count"/>.
49+
/// </summary>
50+
/// <param name="context">The particular context for the translation.</param>
51+
/// <param name="text">The text to translate.</param>
52+
/// <param name="textPlural">The plural version of the text to translate.</param>
53+
/// <param name="count">An integer used to determine the plural form.</param>
54+
/// <param name="assembly">The main assembly to lookup the translation.</param>
55+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
56+
string GetParticularPluralString(string context, string text, string textPlural, long count, Assembly assembly);
6357

64-
void RegisterProvider([NotNull] ITranslationProvider provider);
65-
}
58+
void RegisterProvider(ITranslationProvider provider);
6659
}
Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3-
using Stride.Core.Annotations;
43

5-
namespace Stride.Core.Translation
4+
namespace Stride.Core.Translation;
5+
6+
public interface ITranslationProvider
67
{
7-
public interface ITranslationProvider
8-
{
9-
[NotNull]
10-
string BaseName { get; }
8+
string BaseName { get; }
119

12-
/// <summary>
13-
/// Gets the translation of <paramref name="text"/> in the current culture.
14-
/// </summary>
15-
/// <param name="text">The text to translate.</param>
16-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
17-
[NotNull]
18-
string GetString([NotNull] string text);
10+
/// <summary>
11+
/// Gets the translation of <paramref name="text"/> in the current culture.
12+
/// </summary>
13+
/// <param name="text">The text to translate.</param>
14+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
15+
string GetString(string text);
1916

20-
/// <summary>
21-
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the current culture,
22-
/// choosing the right plural form depending on the <paramref name="count"/>.
23-
/// </summary>
24-
/// <param name="text">The text to translate.</param>
25-
/// <param name="textPlural">The plural version of the text to translate.</param>
26-
/// <param name="count">An integer used to determine the plural form.</param>
27-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
28-
[NotNull]
29-
string GetPluralString([NotNull] string text, string textPlural, long count);
17+
/// <summary>
18+
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the current culture,
19+
/// choosing the right plural form depending on the <paramref name="count"/>.
20+
/// </summary>
21+
/// <param name="text">The text to translate.</param>
22+
/// <param name="textPlural">The plural version of the text to translate.</param>
23+
/// <param name="count">An integer used to determine the plural form.</param>
24+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
25+
string GetPluralString(string text, string textPlural, long count);
3026

31-
/// <summary>
32-
/// Gets the translation of <paramref name="text"/> in the provided <paramref name="context"/> in the current culture.
33-
/// </summary>
34-
/// <param name="context">The particular context for the translation.</param>
35-
/// <param name="text">The text to translate.</param>
36-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
37-
[NotNull]
38-
string GetParticularString(string context, [NotNull] string text);
27+
/// <summary>
28+
/// Gets the translation of <paramref name="text"/> in the provided <paramref name="context"/> in the current culture.
29+
/// </summary>
30+
/// <param name="context">The particular context for the translation.</param>
31+
/// <param name="text">The text to translate.</param>
32+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
33+
string GetParticularString(string context, string text);
3934

40-
/// <summary>
41-
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the provided <paramref name="context"/> in the current culture,
42-
/// choosing the right plural form depending on the <paramref name="count"/>.
43-
/// </summary>
44-
/// <param name="context">The particular context for the translation.</param>
45-
/// <param name="text">The text to translate.</param>
46-
/// <param name="textPlural">The plural version of the text to translate.</param>
47-
/// <param name="count">An integer used to determine the plural form.</param>
48-
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
49-
[NotNull]
50-
string GetParticularPluralString(string context, [NotNull] string text, string textPlural, long count);
51-
}
35+
/// <summary>
36+
/// Gets the translation of <paramref name="text"/> and/or <paramref name="textPlural"/> in the provided <paramref name="context"/> in the current culture,
37+
/// choosing the right plural form depending on the <paramref name="count"/>.
38+
/// </summary>
39+
/// <param name="context">The particular context for the translation.</param>
40+
/// <param name="text">The text to translate.</param>
41+
/// <param name="textPlural">The plural version of the text to translate.</param>
42+
/// <param name="count">An integer used to determine the plural form.</param>
43+
/// <returns>The translation of <paramref name="text"/> in the current culture; or <paramref name="text"/> if no translation is found.</returns>
44+
string GetParticularPluralString(string context, string text, string textPlural, long count);
5245
}

sources/core/Stride.Core.Translation/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3-
using System.Reflection;
3+
44
using System.Runtime.InteropServices;
55

66
// Setting ComVisible to false makes the types in this assembly not visible

0 commit comments

Comments
 (0)