Skip to content

Commit

Permalink
use Intl.displaynames for country display
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 9, 2024
1 parent d8b8c2a commit 6ef88ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 0 additions & 1 deletion js/src/forum/extenders/extendCommentPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default function extendCommentPost() {
extend(CommentPost.prototype, 'headerItems', function (items: ItemList<Mithril.Children>) {
if (app.forum.attribute<boolean>('fof-geoip.showFlag')) {
const ipInfo = this.attrs.post.ip_info?.();
console.log(ipInfo);
if (ipInfo) {
const { image } = getIPData(ipInfo);
if (image) {
Expand Down
14 changes: 12 additions & 2 deletions js/src/forum/helpers/IPDataHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import app from 'flarum/forum/app';
import IPInfo from '../models/IPInfo';
import getFlagEmojiUrl from '../util/getFlagEmojiUrl';
import Tooltip from 'flarum/common/components/Tooltip';
Expand All @@ -13,10 +14,19 @@ export const getThreat = (ipInfo: IPInfo) => {
export const getFlagImage = (ipInfo: IPInfo) => {
if (ipInfo && ipInfo.countryCode() && ipInfo.countryCode().length > 1) {
const url = getFlagEmojiUrl(ipInfo.countryCode());

const currentLocale = app.translator.getLocale() as string;

// Create an instance of Intl.DisplayNames for displaying full country names
const displayNames = new Intl.DisplayNames([currentLocale], { type: 'region' });

// Get the full country name using the country code
const countryName = displayNames.of(ipInfo.countryCode());

if (url) {
return (
<Tooltip text={ipInfo.countryCode()}>
<img src={url} alt={ipInfo.countryCode()} height="16" loading="lazy" />
<Tooltip text={countryName}>
<img src={url} alt={countryName} height="16" loading="lazy" />
</Tooltip>
);
}
Expand Down

0 comments on commit 6ef88ba

Please sign in to comment.