Skip to content

Commit a1e34df

Browse files
authored
Use 'en' codelabs on translated pages (GoogleChrome#7470)
* Use 'en' codelabs on translated pages * Switch to defaultLanguage const * Improve tests
1 parent 72cd097 commit a1e34df

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/site/_includes/components/CodelabsCallout.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@
1515
*/
1616

1717
const {html} = require('common-tags');
18+
19+
const {defaultLanguage} = require('../../../lib/utils/language');
1820
const {findByUrl} = require('../../_filters/find-by-url');
1921
const md = require('../../_filters/md');
2022

23+
/**
24+
* Find matching collection items given a language code.
25+
*
26+
* @param {string[]} slugs
27+
* @param {string} lang
28+
* @returns {EleventyCollectionItem[]}
29+
*/
30+
function filterCodelabsByLang(slugs, lang) {
31+
return slugs
32+
.map((slug) => findByUrl(`/${lang}/${slug}/`))
33+
.filter((item) => item); // filter out any undefined entries
34+
}
35+
2136
/**
2237
* Generates codelab links HTML.
2338
*
@@ -29,12 +44,15 @@ function CodelabsCallout(slugs, lang) {
2944
// Coerce slugs to Array just in case someone pasted in a single slug string.
3045
slugs = slugs instanceof Array ? slugs : [slugs];
3146

32-
const codelabs = slugs
33-
.map((slug) => findByUrl(`/${lang}/${slug}/`))
34-
.filter((item) => item); // filter out any undefined entries
35-
36-
if (!codelabs.length) {
37-
return;
47+
let codelabs = filterCodelabsByLang(slugs, lang);
48+
// If there's no language-specific codelab, returning the default (English)
49+
// language one is preferable.
50+
if (codelabs.length === 0) {
51+
codelabs = filterCodelabsByLang(slugs, defaultLanguage);
52+
}
53+
// If there's still no codelabs found, return an empty string (not undefined).
54+
if (codelabs.length === 0) {
55+
return '';
3856
}
3957

4058
return `

test/unit/src/site/_includes/components/CodelabsCallout.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
const {expect} = require('chai');
1919
const cheerio = require('cheerio');
20+
21+
const {defaultLanguage} = require('../../../../../../src/lib/utils/language');
2022
const {memoize} = require('../../../../../../src/site/_filters/find-by-url');
2123
const CodelabsCallout = require('../../../../../../src/site/_includes/components/CodelabsCallout');
2224

@@ -42,12 +44,19 @@ describe('CodelabsCallout', function () {
4244
expect(li.length).to.equal(3);
4345
});
4446

45-
it('returns nothing if URLs not found', async function () {
47+
it('returns an empty string if URLs not found', async function () {
4648
const html = CodelabsCallout(
4749
collectionAll.map((i) => i.url + '/pop'),
4850
'en',
4951
);
50-
expect(html).to.equal(undefined);
52+
expect(html).to.equal('');
53+
});
54+
55+
it('returns the default-language codelab as a fallback', async function () {
56+
const html = CodelabsCallout('foobar', 'zz');
57+
const $ = cheerio.load(html);
58+
expect($('a').attr('href')).to.equal(`/${defaultLanguage}/foobar/`);
59+
expect($('span').text()).to.equal('foobar');
5160
});
5261
});
5362

0 commit comments

Comments
 (0)