Skip to content

Commit

Permalink
Merge pull request #24 from LazZiya/vNext
Browse files Browse the repository at this point in the history
V next
  • Loading branch information
LazZiya authored Apr 15, 2021
2 parents 4d86522 + 42ef89a commit 2699e24
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
20 changes: 10 additions & 10 deletions XLocalizer/XLocalizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
<PackageIcon>icon.png</PackageIcon>
<PackageTags>asp.net,core,razor,mvc,localization,globalization,client side,validation,translation,autokeyadding</PackageTags>
<PackageReleaseNotes>
Improvements
- Performance imrovements
- Bypass localizing if current culture is same as TranslateFromCulture
See all docs: https://docs.ziyad.info/en/XLocalizer/v1.0/index.md

Release notes: https://github.com/LazZiya/XLocalizer/releases
</PackageReleaseNotes>
<VersionPrefix>1.0.2</VersionPrefix>
Improvements
- Removed deprected property UseExpressValidationAttributes. See https://docs.ziyad.info/en/XLocalizer/v1.0/localizing-validation-attributes-errors.md
- New option LocalizeDefatulCultre: Use to enable/disable localizing default culture
See all docs: https://docs.ziyad.info/en/XLocalizer/v1.0/index.md

Release notes: https://github.com/LazZiya/XLocalizer/releases
</PackageReleaseNotes>
<VersionPrefix>1.0.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIconUrl>https://github.com/LazZiya/XLocalizer/raw/master/XLocalizer/files/icon.png</PackageIconUrl>
Expand Down
11 changes: 6 additions & 5 deletions XLocalizer/XLocalizer.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions XLocalizer/XLocalizerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using XLocalizer.ErrorMessages;
using XLocalizer.ErrorMessages;

namespace XLocalizer
{
Expand All @@ -13,12 +12,6 @@ public class XLocalizerOptions
/// </summary>
public string ResourcesPath { get; set; } = "LocalizationResources";

/// <summary>
/// Express validation attributes are deprected. Use default attributes instead. See <a href="https://docs.ziyad.info/en/XLocalizer/v1.0/localizing-validation-attributes-errors.md">Localizing Data Annotations</a>
/// </summary>
[Obsolete("Express validation attributes are deprected. Use default attributes instead. See https://docs.ziyad.info/en/XLocalizer/v1.0/localizing-validation-attributes-errors.md")]
public bool UseExpressValidationAttributes { get; set; } = false;

/// <summary>
/// If the key string is not found in the DB, it will be inserted autoamtically to the DB.
/// default: false
Expand All @@ -44,6 +37,12 @@ public class XLocalizerOptions
/// </summary>
public string TranslateFromCulture { get; set; }

/// <summary>
/// When set to true the default culture (or source translation culture) will be localized (use case; when using CODE keys instead of texts.).
/// Default value is false (default culture not localized).
/// </summary>
public bool LocalizeDefaultCulture { get; set; } = false;

/// <summary>
/// Customize all valdiation error messages.
/// </summary>
Expand Down
21 changes: 14 additions & 7 deletions XLocalizer/XStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ public IStringLocalizer WithCulture(CultureInfo culture)

private LocalizedString GetLocalizedString(string name, params object[] arguments)
{
// Option 0: If current culture is same as translation culture just return the key back
if (_transCulture.Equals(CultureInfo.CurrentCulture.Name, StringComparison.OrdinalIgnoreCase))
var _targetCulture = CultureInfo.CurrentCulture.Name;
var _targetEqualSource = _transCulture.Equals(_targetCulture, StringComparison.OrdinalIgnoreCase);

// Option 0: Skip localization if:
// LocalizeDefaultCulture == false and currentCulture == _transCulture
if (!_options.LocalizeDefaultCulture && _targetEqualSource)
{
return new LocalizedString(name, string.Format(name, arguments), true, string.Empty);
}
Expand All @@ -111,20 +115,23 @@ private LocalizedString GetLocalizedString(string name, params object[] argument
}

// Option 3: Try online translation service
// and don't do online translation if target == source culture,
// because online tranlsation services requires two different cultures.
var availableInTranslate = false;
if (_options.AutoTranslate)
if (_options.AutoTranslate && !_targetEqualSource)
{
availableInTranslate = _translator.TryTranslate(_transCulture, CultureInfo.CurrentCulture.Name, name, out value);
availableInTranslate = _translator.TryTranslate(_transCulture, _targetCulture, name, out value);
if (availableInTranslate)
{
// Add to cache
_cache.Set<TResource>(name, value);
}
}

// Save to source file when AutoAdd is anebled and
// translation success or AutoTranslate is off
if (_options.AutoAddKeys && (availableInTranslate || !_options.AutoTranslate))
// Save to source file when AutoAdd is anebled and:
// A:translation success or, B: AutoTranslate is off or, C: Target and source cultures are same
// option C: useful when we use code keys to localize defatul culture as well
if (_options.AutoAddKeys && (availableInTranslate || !_options.AutoTranslate || _targetEqualSource))
{
bool savedToResource = _provider.TrySetValue<TResource>(name, value ?? name, "Created by XLocalizer");
_logger.LogInformation($"Save resource to file, status: '{savedToResource}', key: '{name}', value: '{value ?? name}'");
Expand Down

0 comments on commit 2699e24

Please sign in to comment.