Skip to content

Commit d312016

Browse files
authored
feat(loadNonLatinPlex): introduce loadNonLatinPlex utility (#6802)
### Related Ticket(s) Refs #5025 ### Description This utility will dynamically load in non-Latin fonts, as well as setting the font-family to the page accordingly. This leverages the existing Plex CDN fonts. In addition, the globalInit introduced the execution of this code, so this ensures that any page that loads in the Masthead or Footer will do this check and load in the fonts accordingly. The `react` and `web-components` storybooks have been updated to properly set the language/locale codes so that testing of the language selector can be done in Storybook. ### Changelog **New** - `loadNonLatinPlex` utility **Changed** - Added `loadNonLatinPlex` logic to `globalInit` - Added proper language/locale setting in storybooks
1 parent b9a0920 commit d312016

File tree

7 files changed

+548
-275
lines changed

7 files changed

+548
-275
lines changed

packages/react/.storybook/preview-head.html

+174-138
Large diffs are not rendered by default.

packages/services/src/services/global/global.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
2-
* Copyright IBM Corp. 2020
2+
* Copyright IBM Corp. 2020, 2021
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

88
import { AnalyticsAPI } from '../Analytics';
99
import { DDOAPI } from '../DDO';
10+
import loadNonLatinPlex from '@carbon/ibmdotcom-utilities/es/utilities/loadNonLatinPlex/loadNonLatinPlex';
11+
import { LocaleAPI } from '../Locale';
1012

1113
/**
1214
* Flag to determine if the global init has been fired
@@ -34,6 +36,11 @@ export function globalInit() {
3436
);
3537
});
3638

39+
// Sets the Plex font for non-Latin fonts
40+
LocaleAPI.getLang().then(lang => {
41+
loadNonLatinPlex(lang.lc);
42+
});
43+
3744
// analytics tracking
3845
AnalyticsAPI.initAll();
3946
}

packages/utilities/src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from './featureflag';
1414
export * from './formatVideoCaption';
1515
export * from './geolocation';
1616
export * from './ipcinfoCookie';
17+
export * from './loadNonLatinPlex';
1718
export * from './markdownToHtml';
1819
export * from './removeHtmlTagEntities';
1920
export * from './sameHeight';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copyright IBM Corp. 2021
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { loadNonLatinPlex } from '../';
9+
10+
/**
11+
* Cleans the page after testing
12+
*
13+
* @private
14+
*/
15+
function _cleanPage() {
16+
document.body.style.fontFamily = '';
17+
document.getElementsByTagName('head')[0].innerHTML = '';
18+
}
19+
20+
describe('Load plex utility | Arabic', () => {
21+
beforeAll(() => {
22+
loadNonLatinPlex('ar');
23+
});
24+
25+
afterAll(() => {
26+
_cleanPage();
27+
});
28+
29+
it('should set Plex Arabic font-family on the page', () => {
30+
expect(document.body.style.fontFamily).toBe(
31+
'IBM Plex Sans Arabic,IBM Plex Sans,Helvetica Neue,Arial,sans-serif'
32+
);
33+
});
34+
35+
it('should load Plex Arabic font css on the page', () => {
36+
const headContent = document.getElementsByTagName('head')[0].innerHTML;
37+
const fontLink =
38+
'https://1.www.s81c.com/common/carbon-for-ibm-dotcom/plex/sans-arabic.css';
39+
40+
expect(headContent.indexOf(fontLink)).not.toEqual(-1);
41+
});
42+
});
43+
44+
describe('Load plex utility | Japanese', () => {
45+
beforeAll(() => {
46+
loadNonLatinPlex('ja');
47+
});
48+
49+
afterAll(() => {
50+
_cleanPage();
51+
});
52+
53+
it('should set Plex Japanese font-family on the page', () => {
54+
expect(document.body.style.fontFamily).toBe(
55+
'IBM Plex Sans JP,IBM Plex Sans,Helvetica Neue,Arial,sans-serif'
56+
);
57+
});
58+
59+
it('should load Plex Japanese font css on the page', () => {
60+
const headContent = document.getElementsByTagName('head')[0].innerHTML;
61+
const fontLink =
62+
'https://1.www.s81c.com/common/carbon-for-ibm-dotcom/plex/sans-jp.css';
63+
64+
expect(headContent.indexOf(fontLink)).not.toEqual(-1);
65+
});
66+
});
67+
68+
describe('Load plex utility | Korean', () => {
69+
beforeAll(() => {
70+
loadNonLatinPlex('ko');
71+
});
72+
73+
afterAll(() => {
74+
_cleanPage();
75+
});
76+
77+
it('should set Plex Korean font-family on the page', () => {
78+
expect(document.body.style.fontFamily).toBe(
79+
'IBM Plex Sans KR,IBM Plex Sans,Helvetica Neue,Arial,sans-serif'
80+
);
81+
});
82+
83+
it('should load Plex Korean font css on the page', () => {
84+
const headContent = document.getElementsByTagName('head')[0].innerHTML;
85+
const fontLink =
86+
'https://1.www.s81c.com/common/carbon-for-ibm-dotcom/plex/sans-kr.css';
87+
88+
expect(headContent.indexOf(fontLink)).not.toEqual(-1);
89+
});
90+
});
91+
92+
describe('Load plex utility | English', () => {
93+
beforeAll(() => {
94+
loadNonLatinPlex('en');
95+
});
96+
97+
it('should not set anything if passing `en`', () => {
98+
const links = document.getElementsByTagName('link').length;
99+
const family = document.body.style.fontFamily;
100+
101+
expect(links).toEqual(0);
102+
expect(family).toBe('');
103+
});
104+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2021
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as loadNonLatinPlex } from './loadNonLatinPlex';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright IBM Corp. 2021
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
/**
9+
* CDN host for plex fonts
10+
*
11+
* @type {string}
12+
* @private
13+
*/
14+
const _host = 'https://1.www.s81c.com/common/carbon-for-ibm-dotcom/plex';
15+
16+
/**
17+
* Non-Latin font keys and corresponding entry file/font-family
18+
*
19+
* @type {{ar: {entry: string, family: string}, jp: {entry: string, family: string}, kr: {entry: string, family: string}}}
20+
* @private
21+
*/
22+
const _fonts = {
23+
ar: {
24+
entry: 'sans-arabic',
25+
family: 'IBM Plex Sans Arabic',
26+
},
27+
ja: {
28+
entry: 'sans-jp',
29+
family: 'IBM Plex Sans JP',
30+
},
31+
ko: {
32+
entry: 'sans-kr',
33+
family: 'IBM Plex Sans KR',
34+
},
35+
};
36+
37+
/**
38+
* Injects the corresponding CSS entry point to the page
39+
*
40+
* @param {string} language two-character language code
41+
* @private
42+
*/
43+
function _injectCSS(language) {
44+
const link = document.createElement('link');
45+
link.href = `${_host}/${_fonts[language].entry}.css`;
46+
link.type = 'text/css';
47+
link.rel = 'stylesheet';
48+
link.media = 'screen,print';
49+
50+
document.getElementsByTagName('head')[0].appendChild(link);
51+
}
52+
53+
/**
54+
* Sets the language's font-family to the page
55+
*
56+
* @param {string} language two-character language code
57+
* @private
58+
*/
59+
function _setFontFamily(language) {
60+
document.body.style.fontFamily = `${_fonts[language].family},IBM Plex Sans,Helvetica Neue,Arial,sans-serif`;
61+
}
62+
63+
/**
64+
* Utility to load in the corresponding non-Latin Plex font if necessary
65+
*
66+
* @example
67+
* import { loadNonLatinPlex } from '@carbon/ibmdotcom-utilities';
68+
*
69+
* loadNonLatinPlex('ar');
70+
*
71+
* @param {string} language two-character language code
72+
*/
73+
function loadNonLatinPlex(language) {
74+
if (_fonts[language]) {
75+
_injectCSS(language);
76+
_setFontFamily(language);
77+
}
78+
}
79+
80+
export default loadNonLatinPlex;

0 commit comments

Comments
 (0)