TIL: Open Visual Studio Code from command line

Quick tip to make work a bit simpler: Visual Studio Code can be executed from the command line by running the “code” command.

It is especially useful when running like this: “code .” as it opens the VS Code with the current folder. This allows to quickly open a project in VS Code from the command line interface.

Alternatively just running “code” opens an empty instance of the editor, and running “code file_name” opens VS Code with the specified file opened in it.

Advertisements

TIL: Generate C# classes out of JSON or XML in Visual Studio 2015

You can quickly generate C# classes out of JSON or XML markup in Visual Studio 2015. This might come in handy if you have a response from a REST API in JSON format and want to work with it in your C# application.

Note: This option is not available in Visual Studio Code. If you are using it you might want to use online services available.

The process is straightforward: Copy the source JSON or XML into the clipboard. Then in Visual Studio, select Edit->Paste Special->Paste JSON as Classes:

TIL - paste json as classes

Classes declarations will be inserted into the currently opened file. Additional refactoring is usually needed after that, but it will get you started.

As an example, let’s consider the following JSON:

{
    "employees": [
        { "firstName": "John", "lastName": "Doe" },
        { "firstName": "Anna", "lastName": "Smith" },
        { "firstName": "Peter", "lastName": "Jones " }
    ]
}

Visual Studio will generate the following C# code:

public class Rootobject
{
    public Employee[] employees { get; set; }
}

public class Employee
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

TIL: i18next resources are case sensitive

i18next is a popular internationalization JavaScript library.

One thing to remember when you work with it, is that¬†languages’ names are case sensitive.

The case of a language you pass to the i18next’s init function and the case of a language in your resources should be the same. Otherwise your translations won’t work correctly.

For example, the code below won’t work as expected, as the case of the language "en" in resources and "EN" in init function do not math. i18next will return the key name (i.e. "helloKey") instead of the correct translation (i.e. "hello world"):

const resources = {
    en: {
        translation: {
            "helloKey": "hello world"
        }
    }
};

i18next.init({
    lng: 'EN',
    resources: resources
}, (err, t) => {
    // This will NOT work as expected.
    // "en" in resources and "EN" in init function do not match
    // helloWorld = "helloKey" and not "hello world".
    const helloWorld = i18next.t("helloKey");
});

Note: This applies to all resources elements: not only languages, but namespaces and keys as well.

This is specially important when you get the language context from the environment and not setting it directly. Let assume that you have got a 3rd party environment that provides your code with the correct locale:

// Get the language from the custom environment
// We don't know the case of the language here:
// could be "en", "En", "EN", etc.
let environmentLanguage = Custom_System.context.displayLanguage;

i18next.init({
    lng: environmentLanguage,
    resources: resources
});

The advice here is to stick to a particular notation with your resource files.
For example:
1. Always use lowercase for languages in your resource files.
2. Before setting a language in i18next make it lowercase to make sure that it matches your resource.

Happy Coding!