/
LocalizableResourceString.cs
137 lines (120 loc) · 5.91 KB
/
LocalizableResourceString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using System.Linq;
using System.Resources;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// A localizable resource string that may possibly be formatted differently depending on culture.
/// </summary>
public sealed class LocalizableResourceString : LocalizableString, IObjectWritable
{
private readonly string _nameOfLocalizableResource;
private readonly ResourceManager _resourceManager;
private readonly Type _resourceSource;
private readonly string[] _formatArguments;
static LocalizableResourceString()
{
ObjectBinder.RegisterTypeReader(typeof(LocalizableResourceString), reader => new LocalizableResourceString(reader));
}
/// <summary>
/// Creates a localizable resource string with no formatting arguments.
/// </summary>
/// <param name="nameOfLocalizableResource">nameof the resource that needs to be localized.</param>
/// <param name="resourceManager"><see cref="ResourceManager"/> for the calling assembly.</param>
/// <param name="resourceSource">Type handling assembly's resource management. Typically, this is the static class generated for the resources file from which resources are accessed.</param>
public LocalizableResourceString(string nameOfLocalizableResource, ResourceManager resourceManager, Type resourceSource)
: this(nameOfLocalizableResource, resourceManager, resourceSource, Array.Empty<string>())
{
}
/// <summary>
/// Creates a localizable resource string that may possibly be formatted differently depending on culture.
/// </summary>
/// <param name="nameOfLocalizableResource">nameof the resource that needs to be localized.</param>
/// <param name="resourceManager"><see cref="ResourceManager"/> for the calling assembly.</param>
/// <param name="resourceSource">Type handling assembly's resource management. Typically, this is the static class generated for the resources file from which resources are accessed.</param>
/// <param name="formatArguments">Optional arguments for formatting the localizable resource string.</param>
public LocalizableResourceString(string nameOfLocalizableResource, ResourceManager resourceManager, Type resourceSource, params string[] formatArguments)
{
if (nameOfLocalizableResource == null)
{
throw new ArgumentNullException(nameof(nameOfLocalizableResource));
}
if (resourceManager == null)
{
throw new ArgumentNullException(nameof(resourceManager));
}
if (resourceSource == null)
{
throw new ArgumentNullException(nameof(resourceSource));
}
if (formatArguments == null)
{
throw new ArgumentNullException(nameof(formatArguments));
}
_resourceManager = resourceManager;
_nameOfLocalizableResource = nameOfLocalizableResource;
_resourceSource = resourceSource;
_formatArguments = formatArguments;
}
private LocalizableResourceString(ObjectReader reader)
{
_resourceSource = reader.ReadType();
_nameOfLocalizableResource = reader.ReadString();
_resourceManager = new ResourceManager(_resourceSource);
var length = reader.ReadInt32();
if (length == 0)
{
_formatArguments = Array.Empty<string>();
}
else
{
var argumentsBuilder = ArrayBuilder<string>.GetInstance(length);
for (int i = 0; i < length; i++)
{
argumentsBuilder.Add(reader.ReadString());
}
_formatArguments = argumentsBuilder.ToArrayAndFree();
}
}
bool IObjectWritable.ShouldReuseInSerialization => false;
void IObjectWritable.WriteTo(ObjectWriter writer)
{
writer.WriteType(_resourceSource);
writer.WriteString(_nameOfLocalizableResource);
var length = _formatArguments.Length;
writer.WriteInt32(length);
for (int i = 0; i < length; i++)
{
writer.WriteString(_formatArguments[i]);
}
}
protected override string GetText(IFormatProvider formatProvider)
{
var culture = formatProvider as CultureInfo ?? CultureInfo.CurrentUICulture;
var resourceString = _resourceManager.GetString(_nameOfLocalizableResource, culture);
return resourceString != null ?
(_formatArguments.Length > 0 ? string.Format(resourceString, _formatArguments) : resourceString) :
string.Empty;
}
protected override bool AreEqual(object other)
{
var otherResourceString = other as LocalizableResourceString;
return otherResourceString != null &&
_nameOfLocalizableResource == otherResourceString._nameOfLocalizableResource &&
_resourceManager == otherResourceString._resourceManager &&
_resourceSource == otherResourceString._resourceSource &&
_formatArguments.SequenceEqual(otherResourceString._formatArguments, (a, b) => a == b);
}
protected override int GetHash()
{
return Hash.Combine(_nameOfLocalizableResource.GetHashCode(),
Hash.Combine(_resourceManager.GetHashCode(),
Hash.Combine(_resourceSource.GetHashCode(),
Hash.CombineValues(_formatArguments))));
}
}
}