Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Russian language support #2119

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions IsraelHiking.API/Controllers/OsmTracesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ private async Task<string> GetDescriptionByArea(string language, List<Coordinate
{
return defaultDescription;
}
var replacementTarget = language == "he"
? "מסלול ב" + containerName
: "A route in " + containerName;
return defaultDescription.Replace("מסלול", replacementTarget).Replace("Route", replacementTarget).Replace("Recorded using IHM at", replacementTarget);
return defaultDescription.Replace("Recorded using IHM at", containerName);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions IsraelHiking.API/Executors/FeaturesMergeExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@
do
{
index++;
} while (natureReserveFeature.Attributes.Exists(FeatureAttributes.NAME + ":he" + index));
natureReserveFeature.Attributes.Add(FeatureAttributes.NAME + ":he" + index, alternativeTitle);
} while (natureReserveFeature.Attributes.Exists(FeatureAttributes.NAME + ":" + Languages.HEBREW + index));
natureReserveFeature.Attributes.Add(FeatureAttributes.NAME + ":" + Languages.HEBREW + index, alternativeTitle);

Check warning on line 653 in IsraelHiking.API/Executors/FeaturesMergeExecutor.cs

View check run for this annotation

Codecov / codecov/patch

IsraelHiking.API/Executors/FeaturesMergeExecutor.cs#L653

Added line #L653 was not covered by tests
}
natureReserveFeature.SetTitles();
}
Expand Down
10 changes: 8 additions & 2 deletions IsraelHiking.Common/Strings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace IsraelHiking.Common;
using System.Linq;

namespace IsraelHiking.Common;

public static class Categories
{
Expand Down Expand Up @@ -105,11 +107,15 @@ public static class Languages
public const string ALL = "all";
public const string HEBREW = "he";
public const string ENGLISH = "en";
public const string RUSSIAN = "ru";
public const string DEFAULT = "default";
public static readonly string[] Array =
[
HEBREW,
ENGLISH
ENGLISH,
RUSSIAN
];
public static readonly string[] ArrayWithDefault = new [] { DEFAULT }.Concat(Array).ToArray();
}

public static class Branding
Expand Down
86 changes: 70 additions & 16 deletions IsraelHiking.DataAccess/ElasticSearch/ElasticSearchGateway.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Elasticsearch.Net;
using IsraelHiking.Common;
Expand Down Expand Up @@ -123,16 +124,63 @@ private List<IHit<T>> GetAllItemsByScrolling<T>(ISearchResponse<T> response) whe
return list;
}

/// <summary>
/// This method is used to extract the field with the highest contribution to the score
/// It uses the explanation object to recursively find the field
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
private string FindBestField(ExplanationDetail exp)
{
// Extract the field from the explanation
var match = Regex.Match(exp.Description, @"weight\((.*)\)");
if (match.Success)
{
return match.Groups[1].Value;
}

// Recursively check child explanations
foreach (var child in exp.Details)
{
var childResult = FindBestField(child);
if (!string.IsNullOrEmpty(childResult))
return childResult;
}

return null;
}

/// <summary>
/// This method is used to find the language with the highest contribution to the score
/// </summary>
/// <param name="explanation">The explanination object from the query results</param>
/// <param name="fallbackLanguage">The language to use in case no matching language was found</param>
/// <returns></returns>
private string GetBestMatchLanguage(Explanation explanation, string fallbackLanguage)
{
// Recursive method to find the field with the highest contribution to the score
foreach (var details in explanation.Details)
{
var results = FindBestField(details);
if (!string.IsNullOrEmpty(results) && Languages.ArrayWithDefault.Any(l => results.Contains("." + l)))
{
return Languages.ArrayWithDefault.First(l => results.Contains("." + l));
}
}
return fallbackLanguage;
}

