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

Feature RTL support in locale #2670

Merged
merged 2 commits into from
Dec 14, 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
18 changes: 16 additions & 2 deletions packages/vulcan-core/lib/modules/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class App extends PureComponent {
this.state = {
locale: {
id: locale.id,
rtl: locale.rtl ?? false,
method: locale.method,
loading: false,
strings: merge({}, loadedStrings, bundledStrings),
Expand Down Expand Up @@ -137,12 +138,25 @@ class App extends PureComponent {

// if this is a dynamic locale, fetch its data from the server
if (localeObject.dynamic) {
this.setState({ locale: { ...this.state.locale, loading: true } });
this.setState({ locale: { ...this.state.locale, loading: true, rtl: localeObject?.rtl ?? false } });
localeStrings = await this.loadLocaleStrings(localeId);
} else {
localeStrings = getStrings(localeId);
}
this.setState({ locale: { ...this.state.locale, loading: false, id: localeId, strings: localeStrings } });
// before removing the loading we have to change the rtl class on HTML tag if it exists
if (document && typeof document.getElementsByTagName === "function" && document.getElementsByTagName("html")) {
const htmlTag = document.getElementsByTagName("html");
if (htmlTag && htmlTag.length === 1) {
// change in locale didn't change the html lang as well, which is fixed by this PR
htmlTag[0].lang = localeId;
if (localeObject?.rtl === true) {
htmlTag[0].classList.add("rtl")
} else {
htmlTag[0].classList.remove("rtl")
}
}
}
this.setState({ locale: { ...this.state.locale, loading: false, id: localeId, rtl: localeObject?.rtl ?? false, strings: localeStrings } });

cookies.remove('locale', { path: '/' });
cookies.set('locale', localeId, { path: '/' });
Expand Down
10 changes: 8 additions & 2 deletions packages/vulcan-lib/lib/server/apollo-server/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { GraphQLSchema } from '../graphql/index.js';
import _merge from 'lodash/merge';
import { getUser } from 'meteor/apollo';
import { getHeaderLocale } from '../intl.js';
import { getLocale } from '../../modules/intl.js';
import { getSetting } from '../../modules/settings.js';
import { WebApp } from 'meteor/webapp';

Expand Down Expand Up @@ -116,12 +117,17 @@ export const computeContextFromReq = (currentContext, customContextFromReq) => {
}

context.locale = getHeaderLocale(headers, context.currentUser && context.currentUser.locale);
const locale = getLocale(context.locale);

// see https://forums.meteor.com/t/can-i-edit-html-tag-in-meteor/5867/7
WebApp.addHtmlAttributeHook(function() {
return {
lang: context.locale,
let htmlAttributes = {
lang: context.locale
};
if (locale?.rtl === true) {
htmlAttributes.class = 'rtl';
}
return htmlAttributes;
});

return context;
Expand Down