Uncategorized

Adding language id to the body class in Drupal 8

When I started making sites with Drupal 8 I missed a special body class that I sometime need for theming as well as other times, and that’s the language body class. When making multilingual sites this comes in handy sometimes. For Drupal 7 this was made possible by the Internationalization (i18n) module, but since that module has been moved into core in Drupal (source) that special body-class-adding-thingamajig seems to have vanished.

After facing the need of language id when making this site I started to search for a solution, but came up with very few answers. When I solved it I thought I could make a short entry to spread the knowledge.

My needs

Basically, I had two needs for the language class to be added to my theme.

  1. I wanted to hide some of the meny items depending on which language the content was presented in. (I later solved this in another way in my theme, but that was the initial need for a body class representing the current language.)
  2. I have two different RSS-feeds, one for posts in English and one for posts in Swedish. To make it work 100%, the path to the feed needed the language id as well. This was the latter need for me, and also the need that made it to the final site.

The theme

The themes I’ve used for Drupal 8 haven’t had the language flag/path, but some themes may have implemented it, so if you have the path in the body class then you probably don’t need to read this.

The example I’m going to show you is from the the Bootstrap Clean Blog, which I use for this site, and I will show you the code for adding the language id to it.

The files

In the theme folder of Bootstrap Clean Blog you need to locate two files:

  • bootstrap_clean_blog.theme, which is in the root of the theme folder called bootstrap_clean_blog and
  • html.html.twig which is located in the templates folder.

In bootstrap_clean_blog.theme (or any other .theme-file you might have in the theme you’re using/developing) add the following code snippet to it:

function bootstrap_clean_blog_preprocess_html(&$variables) {
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $variables['language'] = $language;
}

If you’re using a different theme than Bootstrap Clean Blog, the name of the theme-file is something like XYZ.theme, where XYZ is the name (lowercase with underscores) of the theme. For example, if you’re using the Pixture Reloaded theme, the theme-file is called pixture_reloaded.theme.

The code above calls for the language by getCurrentLanguage() and assigns the value to the variable $language. After that the value of $language is assigned to the global variable array $variables[‘language’].
You could shorten this down to

