Skip to content

Commit

Permalink
Merge pull request #622 from osmandapp/t610
Browse files Browse the repository at this point in the history
Group translations for all POI types
  • Loading branch information
alisa911 authored Dec 20, 2024
2 parents 90616e9 + f9e1cb7 commit 7d02cba
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 30 deletions.
103 changes: 95 additions & 8 deletions map/src/infoblock/components/wpt/WptTagInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function WptTagInfo({ tag = null, baseTag = null, copy = false, s
const [open, setOpen] = useState(false);
const [openMoreDialog, setOpenMoreDialog] = useState(null);
const [tagList, setTagList] = useState(null);
const [newTag, setNewTag] = useState(null);
const [hover, setHover] = useState(false);

function handleCopy(value) {
Expand All @@ -31,6 +32,38 @@ export default function WptTagInfo({ tag = null, baseTag = null, copy = false, s
setTagList(items);
}
}
if (tag?.otherLangs) {
const currentLanguage = i18n.language;
if (tag.lang === currentLanguage) {
setTagList(tag.otherLangs);
return;
}
let newTag;
let ind = tag.otherLangs.findIndex((item) => item.lang === currentLanguage);
let tagWithoutLangInd = tag.otherLangs.findIndex((item) => !item.lang);
if (ind > -1) {
newTag = tag.otherLangs[ind];
if (tagWithoutLangInd > -1) {
newTag.otherLangs = tag.otherLangs.filter(
(_, index) => index !== ind && index !== tagWithoutLangInd
);
newTag.otherLangs.unshift(tag.otherLangs[tagWithoutLangInd], tag);
} else {
newTag.otherLangs = tag.otherLangs.filter((_, index) => index !== ind);
newTag.otherLangs.unshift(tag);
}
} else {
if (tagWithoutLangInd > -1) {
newTag = tag.otherLangs[tagWithoutLangInd];
newTag.otherLangs = [
...tag.otherLangs.slice(0, tagWithoutLangInd),
...tag.otherLangs.slice(tagWithoutLangInd + 1),
];
}
}
setNewTag(newTag);
setTagList(newTag.otherLangs);
}
}, [tag]);

function wrapValueInLink(tag, value) {
Expand Down Expand Up @@ -116,14 +149,26 @@ export default function WptTagInfo({ tag = null, baseTag = null, copy = false, s
}

function getTranslation(key, value) {
if (key.includes(':')) {
const arr = key.split(':');
const t = arr[0];
const lang = arr[1];
if (i18n.exists('lang_' + lang) && i18n.exists(t)) {
return (
capitalize(translateWithSplit(i18n.t, t)) +
': ' +
capitalize(translateWithSplit(i18n.t, 'lang_' + lang))
);
}
}
if (i18n.exists(key)) {
return capitalize(translateWithSplit(i18n.t, key));
}
return capitalize(value);
}

function getValue(tag) {
const value = prepareValueFromList(tag);
let value = prepareValueFromList(tag);
if (tag.collapsable) {
const items = tag.value.split(SEPARATOR);

Expand All @@ -142,6 +187,25 @@ export default function WptTagInfo({ tag = null, baseTag = null, copy = false, s
)}
</>
);
} else if (tag.otherLangs) {
const mainTag = newTag ?? tag;
const listTags = tagList ?? mainTag.otherLangs;
value = prepareValueFromList(mainTag);
return (
<>
<ListItemText onClick={() => setOpen(!open)}>
<MenuItemWithLines
name={getTranslation(`${POI_PREFIX}${mainTag.textPrefix}`, mainTag.textPrefix)}
maxLines={2}
className={styles.tagPrefix}
/>
<MenuItemWithLines name={value} maxLines={1} className={styles.tagName} />
</ListItemText>
{listTags.length > 1 && (
<IconButton onClick={() => setOpen(!open)}>{open ? <ExpandLess /> : <ExpandMore />}</IconButton>
)}
</>
);
} else {
return (
<ListItemText onClick={() => openMoreInfoDialog(tag)}>
Expand Down Expand Up @@ -220,13 +284,36 @@ export default function WptTagInfo({ tag = null, baseTag = null, copy = false, s
)}
{tagList && (
<Collapse in={open} timeout="auto" unmountOnExit>
{tagList.map((item, index) => (
<MenuItem disableRipple key={index} divider className={styles.tagList}>
<Typography key={index} className={styles.tagName}>
{translateWithSplit(t, `${POI_PREFIX}${item}`)}
</Typography>
</MenuItem>
))}
{tagList.map((item, index) =>
typeof item === 'string' ? (
<MenuItem disableRipple key={index} divider className={styles.tagList}>
<Typography key={index} className={styles.tagName}>
{translateWithSplit(t, `${POI_PREFIX}${item}`)}
</Typography>
</MenuItem>
) : (
<MenuItem
key={index}
style={{ userSelect: 'text' }}
disableRipple={!(tag.key === WIKIPEDIA && ctx.develFeatures)}
className={styles.tagItem}
divider
>
<ListItemText>
<MenuItemWithLines
name={getTranslation(`${POI_PREFIX}${item.textPrefix}`, item.textPrefix)}
maxLines={2}
className={styles.tagPrefix}
/>
<MenuItemWithLines
name={prepareValueFromList(item)}
maxLines={1}
className={styles.tagName}
/>
</ListItemText>
</MenuItem>
)
)}
</Collapse>
)}
{openMoreDialog && (
Expand Down
33 changes: 30 additions & 3 deletions map/src/infoblock/components/wpt/WptTagsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { changeIconColor } from '../../../map/markers/MarkerOptions';
import { createPoiCache, updatePoiCache } from '../../../manager/PoiManager';
import React from 'react';
import { apiGet } from '../../../util/HttpApi';
import { parseTagWithLang } from '../../../manager/SearchManager';

export const DEFAULT_TAG_ICON_SIZE = 24;
export const DEFAULT_TAG_ICON_COLOR = '#727272';
Expand Down Expand Up @@ -271,7 +272,7 @@ async function getWptTags(obj, type, ctx) {
isWikipediaLink = false;
} else if (key === 'addr:housename' || key === 'whitewater:rapid_name') {
tagObj.icon = <PoiNameIcon />;
} else if (key === 'operator' || key === 'brand') {
} else if (key.startsWith('operator') || key.startsWith('brand')) {
tagObj.icon = <BrandIcon />;
} else if (key === 'internet_access_fee_yes') {
tagObj.icon = <InternetIcon />;
Expand All @@ -280,7 +281,12 @@ async function getWptTags(obj, type, ctx) {
const svgData = await getSvgIcon({ value: prepValue, ctx });
tagObj.icon = getIcon(svgData, DEFAULT_TAG_ICON_SIZE, DEFAULT_TAG_ICON_COLOR);
} else {
const svgData = await getSvgIcon({ key, value, ctx });
const tagWithLang = parseTagWithLang(key);
let preparedKey = key;
if (tagWithLang.lang) {
preparedKey = tagWithLang.key;
}
const svgData = await getSvgIcon({ preparedKey, value, ctx });
tagObj.icon = getIcon(svgData, DEFAULT_TAG_ICON_SIZE, DEFAULT_TAG_ICON_COLOR);
}
}
Expand Down Expand Up @@ -314,10 +320,31 @@ async function getWptTags(obj, type, ctx) {
}

res = res.filter((t) => !t.key.startsWith(WEB_PREFIX));

res = mergeTagsWithLang(res);
return { res, id, type: typeTag, subtype: subtypeTag };
}

function mergeTagsWithLang(tags) {
tags.forEach((tag) => {
if (tag.key.includes(':')) {
const arr = tag.key.split(':');
tag.key = arr[0];
tag.lang = arr[1];
}
});
let tagsWithLang = tags.filter((tag) => tag.lang);
tagsWithLang.forEach((tag) => {
if (tags.includes(tag)) {
const sameTags = tags.filter((t) => t.key === tag.key && t !== tag);
if (sameTags) {
tag.otherLangs = sameTags;
tags = tags.filter((t) => !sameTags.includes(t));
}
}
});
return tags;
}

export async function addPoiTypeTag({
typeTag,
subtypeTag,
Expand Down
12 changes: 6 additions & 6 deletions map/src/manager/SearchManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export function addWikiPlacesDefaultFilters(ctx, mainSearch = false, selectedFil
export function getPoiParentCategory(props, t) {
let type = props[MAIN_CATEGORY_KEY_NAME]?.toLowerCase();
if (type) {
const brandRes = parseBrandTag(type);
if (brandRes.brand === SEARCH_BRAND) {
let brandType = _.capitalize(formattingPoiType(t(`poi_${brandRes.brand}`)));
const brandRes = parseTagWithLang(type);
if (brandRes.key === SEARCH_BRAND) {
let brandType = _.capitalize(formattingPoiType(t(`poi_${brandRes.key}`)));
if (brandRes.lang) {
brandType += ' (' + t(`lang_${brandRes.lang}`).toLowerCase() + ')';
}
Expand Down Expand Up @@ -139,9 +139,9 @@ export function getPoiParentCategory(props, t) {
return type && type !== 'undefined' && type !== '' ? type : null;
}

export function parseBrandTag(tag) {
const [brand, lang] = tag.split(':');
return { brand, lang };
export function parseTagWithLang(tag) {
const [key, lang] = tag.split(':');
return { key, lang };
}

export function getPhotoTitle(photo) {
Expand Down
24 changes: 11 additions & 13 deletions map/src/menu/search/search/SearchResults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
TYPE_OSM_TAG,
TYPE_OSM_VALUE,
} from '../../../infoblock/components/wpt/WptTagsProvider';
import { getIconByType, parseBrandTag, SEARCH_BRAND } from '../../../manager/SearchManager';
import { getIconByType, parseTagWithLang, SEARCH_BRAND } from '../../../manager/SearchManager';

export const ZOOM_ERROR = 'Please zoom in closer';
const MIN_SEARCH_ZOOM = 8;
Expand Down Expand Up @@ -70,8 +70,8 @@ export default function SearchResults({ value, setOpenSearchResults, setIsMainSe
const props = f.properties;
const type = props[CATEGORY_TYPE];
if (type === searchTypeMap.POI_TYPE || type === searchTypeMap.POI) {
const brandRes = parseBrandTag(props[CATEGORY_ICON]);
if (brandRes.brand === SEARCH_BRAND) {
const brandRes = parseTagWithLang(props[CATEGORY_ICON]);
if (brandRes.key === SEARCH_BRAND) {
f.icon = await getSearchResultIcon({ result: SEARCH_BRAND, ctx });
return;
}
Expand Down Expand Up @@ -123,17 +123,15 @@ export default function SearchResults({ value, setOpenSearchResults, setIsMainSe
}, [currentLoc, ctx.searchResult]);

useEffect(() => {
if (!memoizedResult) return;
if (memoizedResult === EMPTY_SEARCH_RESULT) {
setResult(EMPTY_SEARCH_RESULT);
return;
}
const updateIcons = async () => {
if (!memoizedResult) return;
if (memoizedResult === EMPTY_SEARCH_RESULT) {
setResult(EMPTY_SEARCH_RESULT);
return;
}

if (!memoizedResult[0]?.icon) {
await calculateIcons(memoizedResult, ctx);
}
setResult({ features: memoizedResult });
const resultWithIcons = [...memoizedResult];
await calculateIcons(resultWithIcons, ctx);
setResult({ features: resultWithIcons });
};

updateIcons().then();
Expand Down

0 comments on commit 7d02cba

Please sign in to comment.