Skip to content

Commit

Permalink
Merge cb8f38b into 28fb4a9
Browse files Browse the repository at this point in the history
  • Loading branch information
jslachta authored Oct 17, 2022
2 parents 28fb4a9 + cb8f38b commit a00e968
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 114 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ jobs:
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"jslachta_LocaleNames" /o:"jslachta" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
dotnet build src/LocaleNames.sln
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info src/LocaleNames.Test/LocaleNames.Test.csproj
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ The translation data are generated from [CLDR locale data for internationalizati
### Find all language codes

```
var allLanguageCodes = LocaleNames.ForLanguageCode("en-US").AllLanguageCodes;
var allLanguageCodes = LocaleNamesFactory.ForLanguageCode("en-US").AllLanguageCodes;
```

### Find language name

```
var translatedLanguageName = LocaleNames.ForCultureInfo(new CultureInfo("en-US")).FindLanguageName("cs-CZ");
var translatedLanguageName = LocaleNamesFactory.ForCultureInfo(new CultureInfo("en-US")).FindLanguageName("cs-CZ");
```

### Find language code

```
var languageCode = LocaleNames.ForCultureInfo(new CultureInfo("en-US")).FindLanguageCode("Czech");
var languageCode = LocaleNamesFactory.ForCultureInfo(new CultureInfo("en-US")).FindLanguageCode("Czech");
```

### Find all country codes

```
var allCountryCodes = LocaleNames.ForLanguageCode("en-US").AllCountryCodes;
var allCountryCodes = LocaleNamesFactory.ForLanguageCode("en-US").AllCountryCodes;
```

### Find country name

```
var translatedCountryName = LocaleNames.ForCultureInfo(new CultureInfo("en-US")).FindCountryName("DE");
var translatedCountryName = LocaleNamesFactory.ForCultureInfo(new CultureInfo("en-US")).FindCountryName("DE");
```

### Find country code

```
var countryCode = LocaleNames.ForCultureInfo(new CultureInfo("en-US")).FindCountryCode("Germany");
var countryCode = LocaleNamesFactory.ForCultureInfo(new CultureInfo("en-US")).FindCountryCode("Germany");
```
# Stats
![Alt](https://repobeats.axiom.co/api/embed/864145fa59a424553c94a73d2343776612860b15.svg "Repobeats analytics image")
Expand Down
8 changes: 4 additions & 4 deletions src/LocaleNames.Test/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FactoryTests
[TestMethod]
public void LocaleNames_Factory_ForLanguageCode_On_Windows_Should_Have_Invariant_Culture()
{
var localeNames = LocaleNames.ForLanguageCode("unknown code");
var localeNames = LocaleNamesFactory.ForLanguageCode("unknown code");

if (OperatingSystem.IsWindows())
{
Expand All @@ -29,7 +29,7 @@ public void LocaleNames_Factory_ForLanguageCode_On_Windows_Should_Have_Invariant
[TestMethod]
public void LocaleNames_Factory_ForCultureInfo_Existing_Culture_Test()
{
var translations = LocaleNames.ForCultureInfo(new System.Globalization.CultureInfo("en-US"));
var translations = LocaleNamesFactory.ForCultureInfo(new System.Globalization.CultureInfo("en-US"));

Assert.AreEqual(new System.Globalization.CultureInfo("en-US"), translations.CultureInfo);
Assert.IsFalse(translations.AreCountryNameTranslationsEmpty);
Expand All @@ -45,7 +45,7 @@ public void LocaleNames_Factory_ForLanguageCode_Test()
/*
* if a valid language code is given, the cultureinfo name should match.
*/
var translations = LocaleNames.ForLanguageCode("cs-CZ");
var translations = LocaleNamesFactory.ForLanguageCode("cs-CZ");

Assert.AreEqual(new System.Globalization.CultureInfo("cs-CZ"), translations.CultureInfo);
Assert.IsFalse(translations.AreCountryNameTranslationsEmpty);
Expand All @@ -61,7 +61,7 @@ public void LocaleNames_Factory_ForLanguageCode_InvalidLanguageCode_Test()
/*
* if a not valid language code is given, the LocaleTranslations will not have any translations.
*/
var translations = LocaleNames.ForLanguageCode("non-existing-code");
var translations = LocaleNamesFactory.ForLanguageCode("non-existing-code");

Assert.IsTrue(translations.AreCountryNameTranslationsEmpty);
Assert.IsTrue(translations.AreLanguageTranslationsEmpty);
Expand Down
4 changes: 2 additions & 2 deletions src/LocaleNames.Test/FindCountryCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FindCountryCodeTests
[TestMethod]
public void LocaleNames_All_Country_Codes_Should_Not_Provide_Variants_And_Continents()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");

var countryCodes = localeNames.AllCountryCodes;

Expand All @@ -34,7 +34,7 @@ public void LocaleNames_All_Country_Codes_Should_Not_Provide_Variants_And_Contin
[TestMethod]
public void LocaleNames_Find_country_code_by_name()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");

Assert.AreEqual("DE", localeNames.FindCountryCode("Germany"));

Expand Down
4 changes: 2 additions & 2 deletions src/LocaleNames.Test/FindCountryNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FindCountryNameTests
[TestMethod]
public void Find_country_name_by_code()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");

Assert.AreEqual("Germany", localeNames.FindCountryName("DE"));
Assert.AreEqual("Czechia", localeNames.FindCountryName("CZ"));
Expand All @@ -22,7 +22,7 @@ public void Find_country_name_by_code()
[TestMethod]
public void Find_all_variants_of_country_name_by_code()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");
var result = localeNames.FindCountryNames("CZ");

Assert.IsTrue(result.Count == 2, "CZ country name has only two variants");
Expand Down
4 changes: 2 additions & 2 deletions src/LocaleNames.Test/FindLanguageCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FindLanguageCodeTests
[TestMethod]
public void LocaleNames_All_Language_Codes_Should_Not_Provide_Variants()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");

var languageCodes = localeNames.AllLanguageCodes;

Expand All @@ -31,7 +31,7 @@ public void LocaleNames_All_Language_Codes_Should_Not_Provide_Variants()
[TestMethod]
public void LocaleNames_Find_language_code_by_name()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");

Assert.AreEqual("de", localeNames.FindLanguageCode("German"));

Expand Down
10 changes: 5 additions & 5 deletions src/LocaleNames.Test/FindLanguageNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FindLanguageNameTests
[TestMethod]
public void LocaleNames_Find_language_name_for_given_culture()
{
var localeNames = LocaleNames.ForCultureInfo(new CultureInfo("cs-CZ"));
var localeNames = LocaleNamesFactory.ForCultureInfo(new CultureInfo("cs-CZ"));

Assert.AreEqual("čeština", localeNames.FindLanguageName("cs"));

Expand All @@ -25,7 +25,7 @@ public void LocaleNames_Find_language_name_for_given_culture()

Assert.AreEqual(null, localeNames.FindLanguageName("unknown code"));

localeNames = LocaleNames.ForCultureInfo(new CultureInfo("yue"));
localeNames = LocaleNamesFactory.ForCultureInfo(new CultureInfo("yue"));

Assert.AreEqual("德文", localeNames.FindLanguageName("de"));

Expand All @@ -37,7 +37,7 @@ public void LocaleNames_Find_language_name_for_given_culture()
[TestMethod]
public void LocaleNames_Find_language_name_for_current_culture()
{
var localeNames = LocaleNames.ForCurrentCulture();
var localeNames = LocaleNamesFactory.ForCurrentCulture();

Assert.AreEqual("angličtina (USA)", localeNames.FindLanguageName("en-US"));

Expand All @@ -49,7 +49,7 @@ public void LocaleNames_Find_language_name_for_current_culture()
[TestMethod]
public void LocaleNames_Find_language_name_for_language_code()
{
var localeNames = LocaleNames.ForLanguageCode("en-US");
var localeNames = LocaleNamesFactory.ForLanguageCode("en-US");

Assert.AreEqual("German", localeNames.FindLanguageName("de"));

Expand All @@ -65,7 +65,7 @@ public void LocaleNames_Find_language_name_for_language_code()
[TestMethod]
public void LocaleNames_Find_language_name_for_unknown_language_code()
{
var localeNames = LocaleNames.ForLanguageCode("unknown-CODE");
var localeNames = LocaleNamesFactory.ForLanguageCode("unknown-CODE");

Assert.AreEqual(null, localeNames.FindLanguageName("de"));
}
Expand Down
6 changes: 3 additions & 3 deletions src/LocaleNames.Test/LocaleNamesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class LocaleNamesTests
[TestMethod]
public void LocaleNames_Is_loading_from_cache()
{
LocaleNames.ClearCache();
var localeName = LocaleNames.ForCultureInfo(new CultureInfo("cs-CZ"));
LocaleNamesFactory.ClearCache();
var localeName = LocaleNamesFactory.ForCultureInfo(new CultureInfo("cs-CZ"));

Assert.IsFalse(localeName.IsFromCache);

localeName = LocaleNames.ForCultureInfo(new CultureInfo("cs-CZ"));
localeName = LocaleNamesFactory.ForCultureInfo(new CultureInfo("cs-CZ"));

Assert.IsTrue(localeName.IsFromCache);
}
Expand Down
15 changes: 10 additions & 5 deletions src/LocaleNames/Enumerations/AltVariant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,34 @@ namespace LocaleNames.Enumerations
[Flags]
public enum AltVariant
{
/// <summary>
/// The none variant.
/// </summary>
None = 0,

/// <summary>
/// The common name variant.
/// </summary>
Common = 0,
Common = 1,

/// <summary>
/// The alternative name variant.
/// </summary>
Alternative = 1,
Alternative = 2,

/// <summary>
/// The short name variant.
/// </summary>
Short = 2,
Short = 4,

/// <summary>
/// The long name variant.
/// </summary>
Long = 4,
Long = 8,

/// <summary>
/// The menu name variant.
/// </summary>
Menu = 8
Menu = 16
}
}
94 changes: 9 additions & 85 deletions src/LocaleNames/LocaleNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,91 +13,11 @@
namespace LocaleNames
{
/// <summary>
/// Locale Names.
///
/// </summary>
public class LocaleNames
{
#region CACHE

private static Dictionary<CultureInfo, LocaleNames> CachedLocaleNames { get; }
= new Dictionary<CultureInfo, LocaleNames>();

/// <summary>
/// Clears the cache.
/// </summary>
public static void ClearCache()
{
lock (CachedLocaleNames)
{
CachedLocaleNames.Clear();
}
}

#endregion CACHE

#region FACTORY

/// <summary>
/// Creates instance of <see cref="LocaleNames"/> for given language code.
/// </summary>
/// <param name="languageCode">The language code.</param>
/// <returns></returns>
public static LocaleNames ForLanguageCode(string languageCode)
{
CultureInfo cultureInfo = null;

try
{
cultureInfo = CultureInfo.GetCultureInfo(languageCode);
}
catch (Exception)
{
cultureInfo = CultureInfo.InvariantCulture;
}

return ForCultureInfo(cultureInfo);
}

/// <summary>
/// Creates instance of <see cref="LocaleNames"/> for current culture.
/// </summary>
/// <returns></returns>
public static LocaleNames ForCurrentCulture()
{
var currentCulture = CultureInfo.CurrentCulture;

return ForCultureInfo(currentCulture);
}

/// <summary>
/// Creates instance of <see cref="LocaleNames"/> for given culture.
/// </summary>
/// <param name="cultureInfo">The culture information.</param>
/// <returns></returns>
public static LocaleNames ForCultureInfo(CultureInfo cultureInfo)
{
lock (CachedLocaleNames)
{
if (CachedLocaleNames.ContainsKey(cultureInfo))
{
var cachedlocalenames = CachedLocaleNames[cultureInfo];
cachedlocalenames.IsFromCache = true;

return cachedlocalenames;
}
else
{
var localeNames = new LocaleNames(cultureInfo);
CachedLocaleNames.Add(cultureInfo, localeNames);

return localeNames;
}
}
}

#endregion FACTORY

#region PROPERTIES
#region FIELDS

/// <summary>
/// Gets a value indicating whether are language translations empty.
Expand All @@ -109,13 +29,17 @@ public static LocaleNames ForCultureInfo(CultureInfo cultureInfo)
/// </summary>
public bool AreCountryNameTranslationsEmpty => !(CountryNames.Keys.Count > 0);

#endregion FIELDS

#region PROPERTIES

/// <summary>
/// Gets a value indicating whether this instance is from cache.
/// </summary>
/// <value>
/// <c>true</c> if this instance is from cache; otherwise, <c>false</c>.
/// </value>
public bool IsFromCache { get; private set; } = false;
public bool IsFromCache { get; internal set; } = false;

/// <summary>
/// Gets the culture information.
Expand Down Expand Up @@ -173,7 +97,7 @@ private IDictionary<string, string> CountryNames
/// Initializes a new instance of the <see cref="LocaleNames"/> class.
/// </summary>
/// <param name="culture">The culture.</param>
LocaleNames(CultureInfo culture)
internal LocaleNames(CultureInfo culture)
{
CultureInfo = culture;
}
Expand Down Expand Up @@ -228,7 +152,7 @@ private bool LoadDictionary(string key, string postfix, ref IDictionary<string,
string targetManifestResourceName = this
.GetType().Assembly
.GetManifestResourceNames()
.Where(i => i.EndsWith(resourceName)).FirstOrDefault();
.FirstOrDefault(i => i.EndsWith(resourceName));

if (targetManifestResourceName != null)
{
Expand Down
Loading

0 comments on commit a00e968

Please sign in to comment.