The SharePoint Framework (SPFx) is the versatile player, the libero, best known from the ‘Catenaccio’ tactic, you rely on when extending SharePoint with your own components. I will show you the idea behind SharePoint Framework multilinguality, how it works, and how to avoid common pitfalls.

Table of Contents

SPFx localization

Microsoft has an introduction to localizing SPFx components here 🔗

When you scaffold an SPFx solution, it comes with a standard setup that you can easily extend by adding your own localized files.

SPFx location

The file choice or language selection is then handled by the SharePoint Framework at run-time in the context of your SharePoint Site, Page, or News.

We will take a look at how this plays with your SharePoint Site Language settings and how your libero interacts with your tactical coach."

Localization in SPFx

You may remember from my post about focusing on key languages 🔗 that SharePoint abstracts some of your locale settings, either from your browser or SharePoint profile, and matches these, when possible, with your site language settings.

This also happens for SPFx solutions, but in a different way. Let me explain with some examples, assuming I have an SPFx solution with the following setup in the loc folder:

  • en-US for my English translations
  • fr-FR for my French translations
  • it-CH for my Italian translations for Swiss users

First Example: Matching Languages Through the Whole Chain of Command"

flowchart LR %% Define the default node style (light grey background) classDef defaultStyle fill:#f0f0f0,stroke:#000,stroke-width:1px; %% François's Profile (Europe) subgraph FR ["👤 François's Profile"] A["🇫🇷 1. French (French)"] B["🇮🇹 2. Italian (Switzerland)"] end subgraph CL ["🧑🏼‍🦰 Content language"] AA["🇺🇸 English Default"] BB["🇫🇷 French"] CC["🇮🇹 Italian"] end %% Subgraph for Selected Language subgraph SELL ["🧑🏿‍⚖️ Selected Language"] AAA["🇫🇷 François sees the content in french 🇫🇷 because he has set French which matches in the Site language"] end %% Subgraph for SPFx Selected Language subgraph SPFX ["🛡️ SPFx Language"] CCCC["en-us"] EEEE["fr-FR"] AAAA["it-CH"] end %% François A -- matches --> BB linkStyle 0 stroke-width:3px,stroke:#0aa8a7 B -. lower priority => no match .-> CC linkStyle 1 stroke-width:3px,stroke:#963939,stroke-dasharray: 5 5 %% Show the language selection BB ----> AAA linkStyle 2 stroke-width:3px,stroke:#225f78 %% Show the spfx selection AAA ----> EEEE linkStyle 3 stroke-width:3px,stroke:#225f78 %% Box blue style A fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style AA fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style AAA fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style B fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style BB fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style CC fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style CCCC fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style EEEE fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style AAAA fill:#dae3e7,stroke:#4c788d,stroke-width:3px; %% François's Profile style FR fill:#dab45f,stroke:#cfa63d,stroke-width:1px; %% Content Language style CL fill:#dab45f,stroke:#cfa63d,stroke-width:1px; %% Selected language style SELL fill:#dab45f,stroke:#cfa63d,stroke-width:1px; %% SPFx language style SPFX fill:#dab45f,stroke:#cfa63d,stroke-width:1px;

Second Example: Localization logic in SPFx

Now, let’s assume François changes the content priority in his SharePoint profile, setting Italian (Switzerland) as the first choice, followed by French (France). At this point, SPFx should select the it-CH localization file, but it instead defaults to en-US.

This means François ends up seeing the page in Italian because SharePoint’s multilingual publishing matches the site’s language settings. However, the SPFx component doesn’t recognize this and defaults to the English translation.

flowchart LR %% Define the default node style (light grey background) classDef defaultStyle fill:#f0f0f0,stroke:#000,stroke-width:1px; %% François's Profile (Europe) subgraph FR ["👤 François's Profile"] A["🇮🇹 1. Italian (Switzerland)"] B["🇫🇷 2. French (France)"] end subgraph CL ["🧑🏼‍🦰 Content language"] AA["🇺🇸 English Default"] CC["🇮🇹 Italian"] BB["🇫🇷 French"] end %% Subgraph for Selected Language subgraph SELL ["🧑🏿‍⚖️ Selected Language"] AAA["🇫🇷 François sees the content in italian 🇮🇹 because he has set italian which matches in the Site language"] end %% Subgraph for SPFx Selected Language subgraph SPFX ["🛡️ SPFx Language"] CCCC["en-us"] AAAA["it-CH"] BBBB["it-IT"] EEEE["fr-FR"] end %% François A -- matches --> CC linkStyle 0 stroke-width:3px,stroke:#0aa8a7 B -. lower priority => no match .-> BB linkStyle 1 stroke-width:3px,stroke:#963939,stroke-dasharray: 5 5 %% Show the language selection CC ----> AAA linkStyle 2 stroke-width:3px,stroke:#225f78 %% Show the spfx selection AAA ----> BBBB linkStyle 3 stroke-width:3px,stroke:#225f78 AAA -. should have matched .-> AAAA linkStyle 4 stroke-width:3px,stroke:#0aa8a7 %% Box blue style A fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style AA fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style AAA fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style B fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style BB fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style CC fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style BBBB fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style CCCC fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style EEEE fill:#dae3e7,stroke:#4c788d,stroke-width:3px; style AAAA fill:#dae3e7,stroke:#4c788d,stroke-width:3px; %% François's Profile style FR fill:#dab45f,stroke:#cfa63d,stroke-width:1px; %% Content Language style CL fill:#dab45f,stroke:#cfa63d,stroke-width:1px; %% Selected language style SELL fill:#dab45f,stroke:#cfa63d,stroke-width:1px; %% SPFx language style SPFX fill:#dab45f,stroke:#cfa63d,stroke-width:1px;

The SharePoint Online language switcher

Assuming you have your content in French and English, and your user has set the correct language, French, in their SharePoint profile, they will be presented with the French version of the page they have navigated to, and your SPFx web part, which also has fr-FR as a translation locale, will render French content.

We live in a perfect world until your user decides to switch to the English content. What happens then?

  • Your page with the prepared content in English will be presented. You have likely created text content in English, and possibly referenced specific documents.
  • However, your SPFx web part will still render content in French, despite the page displaying English content.

Serving content in a different language from the page’s content may be a deliberate choice, but I believe people using the language switcher expect the content to match the selected language.

Training the Libero

SPFx localization works in some cases, but it should be enhanced to provide a more understandable and intuitive approach to consuming content. In the next blog post, I will show you how to override the default SPFx behavior and take control of how SPFx chooses the locale and renders its content.