[gnome-maps/wip/mlundblad/osm-edit-namevariants: 11/12] osmUtils: Add a function to get the bare language code



commit a0f7e4747b9f73a2dc47dd795016ed53789e4098
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Mar 27 22:22:50 2017 +0200

    osmUtils: Add a function to get the bare language code

 src/osmUtils.js |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
---
diff --git a/src/osmUtils.js b/src/osmUtils.js
index 91c6b69..1bbb099 100644
--- a/src/osmUtils.js
+++ b/src/osmUtils.js
@@ -20,10 +20,13 @@
  * Author: Marcus Lundblad <ml update uu se>
  */
 
+const GLib = imports.gi.GLib;
 const Soup = imports.gi.Soup;
 
 const Application = imports.application;
 
+let languageCode = null;
+
 /*
  * Gets a Wikipedia article in OSM tag format (i.e. lang:Article title)
  * given a URL or null if input doesn't match a Wikipedia URL
@@ -65,3 +68,24 @@ function updatePlaceFromOSMObject(place, object) {
 
     Application.placeStore.updatePlace(place);
 }
+
+/**
+ * Get the bare ISO 639 language code of the preferred locale.
+ */
+function getLanguageCode() {
+    if (languageCode)
+        return languageCode;
+
+    let locale = GLib.get_language_names()[0];
+
+    // strip charset
+    if (locale.indexOf('.') !== -1)
+        locale = locale.substring(0, locale.indexOf('.'));
+
+    // strip country
+    if (locale.indexOf('_') !== -1)
+        locale = locale.substring(0, locale.indexOf('_'));
+
+    languageCode = locale;
+    return languageCode;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]