function bootstrap_clean_blog_preprocess_html(&$variables) {
  $variables['language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}

but I wrote it on two separate rows to make it more clear. The shorter code snippet is quite allright, though, and you can use that in your .theme-file.

Next up: The TWIG-file

Allright, so far we’ve collected the language and stored it in a global variable. Now it’s time to make it visible on the webpage, to print it. This is done via TWIG, a fast and secure template engine that Drupal 8 started using. To print the $variables[‘language’] you write the following “twig code” in your template where you want it to be printed.

{{ language }}


 

Bitcoin Blogs 501
Bitcoin Blogs 502
Bitcoin Blogs 503
Bitcoin Blogs 504
Bitcoin Blogs 505
Bitcoin Blogs 506
Bitcoin Blogs 507
Bitcoin Blogs 508
Bitcoin Blogs 509
Bitcoin Blogs 510
Bitcoin Blogs 511
Bitcoin Blogs 512
Bitcoin Blogs 513
Bitcoin Blogs 514
Bitcoin Blogs 515
Bitcoin Blogs 516
Bitcoin Blogs 517
Bitcoin Blogs 518
Bitcoin Blogs 519
Bitcoin Blogs 520
Bitcoin Blogs 521
Bitcoin Blogs 522
Bitcoin Blogs 523
Bitcoin Blogs 524
Bitcoin Blogs 525
Bitcoin Blogs 526
Bitcoin Blogs 527
Bitcoin Blogs 528
Bitcoin Blogs 529
Bitcoin Blogs 530
Bitcoin Blogs 531
Bitcoin Blogs 532
Bitcoin Blogs 533
Bitcoin Blogs 534
Bitcoin Blogs 535
Bitcoin Blogs 536
Bitcoin Blogs 537
Bitcoin Blogs 538
Bitcoin Blogs 539
Bitcoin Blogs 540
Bitcoin Blogs 541
Bitcoin Blogs 542
Bitcoin Blogs 543
Bitcoin Blogs 544
Bitcoin Blogs 545
Bitcoin Blogs 546
Bitcoin Blogs 547
Bitcoin Blogs 548
Bitcoin Blogs 549
Bitcoin Blogs 550
Bitcoin Blogs 551
Bitcoin Blogs 552
Bitcoin Blogs 553
Bitcoin Blogs 554
Bitcoin Blogs 555
Bitcoin Blogs 556
Bitcoin Blogs 557
Bitcoin Blogs 558
Bitcoin Blogs 559
Bitcoin Blogs 560
Bitcoin Blogs 561
Bitcoin Blogs 562
Bitcoin Blogs 563
Bitcoin Blogs 564
Bitcoin Blogs 565
Bitcoin Blogs 566
Bitcoin Blogs 567
Bitcoin Blogs 568
Bitcoin Blogs 569
Bitcoin Blogs 570
Bitcoin Blogs 571
Bitcoin Blogs 572
Bitcoin Blogs 573
Bitcoin Blogs 574
Bitcoin Blogs 575
Bitcoin Blogs 576
Bitcoin Blogs 577
Bitcoin Blogs 578
Bitcoin Blogs 579
Bitcoin Blogs 580
Bitcoin Blogs 581
Bitcoin Blogs 582
Bitcoin Blogs 583
Bitcoin Blogs 584
Bitcoin Blogs 585
Bitcoin Blogs 586
Bitcoin Blogs 587
Bitcoin Blogs 588
Bitcoin Blogs 589
Bitcoin Blogs 590
Bitcoin Blogs 591
Bitcoin Blogs 592
Bitcoin Blogs 593
Bitcoin Blogs 594
Bitcoin Blogs 595
Bitcoin Blogs 596
Bitcoin Blogs 597
Bitcoin Blogs 598
Bitcoin Blogs 599
Bitcoin Blogs 600
Bitcoin Blogs 601
Bitcoin Blogs 602
Bitcoin Blogs 603
Bitcoin Blogs 604
Bitcoin Blogs 605
Bitcoin Blogs 606
Bitcoin Blogs 607
Bitcoin Blogs 608
Bitcoin Blogs 609
Bitcoin Blogs 610
Bitcoin Blogs 611
Bitcoin Blogs 612
Bitcoin Blogs 613
Bitcoin Blogs 614
Bitcoin Blogs 615
Bitcoin Blogs 616
Bitcoin Blogs 617
Bitcoin Blogs 618
Bitcoin Blogs 619
Bitcoin Blogs 620
Bitcoin Blogs 621
Bitcoin Blogs 622
Bitcoin Blogs 623
Bitcoin Blogs 624
Bitcoin Blogs 625
Bitcoin Blogs 626
Bitcoin Blogs 627
Bitcoin Blogs 628
Bitcoin Blogs 629
Bitcoin Blogs 630
Bitcoin Blogs 631
Bitcoin Blogs 632
Bitcoin Blogs 633
Bitcoin Blogs 634
Bitcoin Blogs 635
Bitcoin Blogs 636
Bitcoin Blogs 637
Bitcoin Blogs 638
Bitcoin Blogs 639
Bitcoin Blogs 640
Bitcoin Blogs 641
Bitcoin Blogs 642
Bitcoin Blogs 643
Bitcoin Blogs 644
Bitcoin Blogs 645
Bitcoin Blogs 646
Bitcoin Blogs 647
Bitcoin Blogs 648
Bitcoin Blogs 649
Bitcoin Blogs 650
Bitcoin Blogs 651
Bitcoin Blogs 652
Bitcoin Blogs 653
Bitcoin Blogs 654
Bitcoin Blogs 655
Bitcoin Blogs 656
Bitcoin Blogs 657
Bitcoin Blogs 658
Bitcoin Blogs 659
Bitcoin Blogs 660
Bitcoin Blogs 661
Bitcoin Blogs 662
Bitcoin Blogs 663
Bitcoin Blogs 664
Bitcoin Blogs 665
Bitcoin Blogs 666
Bitcoin Blogs 667
Bitcoin Blogs 668
Bitcoin Blogs 669
Bitcoin Blogs 670
Bitcoin Blogs 671
Bitcoin Blogs 672
Bitcoin Blogs 673
Bitcoin Blogs 674
Bitcoin Blogs 675
Bitcoin Blogs 676
Bitcoin Blogs 677
Bitcoin Blogs 678
Bitcoin Blogs 679
Bitcoin Blogs 680
Bitcoin Blogs 681
Bitcoin Blogs 682
Bitcoin Blogs 683
Bitcoin Blogs 684
Bitcoin Blogs 685
Bitcoin Blogs 686
Bitcoin Blogs 687
Bitcoin Blogs 688
Bitcoin Blogs 689
Bitcoin Blogs 690
Bitcoin Blogs 691
Bitcoin Blogs 692
Bitcoin Blogs 693
Bitcoin Blogs 694
Bitcoin Blogs 695
Bitcoin Blogs 696
Bitcoin Blogs 697
Bitcoin Blogs 698
Bitcoin Blogs 699
Bitcoin Blogs 700
Bitcoin Blogs 701
Bitcoin Blogs 702
Bitcoin Blogs 703
Bitcoin Blogs 704
Bitcoin Blogs 705
Bitcoin Blogs 706
Bitcoin Blogs 707
Bitcoin Blogs 708
Bitcoin Blogs 709
Bitcoin Blogs 710
Bitcoin Blogs 711
Bitcoin Blogs 712
Bitcoin Blogs 713
Bitcoin Blogs 714
Bitcoin Blogs 715
Bitcoin Blogs 716
Bitcoin Blogs 717
Bitcoin Blogs 718
Bitcoin Blogs 719
Bitcoin Blogs 720
Bitcoin Blogs 721
Bitcoin Blogs 722
Bitcoin Blogs 723
Bitcoin Blogs 724
Bitcoin Blogs 725
Bitcoin Blogs 726
Bitcoin Blogs 727
Bitcoin Blogs 728
Bitcoin Blogs 729
Bitcoin Blogs 730
Bitcoin Blogs 731
Bitcoin Blogs 732
Bitcoin Blogs 733
Bitcoin Blogs 734
Bitcoin Blogs 735
Bitcoin Blogs 736
Bitcoin Blogs 737
Bitcoin Blogs 738
Bitcoin Blogs 739
Bitcoin Blogs 740
Bitcoin Blogs 741
Bitcoin Blogs 742
Bitcoin Blogs 743
Bitcoin Blogs 744
Bitcoin Blogs 745
Bitcoin Blogs 746
Bitcoin Blogs 747
Bitcoin Blogs 748
Bitcoin Blogs 749
Bitcoin Blogs 750
Bitcoin Blogs 751
Bitcoin Blogs 752
Bitcoin Blogs 753
Bitcoin Blogs 754
Bitcoin Blogs 755
Bitcoin Blogs 756
Bitcoin Blogs 757
Bitcoin Blogs 758
Bitcoin Blogs 759
Bitcoin Blogs 760
Bitcoin Blogs 761
Bitcoin Blogs 762
Bitcoin Blogs 763
Bitcoin Blogs 764
Bitcoin Blogs 765
Bitcoin Blogs 766
Bitcoin Blogs 767
Bitcoin Blogs 768
Bitcoin Blogs 769
Bitcoin Blogs 770
Bitcoin Blogs 771
Bitcoin Blogs 772
Bitcoin Blogs 773
Bitcoin Blogs 774
Bitcoin Blogs 775
Bitcoin Blogs 776
Bitcoin Blogs 777
Bitcoin Blogs 778
Bitcoin Blogs 779
Bitcoin Blogs 780
Bitcoin Blogs 781
Bitcoin Blogs 782
Bitcoin Blogs 783
Bitcoin Blogs 784
Bitcoin Blogs 785
Bitcoin Blogs 786
Bitcoin Blogs 787
Bitcoin Blogs 788
Bitcoin Blogs 789
Bitcoin Blogs 790
Bitcoin Blogs 791
Bitcoin Blogs 792
Bitcoin Blogs 793
Bitcoin Blogs 794
Bitcoin Blogs 795
Bitcoin Blogs 796
Bitcoin Blogs 797
Bitcoin Blogs 798
Bitcoin Blogs 799
Bitcoin Blogs 800
Bitcoin Blogs 801
Bitcoin Blogs 802
Bitcoin Blogs 803
Bitcoin Blogs 804
Bitcoin Blogs 805
Bitcoin Blogs 806
Bitcoin Blogs 807
Bitcoin Blogs 808
Bitcoin Blogs 809
Bitcoin Blogs 810
Bitcoin Blogs 811
Bitcoin Blogs 812
Bitcoin Blogs 813
Bitcoin Blogs 814
Bitcoin Blogs 815
Bitcoin Blogs 816
Bitcoin Blogs 817
Bitcoin Blogs 818
Bitcoin Blogs 819
Bitcoin Blogs 820
Bitcoin Blogs 821
Bitcoin Blogs 822
Bitcoin Blogs 823
Bitcoin Blogs 824
Bitcoin Blogs 825
Bitcoin Blogs 826
Bitcoin Blogs 827
Bitcoin Blogs 828
Bitcoin Blogs 829
Bitcoin Blogs 830
Bitcoin Blogs 831
Bitcoin Blogs 832
Bitcoin Blogs 833
Bitcoin Blogs 834
Bitcoin Blogs 835
Bitcoin Blogs 836
Bitcoin Blogs 837
Bitcoin Blogs 838
Bitcoin Blogs 839
Bitcoin Blogs 840
Bitcoin Blogs 841
Bitcoin Blogs 842
Bitcoin Blogs 843
Bitcoin Blogs 844
Bitcoin Blogs 845
Bitcoin Blogs 846
Bitcoin Blogs 847
Bitcoin Blogs 848
Bitcoin Blogs 849
Bitcoin Blogs 850
Bitcoin Blogs 851
Bitcoin Blogs 852
Bitcoin Blogs 853
Bitcoin Blogs 854
Bitcoin Blogs 855
Bitcoin Blogs 856
Bitcoin Blogs 857
Bitcoin Blogs 858
Bitcoin Blogs 859
Bitcoin Blogs 860
Bitcoin Blogs 861
Bitcoin Blogs 862
Bitcoin Blogs 863
Bitcoin Blogs 864
Bitcoin Blogs 865
Bitcoin Blogs 866
Bitcoin Blogs 867
Bitcoin Blogs 868
Bitcoin Blogs 869
Bitcoin Blogs 870
Bitcoin Blogs 871
Bitcoin Blogs 872
Bitcoin Blogs 873
Bitcoin Blogs 874
Bitcoin Blogs 875
Bitcoin Blogs 876
Bitcoin Blogs 877
Bitcoin Blogs 878
Bitcoin Blogs 879
Bitcoin Blogs 880
Bitcoin Blogs 881
Bitcoin Blogs 882
Bitcoin Blogs 883
Bitcoin Blogs 884
Bitcoin Blogs 885
Bitcoin Blogs 886
Bitcoin Blogs 887
Bitcoin Blogs 888
Bitcoin Blogs 889
Bitcoin Blogs 890
Bitcoin Blogs 891
Bitcoin Blogs 892
Bitcoin Blogs 893
Bitcoin Blogs 894
Bitcoin Blogs 895
Bitcoin Blogs 896
Bitcoin Blogs 897
Bitcoin Blogs 898
Bitcoin Blogs 899
Bitcoin Blogs 900
Bitcoin Blogs 901
Bitcoin Blogs 902
Bitcoin Blogs 903
Bitcoin Blogs 904
Bitcoin Blogs 905
Bitcoin Blogs 906
Bitcoin Blogs 907
Bitcoin Blogs 908
Bitcoin Blogs 909
Bitcoin Blogs 910
Bitcoin Blogs 911
Bitcoin Blogs 912
Bitcoin Blogs 913
Bitcoin Blogs 914
Bitcoin Blogs 915
Bitcoin Blogs 916
Bitcoin Blogs 917
Bitcoin Blogs 918
Bitcoin Blogs 919
Bitcoin Blogs 920
Bitcoin Blogs 921
Bitcoin Blogs 922
Bitcoin Blogs 923
Bitcoin Blogs 924
Bitcoin Blogs 925
Bitcoin Blogs 926
Bitcoin Blogs 927
Bitcoin Blogs 928
Bitcoin Blogs 929
Bitcoin Blogs 930
Bitcoin Blogs 931
Bitcoin Blogs 932
Bitcoin Blogs 933
Bitcoin Blogs 934
Bitcoin Blogs 935
Bitcoin Blogs 936
Bitcoin Blogs 937
Bitcoin Blogs 938
Bitcoin Blogs 939
Bitcoin Blogs 940
Bitcoin Blogs 941
Bitcoin Blogs 942
Bitcoin Blogs 943
Bitcoin Blogs 944
Bitcoin Blogs 945
Bitcoin Blogs 946
Bitcoin Blogs 947
Bitcoin Blogs 948
Bitcoin Blogs 949
Bitcoin Blogs 950
Bitcoin Blogs 951
Bitcoin Blogs 952
Bitcoin Blogs 953
Bitcoin Blogs 954
Bitcoin Blogs 955
Bitcoin Blogs 956
Bitcoin Blogs 957
Bitcoin Blogs 958
Bitcoin Blogs 959
Bitcoin Blogs 960
Bitcoin Blogs 961
Bitcoin Blogs 962
Bitcoin Blogs 963
Bitcoin Blogs 964
Bitcoin Blogs 965
Bitcoin Blogs 966
Bitcoin Blogs 967
Bitcoin Blogs 968
Bitcoin Blogs 969
Bitcoin Blogs 970
Bitcoin Blogs 971
Bitcoin Blogs 972
Bitcoin Blogs 973
Bitcoin Blogs 974
Bitcoin Blogs 975
Bitcoin Blogs 976
Bitcoin Blogs 977
Bitcoin Blogs 978
Bitcoin Blogs 979
Bitcoin Blogs 980
Bitcoin Blogs 981
Bitcoin Blogs 982
Bitcoin Blogs 983
Bitcoin Blogs 984
Bitcoin Blogs 985
Bitcoin Blogs 986
Bitcoin Blogs 987
Bitcoin Blogs 988
Bitcoin Blogs 989
Bitcoin Blogs 990
Bitcoin Blogs 991
Bitcoin Blogs 992
Bitcoin Blogs 993
Bitcoin Blogs 994
Bitcoin Blogs 995
Bitcoin Blogs 996
Bitcoin Blogs 997
Bitcoin Blogs 998
Bitcoin Blogs 999
Bitcoin Blogs 1000

Related Articles

Leave a Reply

Back to top button