Can the font be customized based on the language being used?

I have been working on an AngularJS application that needs to support three different languages, each requiring a different font. While using angular-translate for language translation, I encountered the challenge of changing the font dynamically based on the language selection. Initially, I used CSS variables to achieve this functionality by updating the variable value with the desired font-face name upon language change. Unfortunately, this approach did not work in Internet Explorer due to browser limitations. In my quest to find an alternative solution, I came across a helpful resource on how to change font-faces via JavaScript (source). The idea of defining multiple font-faces for different languages seemed promising. For example:

@font-face {
    font-family: 'Regular';
    font-style: normal;
    font-weight: 400;
    src: local('Roboto'), local('Roboto-Regular'), url('../fonts/roboto-regular-webfont.woff2') format('woff2'), url('../fonts/roboto-regular-webfont.woff') format('woff');
    font-display: block;
}
The proposed method involved adding new styles with the same font-family name but different source URLs. However, it raised the issue of having to add a new style for each language switch, which was not ideal. My query is whether there is a way to delete or update font-faces in CSS using JavaScript. Alternatively, I am open to exploring other effective approaches to ensure that font changes along with language changes seamlessly within the application. Your insights and suggestions are highly appreciated.

Answer №1

To implement the code snippet below, you should add it within a controller:

app.controller("fontController", ($scope) => {
    $scope.fonts = {};
    $scope.fonts["en"] = "font-name";
    $scope.fonts["sp"] = "font-name-2";
    $scope.current_lang = "en";
    document.getElementsByTagName("body")[0].style.fontFamily = $scope.fonts[$scope.current_lang];
    //$("body").css("font-family", $scope.fonts[$scope.current_lang]); // using jQuery
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding a button in Node and Mongoose for updating data: A step-by-step guide

I have set up a mongoose database containing events, each event is displayed within a bootstrap card. My challenge now is to find a way to redirect the admin to a page where they can update the event card and save it to my mongoose db. The problem I' ...

Unable to eliminate top padding without using universal selector

Trying to remove the top padding on a webpage, but encountering some difficulty. Here's the HTML code: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <title&g ...

Sending a different name for the jQuery parameter

I'm looking to select CSS settings based on the presence of a data tag. What would be the correct approach for this? var datadirection = 'left'; //default direction if( $(this).attr('data-right') ){ datadirection = 'righ ...

Steps for resolving the error message: "An appropriate loader is required to handle this file type, but there are no loaders currently configured to process it."

I am encountering an issue when working with next.js and trying to export a type in a file called index.ts within a third-party package. Module parse failed: Unexpected token (23:7) You may need an appropriate loader to handle this file type, current ...

Issue with extra spacing within a div element in Bootstrap 5

Why is there extra spacing on the right side of my div? The image and text are supposed to take up 50% of the screen each. However, for some reason, there is additional spacing on the right that prevents this from happening. I am using Bootstrap 5. Click ...

Troubleshooting: Google Tag Manager showing a blank page

After attempting to integrate Google Tag Manager into my website, I encountered a strange issue where upon refreshing the page, it would go completely blank with no specific error displayed. I followed the only upvoted solution from a thread on Stack Over ...

Firebug is indicating that the JavaScript is loading correctly from static files, but unfortunately it is not functioning as expected within the Django framework

After successfully loading the required js files for my template, I am facing an issue where the html elements are not being targeted by the javascript. Strangely, when I include the JS directly in the html file, it works perfectly fine. Can someone explai ...

When using PHP submit, the text input from dynamically generated JS fields is not being posted

My HTML form includes a table of text inputs that I intend to use for generating an SQL INSERT query. The issue I am facing is that my manually inputted table has 5 text inputs, with the option for users to add more. The additional input fields are being c ...

Extracting Query Parameters from a Successful Ajax Call in Javascript

How can I extract data from the success response? Take a look at my code snippet: $.ajax({ async: 'true', url: 'https://exampleapi.com/product', type: 'GET', data: {page: idQuery}, success:(response => { v ...

Using this PHP function

I am facing an issue with a table that contains some information. I want to be able to click on a specific row (e.g. the second row) and display all the corresponding database information. However, I am unsure how to achieve this using the $this function ...

Turn off scroll bar to create a seamless browsing experience on your website

As I work on creating the frontend for a single-page website with seamless scrolling between divs, I'm looking to eliminate mouse scrolling altogether. I understand that using overflow:hidden; can hide scroll bars, but my goal is to make the page scr ...

Mastering the correct application of flex properties within nested flex containers

I'm struggling with utilizing flexbox properly and would like some clarification on how nesting parent and child elements functions. I understand that the child elements inherit the parent's flex properties, but I'm unsure if this inheritan ...

Struggling to resize tables in React Material-UI

Here is my SandBox link for reference: https://codesandbox.io/embed/material-demo-j6pz9?fontsize=14&hidenavigation=1&theme=dark I am attempting to organize content within a card using an inline grid display to have them appear side by side. I unde ...

loading initial data in angularjs

When developing a web application that relies on multiple data sources for every page, what is the most effective way to retrieve the initial data? As observed on Twitter, the tweets that are initially visible on page load are included in the HTML source, ...

Tips for updating the class of a button upon clicking it

I have a dilemma with my two buttons - one is green and the other has no background. I want them to change appearance when clicked, from green to one with a dashed border-bottom style. Below is the HTML code for these buttons: <div class="btns"> ...

Issue with displaying AngularJS option values in select box on Windows tablet with IE browser

Everything is working perfectly fine with binding the data in the select box input option field across all browsers and operating systems, except for Windows Tablet. Has anyone encountered this issue before? Does anyone have any insights on why it might n ...

The VueJS function is not defined

Looking for a way to fetch data from graphql in my vue project and store it in a variable. The function is asynchronous and the value of rawID needs to be awaited. However, there is a possibility that it could result in undefined, causing an error in the ...

Concealing external scripts on specific webpages

In my HTML template, I have integrated a third-party JavaScript code that provides a chat option. This script is included in my header.html so that it appears on all pages. However, I do not want this chat option to display on the login page. I want it to ...

What is the best way to conceal a div when there are no visible children using AngularJS?

I need help with hiding a parent div only if all the child items are invisible. I have successfully hidden the parent when there are no children, but I am struggling to figure out how to hide it based on visibility. <div ng-repeat="group in section.gro ...

Revising Your Task Checklist with React

I encountered an issue while attempting to update the value retrieved from Addlist. Unfortunately, my solution isn't working as expected. Additionally, clicking on the '+' button without entering any text results in an empty list being creat ...