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

feat(map): weglot support [EP-3441] #341

Merged
merged 1 commit into from
Dec 8, 2020
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
1 change: 1 addition & 0 deletions packages/earth-map/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ REACT_APP_AUTH0_AUDIENCE='' # required
REACT_APP_AUTH0_NAMESPACE='https://marapp.org' # required

REACT_APP_GTM_TAG='' # optional
REACT_APP_WEGLOT_API_KEY='' # optional

REACT_APP_ENABLE_PUBLIC_ACCESS='false' # optional
REACT_APP_EXTERNAL_IDP_URL='' # optional
Expand Down
1 change: 1 addition & 0 deletions packages/earth-map/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The following environment variables are required by the application.
| `REACT_APP_GTM_TAG` | Google Tag Manager ID |
| `REACT_APP_ENABLE_PUBLIC_ACCESS` | Enable unauthenticated access |
| `REACT_APP_EXTERNAL_IDP_URL` | External Identity Provider URL |
| `REACT_APP_WEGLOT_API_KEY` | https://weglot.com/ - Weglot API Key |

## Getting started

Expand Down
3 changes: 3 additions & 0 deletions packages/earth-map/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
-->

<title>%REACT_APP_NAME%</title>
<% if (process.env.REACT_APP_WEGLOT_API_KEY) { %>
<script type="text/javascript" src="https://cdn.weglot.com/weglot.min.js"></script>
<% } %>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LegendInfoComponent extends React.Component<ILegendInfo> {
return (
<div className="marapp-qa-layerinfo c-layer-info">
<h3 className="ng-text-display-m ng-body-color">{title}</h3>
<Html className="layer-info--html" html={description} />
<Html className="layer-info--html translate-content" html={description} />
</div>
);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/earth-map/src/components/widget/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class Widget extends React.PureComponent<IWidgetTemplate, IWidgetState> {

{/* CONTENT || INFO */}

<div className="widget--content ng-margin-large-bottom">
<div className="widget--content ng-margin-large-bottom translate-content">
{children({
...this.props,
...data,
Expand Down Expand Up @@ -311,7 +311,9 @@ class Widget extends React.PureComponent<IWidgetTemplate, IWidgetState> {
onRequestClose={() => this.setState({ activeInfo: !activeInfo })}
>
<h3 className="ng-text-display-m ng-body-color">{name}</h3>
{widgetDescription && <Html html={widgetDescription} className="widget--info" />}
{widgetDescription && (
<Html html={widgetDescription} className="widget--info translate-content" />
)}
</ModalComponent>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/earth-map/src/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const FULLPAGE_LICENSE = process.env.REACT_APP_FULLPAGE_LICENSE;
export const ADMIN_URL = process.env.REACT_APP_ADMIN_URL;
export const APP_NAME = process.env.REACT_APP_NAME;
export const MAPBOX_TOKEN = process.env.REACT_APP_MAPBOX_TOKEN;
export const WEGLOT_API_KEY = process.env.REACT_APP_WEGLOT_API_KEY;

export const GTM_TAG = process.env.REACT_APP_GTM_TAG;
export const ENABLE_PUBLIC_ACCESS = yn(process.env.REACT_APP_ENABLE_PUBLIC_ACCESS, {
Expand Down
13 changes: 12 additions & 1 deletion packages/earth-map/src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ import translationEN from './en/translation.json';
import translationES from './es/translation.json';
import translationFR from './fr/translation.json';

import * as weglot from './weglot';

const lang = SessionStorage.get('lang') || Elang.EN;

i18n.on('initialized', async (options) => {
await weglot.init();
weglot.changeLanguage(options.lng);
});

i18n.use(initReactI18next).init({
resources: {
[Elang.EN]: {
Expand All @@ -48,4 +55,8 @@ i18n.use(initReactI18next).init({
keySeparator: false,
});

i18n.on('languageChanged', (lng) => SessionStorage.add('lang', lng));
i18n.on('languageChanged', (lng) => {
weglot.changeLanguage(lng);

SessionStorage.add('lang', lng);
});
68 changes: 68 additions & 0 deletions packages/earth-map/src/locales/weglot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2018-2020 National Geographic Society
*
* Use of this software does not constitute endorsement by National Geographic
* Society (NGS). The NGS name and NGS logo may not be used for any purpose without
* written permission from NGS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

import { WEGLOT_API_KEY } from '../config';

const Weglot = window['Weglot'];

export function init(): Promise<any> {
if (!Weglot) {
return;
}

const resolver = new Promise((resolve) => Weglot.on('initialized', resolve));

Weglot.initialize({
api_key: WEGLOT_API_KEY,
dynamic: '.translate-content',
exceptions: '.translate-content-ignore',
hide_switcher: true,
cache: true,
});

return resolver;
}

export function getAvailableLanguages(): string[] {
if (!Weglot) {
return [];
}

return [
Weglot.options.language_from,
...Weglot.options.languages.map((lang) => lang.language_to),
];
}

export function changeLanguage(lang: string): void {
if (!Weglot) {
return;
}

const availableLanguages = getAvailableLanguages();

if (availableLanguages.includes(lang)) {
Weglot.switchTo(lang);
} else {
const fallbackLang = Weglot.options.language_from;
ancashoria marked this conversation as resolved.
Show resolved Hide resolved
Weglot.switchTo(fallbackLang);

console.error(`Language ${lang} is not available. Fallback to ${fallbackLang}`);
}
}