microsoft / azure-devops-extension-sdk

Client SDK for developing Azure DevOps extensions
MIT License
125 stars 39 forks source link

No public API to get UI culture info #62

Open Opiumtm opened 1 year ago

Opiumtm commented 1 year ago

Problem: Need to support different languages (Russian, English and German) in a work item control extension.

There is no public API to get locale that configured in user profile. Old VSS SDK had API for this info:

var culture = TFS.uiCulture.toLowerCase();

New SDK does not have public methods to get configured work item form UI culture info.

In azure-devops-ui there is a function to get current culture

import { getCurrentCulture } from "azure-devops-ui/Core/Util/Culture"

defined as

/**
 * Get culture settings for the current user's preferred culture
 */
export declare function getCurrentCulture(): ICultureInfo;

...but it just doesn't work and always returns invariant culture (en-US) regardless of configured UI culture. Because if we look at JS code for this function, we see..

var currentCulture;
/**
 * Get culture settings for the current user's preferred culture
 */
export function getCurrentCulture() {
    if (!currentCulture) {
        currentCulture = getInvariantCulture();
    }
    return currentCulture;
}

...and module variable currentCulture is nowhere and never initialized, so function getCurrentCulture always returns invariant culture info.

So to solve business problem, I forced to resort to dirty hacks such as patching SDK script to retrieve configured culture from previously private "handshake info" provided to SDK.init function internally.

/*
* Dirty hack here! 
* SDK.getPageContext() isn't available, but a function injected in SDK module JavaScript source, and then patched 
* SDK module aliased in webpack pretending to be original azure-devops-extension-sdk module
*/
function getLocale(): string {
    return SDK.getPageContext()?.globalization?.culture;
}

Such dirty hacks for solving ordinary problems is not what we would like to have from the new SDK.