Skip to content

Commit 317664a

Browse files
authored
Merge pull request #420 from commercetools/localizedstring-enhancement
Add constructors to LocalizedString
2 parents b9257be + 63cd5a7 commit 317664a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#nullable enable
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Runtime.Serialization;
6+
7+
namespace commercetools.Sdk.Api.Models.Common;
8+
9+
public partial class LocalizedString
10+
{
11+
public LocalizedString(): base()
12+
{
13+
}
14+
15+
public LocalizedString(IDictionary<string, string> dictionary) : base(dictionary)
16+
{
17+
}
18+
19+
public LocalizedString(IDictionary<string, string> dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer)
20+
{
21+
}
22+
23+
public LocalizedString(IEnumerable<KeyValuePair<string, string>> collection) : base(collection)
24+
{
25+
}
26+
27+
public LocalizedString(IEnumerable<KeyValuePair<string, string>> collection, IEqualityComparer<string> comparer) : base(collection, comparer)
28+
{
29+
}
30+
31+
public LocalizedString(IEqualityComparer<string> comparer) : base(comparer)
32+
{
33+
}
34+
35+
public LocalizedString(int capacity) : base(capacity)
36+
{
37+
}
38+
39+
public LocalizedString(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer)
40+
{
41+
}
42+
43+
protected LocalizedString(SerializationInfo info, StreamingContext context) : base(info, context)
44+
{
45+
}
46+
47+
/**
48+
* Creates an instance with one locale translation pair.
49+
*/
50+
public LocalizedString(string locale, string value)
51+
{
52+
this.Add(locale, value);
53+
}
54+
55+
/**
56+
* Creates an instance for two different locales.
57+
*/
58+
public LocalizedString(string locale1, string value1, string locale2, string value2)
59+
{
60+
this.Add(locale1, value1);
61+
this.Add(locale2, value2);
62+
}
63+
64+
public LocalizedString Plus(string locale, string value)
65+
{
66+
return new LocalizedString(this.Append(new KeyValuePair<string, string>(locale, value)));
67+
}
68+
69+
public string? Find(string locale)
70+
{
71+
return this.GetValueOrDefault(locale);
72+
}
73+
74+
/**
75+
* Searches the translation for some exact locales in the order they appear and returning the result.
76+
*/
77+
public string? Find(IEnumerable<string> locales)
78+
{
79+
var firstOrDefault = locales.FirstOrDefault(this.ContainsKey);
80+
81+
return this.GetValueOrDefault(firstOrDefault);
82+
}
83+
84+
public static LocalizedString OfEnglish(string translationForEnglish) {
85+
return new LocalizedString("en", translationForEnglish);
86+
}
87+
}

0 commit comments

Comments
 (0)