{"id":2695,"date":"2020-05-22T12:02:31","date_gmt":"2020-05-22T11:02:31","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2695"},"modified":"2020-05-22T12:02:33","modified_gmt":"2020-05-22T11:02:33","slug":"getting-the-current-language-code-in-a-mendix-app-from-javascript","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/","title":{"rendered":"Getting the current language code in a Mendix app from JavaScript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When we&#8217;re working on a multi language application, we need to be aware of the user&#8217;s language ensure they receive content in their own language.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mendix has great language support, and it also has great caching on the client side. These can cause problems together when you change the language of a user in your application, it may not always be reflected back to local cache.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a way around this, we can use the <a href=\"https:\/\/apidocs.rnd.mendix.com\/8\/client\/mx.data.html\">Mendix Client API<\/a> to force a retrieve of the data from the application. We need to use an XPath request to ensure we bypass the cache.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let xPath = \"\/\/System.Language&#91;System.User_Language=\\\"\" + mx.session.getUserId() + \"\\\"]\";<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We have the current user&#8217;s session ID, so we can retrieve the System.Language using this a constraint on the System.User_Language association. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we can just use an mx.data.get call to retrieve it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mx.data.get({\n    xpath: xPath,\n    filter: {\n        amount: 1\n    }, \n    callback: function(obj) { \n        let lang = obj&#91;0].jsonData.attributes.Code.value;\n        console.log(\"Current language is \" + lang);\n    }\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we are just echoing the language code back to the user on the console.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we&#8217;re working on a multi language application, we need to be aware of the user&#8217;s language ensure they receive content in their own language. Mendix has great language support, and it also has great caching on the client side. These can cause problems together when you change the language of a user in your &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Getting the current language code in a Mendix app from JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2699,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,105,98],"class_list":["post-2695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","tag-javascript","tag-locale","tag-mendix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Getting the current language code in a Mendix app from JavaScript - Robert Price<\/title>\n<meta name=\"description\" content=\"How to get the current locale language code for a user in a Mendix application using JavaScript. Full working example included.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting the current language code in a Mendix app from JavaScript - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to get the current locale language code for a user in a Mendix application using JavaScript. Full working example included.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-22T11:02:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-22T11:02:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/language_xpath.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Getting the current language code in a Mendix app from JavaScript\",\"datePublished\":\"2020-05-22T11:02:31+00:00\",\"dateModified\":\"2020-05-22T11:02:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/\"},\"wordCount\":170,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/language_xpath.jpg\",\"keywords\":[\"JavaScript\",\"Locale\",\"Mendix\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/\",\"name\":\"Getting the current language code in a Mendix app from JavaScript - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/language_xpath.jpg\",\"datePublished\":\"2020-05-22T11:02:31+00:00\",\"dateModified\":\"2020-05-22T11:02:33+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to get the current locale language code for a user in a Mendix application using JavaScript. Full working example included.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/language_xpath.jpg\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/language_xpath.jpg\",\"width\":1200,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/getting-the-current-language-code-in-a-mendix-app-from-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting the current language code in a Mendix app from JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Getting the current language code in a Mendix app from JavaScript - Robert Price","description":"How to get the current locale language code for a user in a Mendix application using JavaScript. Full working example included.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/","og_locale":"en_GB","og_type":"article","og_title":"Getting the current language code in a Mendix app from JavaScript - Robert Price","og_description":"How to get the current locale language code for a user in a Mendix application using JavaScript. Full working example included.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/","og_site_name":"Robert Price","article_published_time":"2020-05-22T11:02:31+00:00","article_modified_time":"2020-05-22T11:02:33+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/language_xpath.jpg","type":"image\/jpeg"}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Getting the current language code in a Mendix app from JavaScript","datePublished":"2020-05-22T11:02:31+00:00","dateModified":"2020-05-22T11:02:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/"},"wordCount":170,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/language_xpath.jpg","keywords":["JavaScript","Locale","Mendix"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/","name":"Getting the current language code in a Mendix app from JavaScript - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/language_xpath.jpg","datePublished":"2020-05-22T11:02:31+00:00","dateModified":"2020-05-22T11:02:33+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to get the current locale language code for a user in a Mendix application using JavaScript. Full working example included.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/language_xpath.jpg","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/language_xpath.jpg","width":1200,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/getting-the-current-language-code-in-a-mendix-app-from-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Getting the current language code in a Mendix app from JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2695"}],"version-history":[{"count":1,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2695\/revisions"}],"predecessor-version":[{"id":2697,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2695\/revisions\/2697"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2699"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}