private IFeature HitToFeature(IHit<PointDocument> d, string language)
{
var searchTermLanguage = GetBestMatchLanguage(d.Explanation, language);
IFeature feature = new Feature(new Point(d.Source.Location[0], d.Source.Location[1]), new AttributesTable
{
{ FeatureAttributes.NAME, d.Source.Name.GetValueOrDefault(language, d.Source.Name.GetValueOrDefault(Languages.ENGLISH, string.Empty)) },
{ FeatureAttributes.NAME, d.Source.Name.GetValueOrDefault(searchTermLanguage, d.Source.Name.GetValueOrDefault(Languages.ENGLISH, string.Empty)) },
{ FeatureAttributes.POI_SOURCE, d.Source.PoiSource },
{ FeatureAttributes.POI_ICON, d.Source.PoiIcon },
{ FeatureAttributes.POI_CATEGORY, d.Source.PoiCategory },
{ FeatureAttributes.POI_ICON_COLOR, d.Source.PoiIconColor },
{ FeatureAttributes.DESCRIPTION, d.Source.Description.GetValueOrDefault(language, d.Source.Description.GetValueOrDefault(Languages.ENGLISH, string.Empty)) },
{ FeatureAttributes.DESCRIPTION, d.Source.Description.GetValueOrDefault(searchTermLanguage, d.Source.Description.GetValueOrDefault(Languages.ENGLISH, string.Empty)) },
{ FeatureAttributes.POI_ID, d.Id },
{ FeatureAttributes.POI_LANGUAGE, Languages.ALL },
{ FeatureAttributes.ID, string.Join("_", d.Id.Split("_").Skip(1)) }
Expand All @@ -146,19 +194,22 @@ private IFeature HitToFeature(IHit<PointDocument> d, string language)
return feature;
}

private QueryContainer DocumentNameSearchQuery<T>(QueryContainerDescriptor<T> q, string searchTerm, string language) where T: class
private QueryContainer DocumentNameSearchQuery<T>(QueryContainerDescriptor<T> q, string searchTerm) where T: class
{
return q.DisMax(dm =>
dm.Queries(sh =>
sh.MatchPhrase(m =>
m.Query(searchTerm)
.Field("name." + language + ".keyword")
.Boost(5)
sh.MultiMatch(m =>
m.Type(TextQueryType.Phrase)
.Query(searchTerm)
.Boost(5)
.Fields(f => f.Fields(Languages.ArrayWithDefault.Select(l => new Field("name." + l + ".keyword"))))
),
sh => sh.Match(m =>
m.Query(searchTerm)
.Field("name." + language)
sh =>
sh.MultiMatch(m =>
m.Type(TextQueryType.BestFields)
.Query(searchTerm)
.Fuzziness(Fuzziness.Auto)
.Fields(f => f.Fields(Languages.ArrayWithDefault.Select(l => new Field("name." + l))))
)
)
);
Expand All @@ -175,7 +226,8 @@ public async Task<List<IFeature>> Search(string searchTerm, string language)
.Size(NUMBER_OF_RESULTS)
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(q => DocumentNameSearchQuery(q, searchTerm, language))
.Query(q => DocumentNameSearchQuery(q, searchTerm))
.Explain()
);
return response.Hits.Select(d=> HitToFeature(d, language)).ToList();
}
Expand All @@ -192,10 +244,12 @@ public async Task<List<IFeature>> SearchExact(string searchTerm, string language
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(q =>
q.MatchPhrase(m =>
m.Query(searchTerm)
.Field("name." + language + ".keyword"))
q.MultiMatch(m =>
m.Type(TextQueryType.Phrase)
.Query(searchTerm)
.Fields(f => f.Fields(Languages.ArrayWithDefault.Select(l => new Field("name." + l + ".keyword"))))
)
).Explain()
);
return response.Hits.Select(d=> HitToFeature(d, language)).ToList();
}
Expand All @@ -213,7 +267,7 @@ public async Task<List<IFeature>> SearchPlaces(string searchTerm, string languag
.Size(1)
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(q => DocumentNameSearchQuery(q, place, language))
.Query(q => DocumentNameSearchQuery(q, place))
);
if (placesResponse.Documents.Count == 0)
{
Expand All @@ -223,7 +277,7 @@ public async Task<List<IFeature>> SearchPlaces(string searchTerm, string languag
.Size(NUMBER_OF_RESULTS)
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(q => DocumentNameSearchQuery(q, searchTerm, language) &&
.Query(q => DocumentNameSearchQuery(q, searchTerm) &&
q.GeoShape(b =>
{
b.Field(p => p.Location);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<div [dir]="resources.direction">
<div mat-dialog-content>
<div *ngIf="step === 0">
<h2>{{resources.language}}</h2>
<mat-radio-group (change)=resources.setLanguage($event.value) [value]="getLanuguageCode()">
<div *ngFor="let language of availableLanguages">
<mat-radio-button [value]="language.code" color="primary" angulartics2On="click" angularticsCategory="Intro" angularticsAction="Select language {{language.code}}">{{resources.getLabelForCode(language.code)}}</mat-radio-button>
<mat-radio-button [value]="language.code" color="primary" angulartics2On="click" angularticsCategory="Intro" angularticsAction="Select language {{language.code}}">{{language.label}}</mat-radio-button>
</div>
</mat-radio-group>
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div mat-dialog-content>
<mat-radio-group [(ngModel)]="selectedLanguageCode">
<div *ngFor="let language of availableLanguages">
<mat-radio-button [value]="language.code" color="primary" angulartics2On="click" angularticsCategory="Language" angularticsAction="Select language {{language.code}}">{{resources.getLabelForCode(language.code)}}</mat-radio-button>
<mat-radio-button [value]="language.code" color="primary" angulartics2On="click" angularticsCategory="Language" angularticsAction="Select language {{language.code}}">{{language.label}}</mat-radio-button>
</div>
</mat-radio-group>
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<button mat-menu-item (click)="openLanguage()" angulartics2On="click" angularticsCategory="Menu" angularticsAction="Open language selection">
<span class="flex flex-row justify-center items-center">
<span class="w-1/12 text-center"><i class="fa fa-lg icon-language mat-icon"></i></span>
<span class="flex-1 margin-sides">{{resources.language}}</span>
<span class="flex-1 margin-sides">Language</span>
</span>
</button>
<a mat-menu-item *ngIf="isShowEditOsmButton()" [href]="getOsmAddress()" [target]="'_blank'" angulartics2On="click" angularticsCategory="Menu" angularticsAction="Edit in OSM">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class SearchComponent {
searchTerm
} as SearchRequestQueueItem);
try {
const results = await this.searchResultsProvider.getResults(searchTerm, this.resources.hasRtlCharacters(searchTerm));
const results = await this.searchResultsProvider.getResults(searchTerm);
const queueItem = this.requestsQueue.find(itemToFind => itemToFind.searchTerm === searchTerm);
if (queueItem == null || this.requestsQueue.indexOf(queueItem) !== this.requestsQueue.length - 1) {
this.requestsQueue.splice(0, this.requestsQueue.length - 1);
Expand Down
3 changes: 2 additions & 1 deletion IsraelHiking.Web/src/application/models/language.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type LanguageCode = "en-US" | "he";
export type LanguageCode = "en-US" | "he" | "ru";

export type Language = {
code: LanguageCode;
rtl: boolean;
label: string;
};
7 changes: 7 additions & 0 deletions IsraelHiking.Web/src/application/reducers/initial-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ export const SPECIAL_LAYERS = [...SPECIAL_BASELAYERS, ...SPECIAL_OVERLAYS];
export const AVAILABLE_LANGUAGES: Language[] = [{
code: "he",
rtl: true,
label: "עברית"
},
{
code: "en-US",
rtl: false,
label: "English"
},
{
code: "ru",
rtl: false,
label: "Русский"
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ImageGalleryService {
private readonly resourcesService = inject(ResourcesService);

public open(urls: string[], index: number) {
if (this.resourcesService.getCurrentLanguageCodeSimplified() === "he") {
if (this.resourcesService.direction === "rtl") {
urls = [...urls].reverse();
index = urls.length - 1 - index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe("Poi Service", () => {
}
] as any;

store.dispatch(new SetLanguageAction({ code: "he", rtl: false }));
store.dispatch(new SetLanguageAction({ code: "he", rtl: false, label: "עברית" }));

await new Promise((resolve) => setTimeout(resolve, 100)); // this is in order to let the code continue to run to the next await

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe("ResourcesService", () => {
expect(service.hasRtlCharacters("1. نص عربي")).toBeTruthy();
expect(service.hasRtlCharacters("hello")).toBeFalsy();
expect(service.hasRtlCharacters("1. hello")).toBeFalsy();
expect(service.hasRtlCharacters("1. Кейсария")).toBeFalsy();
}));

it("Should be able get the layout direction for titles", inject([ResourcesService], (service: ResourcesService) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,10 +951,6 @@ export class ResourcesService {
await this.setLanguageInternal(language);
}

public getLabelForCode(code: LanguageCode): string {
return code === "he" ? "עברית" : "English";
}

public translate(word: string): string {
return this.gettextCatalog.getString(word) || word;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GeoJsonParser } from "./geojson.parser";
import { RunningContextService } from "./running-context.service";
import { PoiService } from "./poi.service";
import { CoordinatesService } from "./coordinates.service";
import { ResourcesService } from "./resources.service";
import type { SearchResultsPointOfInterest } from "../models/models";

describe("SearchResultsProvider", () => {
Expand All @@ -18,6 +19,9 @@ describe("SearchResultsProvider", () => {
CoordinatesService,
{ provide: RunningContextService, useValue: { isOnline: true } },
{ provide: PoiService, useValue: null },
{ provide: ResourcesService, useValue: {
getCurrentLanguageCodeSimplified: () => "en"
} },
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
]
Expand All @@ -26,7 +30,7 @@ describe("SearchResultsProvider", () => {

it("Should get all kind of features in results", (inject([SearchResultsProvider, HttpTestingController],
async (provider: SearchResultsProvider, mockBackend: HttpTestingController) => {
const promise = provider.getResults("searchTerm", true).then((results: SearchResultsPointOfInterest[]) => {
const promise = provider.getResults("searchTerm").then((results: SearchResultsPointOfInterest[]) => {
expect(results.length).toBe(1);
}, fail);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { firstValueFrom } from "rxjs";

import { CoordinatesService } from "./coordinates.service";
import { RouteStrings, getIdFromLatLng } from "./hash.service";
import { ResourcesService } from "./resources.service";
import { Urls } from "../urls";
import type { SearchResultsPointOfInterest } from "../models/models";

Expand All @@ -13,8 +14,9 @@ export class SearchResultsProvider {

private readonly httpClient = inject(HttpClient);
private readonly coordinatesService = inject(CoordinatesService);
private readonly resources = inject(ResourcesService)

public async getResults(searchTerm: string, isHebrew: boolean): Promise<SearchResultsPointOfInterest[]> {
public async getResults(searchTerm: string): Promise<SearchResultsPointOfInterest[]> {
const searchWithoutBadCharacters = searchTerm.replace("/", " ").replace("\t", " ");
const latlng = this.coordinatesService.parseCoordinates(searchWithoutBadCharacters);
if (latlng) {
Expand All @@ -30,8 +32,7 @@ export class SearchResultsProvider {
description: "",
}];
}
const language = isHebrew ? "he" : "en";
const params = new HttpParams().set("language", language);
const params = new HttpParams().set("language", this.resources.getCurrentLanguageCodeSimplified());
const response = await firstValueFrom(this.httpClient.get(Urls.search + encodeURIComponent(searchWithoutBadCharacters), {
params
}).pipe(timeout(3000)));
Expand Down
2 changes: 1 addition & 1 deletion IsraelHiking.Web/src/translations/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
"km per hour": "קמ\"ש",
"Km POIs": "נ\"צ לק\"מ",
"Lake, Reservoir": "מקווה מים",
"Language": "שפה - Language",
"Language": "שפה",
"Last recording did not end well. Feel free to start a new one.": "ההקלטה האחרונה לא הסתיימה בהצלחה. אתם מוזמנים להתחיל הקלטה חדשה.",
"Last updated on": "עודכן לאחרונה ב- ",
"Layers": "מסלולים - מפות - שכבות",
Expand Down
Loading