02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Localizing your site with language selectors

You might speak only one language, but yet there are differences in the way users expect content to look like when they are of different countries. The language attribute enables you to create small custom rules that would apply to specific sub-languages at ease without needing to create separate pages for each sub language (such as American English vs. British English). By the end of this lesson, you will know the basics of how to work with the lang attribute in HTML and the lang-CSS selector.

properties: CSS2 language pseudo-class rules, lang-CSS

Video Transcript

00:00In the last few lectures, we were talking about CSS attribute selectors.
00:05We're going to go back to CSS2 now, and we're going to visit the lang attribute.
00:10Well that lang selector.
00:11It's a sudo selector, it's anything that starts with a round bracket, which is going to be
00:16the first one that we see.
00:17It looks like a function if you know JavaScript.
00:19Basically, it is a more advanced feature that is not going to support a lot of other things
00:24that we're going to see.
00:25You can't nest advanced features one inside of each other.
00:29You cannot nest inside of anything that we're going to see later on, the selector that we're
00:34going to see currently.
00:36What does it do?
00:37The language enables us to create customizations based on a language in our CSS.
00:43In general, Google suggests, and most search engines suggest, that you separate your HTML
00:48pages per language.
00:49Don't serve up the same HTML page that will serve up to your customers or users that speak
00:54completely different languages.
00:57With that said, my personal suggestion to you is if you have a brand and you don't have
01:02the capability of maintaining completely separate entities, for example, your French users versus
01:07your Canadian French users, but you do want to add a little bit layer of customization
01:13such as in headlines, where you just want to give those users a little bit of a level
01:17of personalizations to those communities, the language attribute could become very, very
01:22useful.
01:23We're going to take a look at that in this lecture.
01:25By the end of this lecture, you're going to know everything you need to know about the
01:28language attributes and selectors and what can you do with them and what can't you do
01:34with them.
01:35Let's jump right into it and see it in action.
01:40We're taking a break from a lot of CSS rules and we're going to focus on one.
01:45It's a little bit of a practice.
01:46It's a CSS2 selector, which enables us to control languages.
01:51While we were away, I've added three new headlines.
01:54One with bicycle in American English, a second with biciclet, biciclet, I believe, which
02:01is Canadian French and vello in French French, which all mean bicycle.
02:07But really, if you're creating a website and imagine that it's in French, or even
02:11in that case, if you're in American English or British English, but you don't want to
02:15create a brand completely new page for the content.
02:18Imagine that there's a headline that's talking about underwear while in the United
02:21States it would be called underwear, but in the UK and a lot of other regions in the
02:26world, it would be called pants.
02:28So if you want to create the same page and there's just some small changes that you want
02:32to create to the page, there are ways for you to define those small little differences.
02:38You probably wouldn't put a lot of different languages in one page from a level of you
02:42don't want to load up the page with too much information, but if it's small things
02:45such as headlines where you want to create a little bit of customization based on where
02:50the user is, the lang attribute, it becomes incredibly powerful.
02:54Now I want to show you the lang attribute in general in HTML, and you'll notice in all
02:59of our documents in our defining element in our HTML, we defined it to be in English.
03:05I'm going to go ahead and change that to FR because our document is actually going
03:08to be based off of French, even though that it is very much in English.
03:14Now if you scroll down and find our headlines that we've just added in, you'll notice
03:17that I've added a language attribute to each of them.
03:21One is French Canadian, another is French French, and what I want to do next is I want
03:26to basically define a specific styling rule and to be able to control and gain access
03:32to specific elements.
03:34So for example, if I go ahead and say in all of this document that speaks French,
03:40I want to build a new rule.
03:42And this new rule that I want to build is very explicitly to everything that is French,
03:46assuming we're on the French page, because the CSS could be used both for the French
03:51language and many other languages as well.
03:53In this case, let's do something relatively theoretical.
03:57Maybe we know that people that speak French really love it when their backgrounds are red.
04:02In that case, we would go ahead, and I'm just going to go ahead here
04:05and just create here another CSS2 rules for language.
04:10So let me just find here a spot for that, and I'm just going to paste it right here,
04:14and we're talking about CSS2 language selectors.
04:19Now, the rule was incredibly easy.
04:22I first defined what property do I want.
04:24In this case, really, I want everything.
04:26So I'm going to start it with a star there, and I'm going to set the language
04:30by setting the property lang, opening up a round bracket,
04:34and then defining the language itself.
04:36In this case, I want everything that is French to be highlighted,
04:40let's say I want its background to be red.
04:44Now, in this case, once I define the French, anything that is French,
04:47for its background to be red, let's see what would happen.
04:51I'm going to click here, refresh, and notice how everything now turned French
04:55because everything in our document is French.
04:58That's not really true, right?
04:59So let's go ahead here and revert back our full document to be in English.
05:04And notice where the red color will be defined this time around.
05:08This time around, only the French-related languages,
05:12such as bicycle, which was in Canadian French,
05:14notice that differentiator, where it was FR-CA for Canadian French,
05:20or FR-FR for French French.
05:23Now, Parisian French, more accurately, or European-style French.
05:28In this case, if I only wanted for French that is Canadian to be red,
05:32then maybe I would highlight that Canadian French to be in red.
05:35A more realistic scenario is you might want to basically assume
05:40that if the ad, usually you would do this in JavaScript,
05:43or if, for example, anything that is Canadian French, let's hide.
05:48So I would go ahead there, and instead of setting it to be red,
05:51I would say display none.
05:53And I basically get rid of that item
05:55because I know that I'm only interested in showing the French language item.
06:01So anything that has a language separator
06:03that is not that specific language,
06:05then would then be automatically removed.
06:07Now, obviously, something like that I would probably do
06:10with the help of JavaScript.
06:12And in that case, or in that case,
06:15I would add an extra logic into the HTML page itself
06:18and inject just those extra words
06:21that would just get rid of a specific language
06:23that is not being controlled right now.
06:26JavaScript would probably be a really good scenario for this
06:28because if someone does not support JavaScript,
06:30they'll just see both headlines.
06:32But if someone does support JavaScript,
06:33then we'll go ahead and remove the content
06:35that is not relevant to them in that specific time.
06:38So we're not going to cover that
06:40because that is a separate title which is working in JavaScript.
06:43But before we wrap up this conversation about languages,
06:46I want to talk about one more thing.
06:50We've met already before, earlier,
06:53we've met the possibility of creating different CSS access rules.
06:57And one of those access rules that we've met not long ago
07:01was the pipe.
07:02And we said the pipe basically enables us to select
07:04an item, select an item that starts with a specific value
07:09that is dash separated, remember?
07:11So if I didn't want to do French, for example, here,
07:14and I'm just going to go ahead here
07:15and just comment this out here for a second.
07:18And if instead I went ahead and said,
07:20hey, by the way, anything that starts with anything, right,
07:24that has a property which is called lang,
07:28that its value is starting with the value of fr,
07:33then we want to do something too.
07:35So for example, we want to set that value to be display none.
07:40Or in this case, really, let's set it exactly
07:42to be the same type of sample with frca.
07:46It works very similarly, but the difference is quite big.
07:50Now, you're not going to recognize
07:52the difference immediately.
07:53If we go ahead and we click on refresh,
07:55we'll notice that we're still getting rid
07:57of that Canadian French.
07:59But the only big difference between the two is this.
08:02If I went ahead here and I set to be,
08:04for example, I set our whole document
08:08to be inside of that specific language.
08:11So I'm going to go ahead here to my language
08:13and I'm going to set the language
08:14to be frca, Canadian French.
08:17Our whole document is Canadian French.
08:19Now again, instead of making it invisible,
08:22let me just use that red color
08:23because we don't want our whole document to disappear.
08:27So let me just delete that.
08:29I'm going to go ahead here, set that background to be red.
08:31And if I click on refresh, notice what happens.
08:34Notice that even though I've selected
08:36supposedly everything and everything here
08:38in this document is now French
08:39because I defined the whole document to be red, right?
08:43Oh, excuse me.
08:44The whole document to be Canadian French.
08:46So I would assume that everything here
08:48would be Canadian French, but not true.
08:50Because I was using a property directly,
08:52so it's true, the whole body, the whole page is red.
08:56That paragraph is red, the whole thing is red,
09:00but everything else is not.
09:02And that is the big difference
09:04or the small difference really
09:05between the language attribute
09:09when you're defining it directly with the language itself
09:12versus not using the language
09:13and instead of that using an attribute.
09:16So in this case, when we had this starting with FR,
09:19then everything that would be in French would start,
09:21but it would still not solve our problem
09:24or our desire if we said
09:25that the whole document is French
09:27and we set the whole document to be French.
09:30This would not work.
09:31This would only work in a scenario
09:33where we only want specific items
09:35that are defined in that rule.
09:38But really, many times what we would really want
09:41is for example, if we have this FR,
09:42but inside there are sub, div, sub, whatever's inside there,
09:46they would not be affected.
09:47Only the parent itself would be affected.
09:50So if we want all to be affected,
09:52when we're using the language by default,
09:54it will automatically affect all of the items.
09:58So that's really the big difference
09:59between the FR, the language itself,
10:03using the language versus using the property definers
10:06that we've met earlier.
10:08So here we go.
10:09If we set it, we'll see that all of the page
10:10will be affected and I'm just gonna set here
10:13because we set it to be display none.
10:14So that made the whole document invisible,
10:16which is obviously not what we want.
10:18So I'm gonna go ahead there back into our HTML document
10:21to make sure that we're actually defining it
10:23to be English as a default.
10:26And there we go.
10:27That's it.
10:28That's the core of working with language in general.
10:31We're not done with that topic completely,
10:33although we've seen how to work with it now
10:35inside of the laws that govern CSS2,
10:38but now that we're living in the CSS3 world,
10:41is there a better, more dynamic way
10:43where we could automatically get rid of all the languages
10:46that are not that specific language?
10:48So I put that on you as a homework assignment.
10:50You could use JavaScript or you could use CSS.
10:53Try to figure out a way
10:55before you watch the next few lectures.
10:57Try to find a way, how could you get rid
10:59of all the irrelevant languages,
11:01all the languages that are not the default language
11:03that we want?
11:04I'm fine with you having the default language
11:07that you want up above here.
11:09What we want is everything if we,
11:10let's say I set it to be French, French.
11:12This is the language I want this application to be in.
11:15This page should be rendered in French, French,
11:17which what this means basically is
11:19whenever I serve the French, French people
11:21or the Canadian French,
11:22all I have to do is change this HTML tab.
11:25All the rest is completely the same.
11:27If I change this one thing or I do it with JavaScript,
11:30then that's all I have to change.
11:32And in that case,
11:33we want to get rid of all the stuff that are not.
11:34So anything that is Lang English would get this,
11:37would disappear.
11:37Anything that's Lang CA would disappear.
11:39And that's your goal.
11:40Try to find out,
11:41is there a way for you to do this in a dynamic way?
11:43How about starting without a dynamic way?
11:46But if you know a little bit of JavaScript, go for it.
11:49If you don't know JavaScript,
11:50try to search up the selectors.
11:52We look into our referrals in the end of the chapter,
11:54try to figure it out before you watch the lecture
11:56because that's one of the things
11:57that we're gonna encounter very soon.
11:59So see you in the next lecture
12:01where we continue exploring rules of CSS
12:04and I hope you will take that break
12:06and take up on that challenge.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!