From 7131273d1e157584b1acf04164fd75c9112d42b7 Mon Sep 17 00:00:00 2001 From: Juliana Crivelli Date: Thu, 16 Nov 2023 17:06:48 -0300 Subject: [PATCH 1/4] Wildcard locales as fallback --- dotnet/Services/ProductReviewService.cs | 21 ++-- dotnet/Utils/Locale.cs | 139 ++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 dotnet/Utils/Locale.cs diff --git a/dotnet/Services/ProductReviewService.cs b/dotnet/Services/ProductReviewService.cs index f9fa2c15..2b500ae0 100644 --- a/dotnet/Services/ProductReviewService.cs +++ b/dotnet/Services/ProductReviewService.cs @@ -11,6 +11,7 @@ using System.Net; using ReviewsRatings.DataSources; using Vtex.Api.Context; + using ReviewsRatings.Utils; /// /// Business logic @@ -393,7 +394,7 @@ public async Task GetReviewsByProductId(string productId { string searchQuery = string.Empty; string ratingQuery = string.Empty; - string localeQuery = string.Empty; + string localeQuery = $"(locale={locale}-*)"; bool ratingFilter = rating > 0 && rating <= 5; bool pastRevNLocale = pastReviews && !string.IsNullOrEmpty(locale); ReviewsResponseWrapper wrapper = new ReviewsResponseWrapper(); @@ -413,13 +414,23 @@ public async Task GetReviewsByProductId(string productId AppSettings settings = await GetAppSettings(); - if (pastRevNLocale) + if (Locale.localeList.ContainsKey(locale)) { + localeQuery = ""; + foreach (var language in Locale.localeList[locale]) + { + localeQuery += $"(locale={locale}-{language}) OR"; + } + localeQuery = localeQuery.Substring(localeQuery.Length - 2); + } + + if (pastRevNLocale) + { if (settings.RequireApproval) { searchQuery = $" AND approved=true"; } - localeQuery = $"((locale={locale}-*) OR (locale is null))"; + localeQuery = $"({localeQuery} OR (locale is null))"; if (ratingFilter) { ratingQuery = $" AND rating={rating}"; @@ -438,10 +449,6 @@ public async Task GetReviewsByProductId(string productId { searchQuery = $"{searchQuery}&approved=true"; } - if (!string.IsNullOrEmpty(locale)) - { - localeQuery = $"&locale={locale}-*"; - } wrapper = await this._productReviewRepository.GetProductReviewsMD($"productId={productId}{sort}{searchQuery}{ratingQuery}{localeQuery}", from.ToString(), to.ToString()); } diff --git a/dotnet/Utils/Locale.cs b/dotnet/Utils/Locale.cs new file mode 100644 index 00000000..81091967 --- /dev/null +++ b/dotnet/Utils/Locale.cs @@ -0,0 +1,139 @@ +using System.Collections.Generic; + +namespace ReviewsRatings.Utils +{ + public static class Locale + { + public static Dictionary> localeList = new Dictionary>() { + + { "ca", new List() {"ES", "AD" }}, + {"ar", new List() {"JO", "KM", "ER", "OM", "EG", "PS", "SY", "DJ", "SA", "SO", "SD", "YE", "LY", "TN", "EH", "MA", "BH", "AE", "KW", "LB", "QA", "IQ", "TD", "MR", "DZ"}}, + { "fa", new List() {"AF", "IR"}}, + { "ps", new List() { "AF"}}, + {"en", new List() { "PH", "SH", "IM", "LC", "DM", "GY", "NA", "WS", "AS", "IN", "PW", "GM", "FM", "TO", "SG", "SZ", "AI", "IE", "NU", "MW", "FJ", "CX", "NZ", "BB", "SL", "JE", "TK", "PR", "RW", "HK", "PG", "GI", "LS", "SD", "BZ", "KR", "VG", "SB", "MT", "ZA", "VC", "CK", "BW", "NR", "CM", "UG", "MS", "AQ", "HM", "ER", "GG", "LR", "AU", "ZM", "MU", "GB", "IO", "US", "VU", "CC", "TT", "GD", "KI", "UM", "VI", "ZW", "CA", "MH", "GU", "KE", "TC", "PN", "NF", "BM", "BS", "TV", "SC", "KY", "PK", "KN", "SX", "AG", "SS", "GH", "FK", "NG", "TZ", "GS", "JM", "CW", "MP"}}, + { "sq", new List() { "AL", "ME"}}, + { "hy", new List() { "AM"}}, + { "pt", new List() { "ST", "CV", "TL", "GQ", "BR", "MO", "GW", "MZ", "AO", "PT"}}, + { "es", new List() { "MX", "PY", "AQ", "ES", "CO", "DO", "PE", "PA", "PR", "HN", "CU", "BO", "CL", "UY", "GT", "NI", "EH", "VE", "SV", "GQ", "EC", "AR", "CR"}}, + { "fr", new List() { "NC", "CH", "RE", "LU", "PM", "CG", "AQ", "GF", "IT", "KM", "BL", "GN", "BI", "TF", "BJ", "NE", "DJ", "CD", "RW", "MC", "MU", "SC", "WF", "GA", "PF", "BF", "CI", "HT", "MG", "SN", "CF", "TN", "VU", "ML", "EH", "YT", "FR", "GP", "MA", "BE", "GQ", "LB", "MQ", "MF", "CA", "TD", "TG", "MR", "CM"}}, + { "ru", new List() { "MD", "TJ", "KG", "RU", "AQ", "BY", "KZ"}}, + { "sm", new List() { "AS", "WS", "TK"}}, + { "de", new List() { "DE", "CH", "LU", "BE", "IT", "AT", "NA", "LI"}}, + { "nl", new List() { "AW", "BE", "NL", "CW", "SR", "BQ", "SX"}}, + { "pap", new List() { "AW"}}, + { "sv", new List() { "FI", "AX", "SE"}}, + { "az", new List() { "AZ"}}, + { "bs", new List() { "BA", "ME"}}, + { "hr", new List() { "BA", "HR", "ME"}}, + { "sr", new List() { "RS", "BA", "ME"}}, + { "bn", new List() { "BD"}}, + { "bg", new List() { "BG"}}, + { "ms", new List() { "MY", "BN", "SG"}}, + { "qu", new List() { "BO"}}, + { "gn", new List() { "BO", "PY"}}, + { "ay", new List() { "BO"}}, + { "dz", new List() { "BT"}}, + { "no", new List() { "SJ", "BV", "NO"}}, + { "tn", new List() { "ZA", "BW"}}, + { "be", new List() { "BY"}}, + { "sg", new List() { "CF"}}, + { "it", new List() { "SM", "CH", "VA", "IT"}}, + { "rm", new List() { "CH"}}, + { "rar", new List() { "CK"}}, + { "zh-hans", new List() { "CN", "SG"}}, + { "el", new List() { "GR", "CY"}}, + { "tr", new List() { "TR", "CY"}}, + { "cs", new List() { "CZ"}}, + { "so", new List() { "SO", "DJ"}}, + { "aa", new List() { "DJ"}}, + { "da", new List() { "DK", "FO", "GL"}}, + { "et", new List() { "EE"}}, + { "ti", new List() { "ER"}}, + { "ast", new List() { "ES"}}, + { "eu", new List() { "ES"}}, + { "gl", new List() { "ES"}}, + { "am", new List() { "ET"}}, + { "om", new List() { "ET"}}, + { "fi", new List() { "FI"}}, + { "se", new List() { "FI", "NO"}}, + { "fo", new List() { "FO"}}, + { "ga", new List() { "IE", "GB"}}, + { "cy", new List() { "GB"}}, + { "gd", new List() { "GB"}}, + { "kw", new List() { "GB"}}, + { "ka", new List() { "GE"}}, + { "kl", new List() { "GL"}}, + { "ch", new List() { "MP", "GU"}}, + { "zh-hant", new List() { "MO", "TW", "HK"}}, + { "ht", new List() { "HT"}}, + { "hu", new List() { "HU"}}, + { "id", new List() { "ID"}}, + { "he", new List() { "PS", "IL"}}, + { "hi", new List() { "IN"}}, + { "ku", new List() { "IQ"}}, + { "is", new List() { "IS"}}, + { "ja", new List() { "JP", "PW"}}, + { "sw", new List() { "TZ", "KE", "UG", "KM"}}, + { "ky", new List() { "KG"}}, + { "km", new List() { "KH"}}, + { "ko", new List() { "KR", "KP"}}, + { "kk", new List() { "KZ"}}, + { "lo", new List() { "LA"}}, + { "si", new List() { "LK"}}, + { "ta", new List() { "LK", "SG"}}, + { "st", new List() { "LS", "ZA"}}, + { "lt", new List() { "LT"}}, + { "lb", new List() { "LU"}}, + {"lv", new List() { "LV"}}, + { "zgh", new List() { "MA"}}, + { "ro", new List() { "MD", "RO"}}, + { "uk", new List() { "MD", "UA"}}, + { "srp", new List() { "ME"}}, + { "mg", new List() { "MG"}}, + { "mh", new List() { "MH"}}, + { "mk", new List() { "MK"}}, + { "my", new List() { "MM"}}, + { "mn", new List() { "MN"}}, + { "mt", new List() { "MT"}}, + { "mfe", new List() { "MU"}}, + { "dv", new List() { "MV"}}, + { "ny", new List() { "MW"}}, + { "sf", new List() { "NA"}}, + { "pih", new List() { "NF", "PN"}}, + { "nb", new List() { "NO"}}, + { "nn", new List() { "NO"}}, + { "ne", new List() { "NP"}}, + { "na", new List() { "NR"}}, + { "niu", new List() { "NU"}}, + { "mi", new List() { "NZ"}}, + { "tpi", new List() { "PG"}}, + { "ho", new List() { "PG"}}, + { "tl", new List() { "PH"}}, + { "ur", new List() { "PK"}}, + { "pl", new List() { "PL"}}, + { "pau", new List() { "PW"}}, + { "sov", new List() { "PW"}}, + { "tox", new List() { "PW"}}, + { "sr-Latn", new List() { "RS"}}, + { "rw", new List() { "RW"}}, + { "crs", new List() { "SC"}}, + { "sl", new List() { "SI"}}, + { "sk", new List() { "SK"}}, + { "ss", new List() { "SZ"}}, + { "th", new List() { "TH"}}, + { "tg", new List() { "TJ"}}, + { "tkl", new List() { "TK"}}, + { "tet", new List() { "TL"}}, + { "tk", new List() { "TM"}}, + { "uz", new List() { "UZ"}}, + { "kaa", new List() { "UZ"}}, + { "vi", new List() { "VN"}}, + { "bi", new List() { "VU"}}, + { "af", new List() { "ZA"}}, + { "xh", new List() { "ZA"}}, + { "zu", new List() { "ZA"}}, + { "sn", new List() { "ZW"}}, + { "nd", new List() { "ZW"}} + }; + } +} From 65598325f7f4ff94246d2d91d4a329da24e569e0 Mon Sep 17 00:00:00 2001 From: jumc Date: Thu, 16 Nov 2023 17:14:39 -0300 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd907d3a..3d5a9742 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,14 +6,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Changed + +- Using static language-country pairings derived from the locale and searching for `locale={locale}-*` only as fallback + ## [3.13.3] - 2023-11-16 ### Removed + - Prefix wildcard to query for locale at Master Data's ProductReviews ## [3.13.2] - 2023-08-09 ### Added + - Translation of the ShowMore component. ## [3.13.1] - 2023-06-13 From 1be67e6a67aea4793c65a8ba7489b1462b1af350 Mon Sep 17 00:00:00 2001 From: jumc Date: Thu, 16 Nov 2023 17:15:16 -0300 Subject: [PATCH 3/4] Formatting --- dotnet/Utils/Locale.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/Utils/Locale.cs b/dotnet/Utils/Locale.cs index 81091967..b02df9b4 100644 --- a/dotnet/Utils/Locale.cs +++ b/dotnet/Utils/Locale.cs @@ -7,10 +7,10 @@ public static class Locale public static Dictionary> localeList = new Dictionary>() { { "ca", new List() {"ES", "AD" }}, - {"ar", new List() {"JO", "KM", "ER", "OM", "EG", "PS", "SY", "DJ", "SA", "SO", "SD", "YE", "LY", "TN", "EH", "MA", "BH", "AE", "KW", "LB", "QA", "IQ", "TD", "MR", "DZ"}}, + { "ar", new List() {"JO", "KM", "ER", "OM", "EG", "PS", "SY", "DJ", "SA", "SO", "SD", "YE", "LY", "TN", "EH", "MA", "BH", "AE", "KW", "LB", "QA", "IQ", "TD", "MR", "DZ"}}, { "fa", new List() {"AF", "IR"}}, { "ps", new List() { "AF"}}, - {"en", new List() { "PH", "SH", "IM", "LC", "DM", "GY", "NA", "WS", "AS", "IN", "PW", "GM", "FM", "TO", "SG", "SZ", "AI", "IE", "NU", "MW", "FJ", "CX", "NZ", "BB", "SL", "JE", "TK", "PR", "RW", "HK", "PG", "GI", "LS", "SD", "BZ", "KR", "VG", "SB", "MT", "ZA", "VC", "CK", "BW", "NR", "CM", "UG", "MS", "AQ", "HM", "ER", "GG", "LR", "AU", "ZM", "MU", "GB", "IO", "US", "VU", "CC", "TT", "GD", "KI", "UM", "VI", "ZW", "CA", "MH", "GU", "KE", "TC", "PN", "NF", "BM", "BS", "TV", "SC", "KY", "PK", "KN", "SX", "AG", "SS", "GH", "FK", "NG", "TZ", "GS", "JM", "CW", "MP"}}, + { "en", new List() { "PH", "SH", "IM", "LC", "DM", "GY", "NA", "WS", "AS", "IN", "PW", "GM", "FM", "TO", "SG", "SZ", "AI", "IE", "NU", "MW", "FJ", "CX", "NZ", "BB", "SL", "JE", "TK", "PR", "RW", "HK", "PG", "GI", "LS", "SD", "BZ", "KR", "VG", "SB", "MT", "ZA", "VC", "CK", "BW", "NR", "CM", "UG", "MS", "AQ", "HM", "ER", "GG", "LR", "AU", "ZM", "MU", "GB", "IO", "US", "VU", "CC", "TT", "GD", "KI", "UM", "VI", "ZW", "CA", "MH", "GU", "KE", "TC", "PN", "NF", "BM", "BS", "TV", "SC", "KY", "PK", "KN", "SX", "AG", "SS", "GH", "FK", "NG", "TZ", "GS", "JM", "CW", "MP"}}, { "sq", new List() { "AL", "ME"}}, { "hy", new List() { "AM"}}, { "pt", new List() { "ST", "CV", "TL", "GQ", "BR", "MO", "GW", "MZ", "AO", "PT"}}, From 99da173e9a391c2687c2f1f0727ab30b8ab10cdb Mon Sep 17 00:00:00 2001 From: jumc Date: Thu, 16 Nov 2023 18:45:49 -0300 Subject: [PATCH 4/4] Null check --- dotnet/Services/ProductReviewService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/Services/ProductReviewService.cs b/dotnet/Services/ProductReviewService.cs index 2b500ae0..5d648443 100644 --- a/dotnet/Services/ProductReviewService.cs +++ b/dotnet/Services/ProductReviewService.cs @@ -414,7 +414,7 @@ public async Task GetReviewsByProductId(string productId AppSettings settings = await GetAppSettings(); - if (Locale.localeList.ContainsKey(locale)) + if (!string.IsNullOrEmpty(locale) && Locale.localeList.ContainsKey(locale)) { localeQuery = ""; foreach (var language in Locale.localeList[locale])