Skip to content

Instantly share code, notes, and snippets.

@philJohnson
Created June 16, 2021 07:04
Show Gist options
  • Save philJohnson/5539b9ffc5d611de2e99c13467ef6ec2 to your computer and use it in GitHub Desktop.
Save philJohnson/5539b9ffc5d611de2e99c13467ef6ec2 to your computer and use it in GitHub Desktop.
Polylang Language Selector
import React from 'react';
import { StaticQuery, graphql } from 'gatsby';
import { StyledLocationSelector, StyledList } from './styles';
export const LanguageSelector: React.StatelessComponent<any> = ({language}) => (
<StaticQuery
query={graphql`
query Languages {
wp {
languages {
name
homeUrl
}
}
}
`}
render={({wp: {languages}}) => {
if(languages.length > 1) {
return (
<StyledLocationSelector>
<span>Language Select</span>
<StyledList>
{languages.map(({homeUrl, name}) => {
return <li><a href={homeUrl} >{name}</a></li>
})}
</StyledList>
</StyledLocationSelector>
)}
return null;
}}
/>
);
export default LanguageSelector;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment