Browse Source

Add user language in settings, use that field as language for UI

tags/3.4.3
fabrixxm 3 years ago
parent
commit
fb3ac68cc0

+ 43
- 20
include/pgettext.php View File

@@ -11,7 +11,7 @@ require_once("include/dba.php");
11 11
  *
12 12
  * Get the language setting directly from system variables, bypassing get_config()
13 13
  * as database may not yet be configured.
14
- * 
14
+ *
15 15
  * If possible, we use the value from the browser.
16 16
  *
17 17
  */
@@ -21,22 +21,22 @@ if(! function_exists('get_browser_language')) {
21 21
 function get_browser_language() {
22 22
 
23 23
 	if (x($_SERVER,'HTTP_ACCEPT_LANGUAGE')) {
24
-	    // break up string into pieces (languages and q factors)
25
-    	preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', 
24
+		// break up string into pieces (languages and q factors)
25
+		preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
26 26
 			$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
27 27
 
28
-    	if (count($lang_parse[1])) {
29
-        	// create a list like "en" => 0.8
30
-        	$langs = array_combine($lang_parse[1], $lang_parse[4]);
31
-    	
32
-        	// set default to 1 for any without q factor
33
-        	foreach ($langs as $lang => $val) {
34
-            	if ($val === '') $langs[$lang] = 1;
35
-        	}
36
-
37
-        	// sort list based on value	
38
-        	arsort($langs, SORT_NUMERIC);
39
-    	}
28
+		if (count($lang_parse[1])) {
29
+			// create a list like "en" => 0.8
30
+			$langs = array_combine($lang_parse[1], $lang_parse[4]);
31
+
32
+			// set default to 1 for any without q factor
33
+			foreach ($langs as $lang => $val) {
34
+				if ($val === '') $langs[$lang] = 1;
35
+			}
36
+
37
+			// sort list based on value
38
+			arsort($langs, SORT_NUMERIC);
39
+		}
40 40
 	}
41 41
 
42 42
 	if(isset($langs) && count($langs)) {
@@ -94,7 +94,7 @@ if(! function_exists('load_translation_table')) {
94 94
  * load string translation table for alternate language
95 95
  *
96 96
  * first plugin strings are loaded, then globals
97
- * 
97
+ *
98 98
  * @param string $lang language code to load
99 99
  */
100 100
 function load_translation_table($lang) {
@@ -111,7 +111,7 @@ function load_translation_table($lang) {
111 111
 			}
112 112
 		}
113 113
 	}
114
-	
114
+
115 115
 	if(file_exists("view/$lang/strings.php")) {
116 116
 		include("view/$lang/strings.php");
117 117
 	}
@@ -145,7 +145,7 @@ function tt($singular, $plural, $count){
145 145
 		$k = $f($count);
146 146
 		return is_array($t)?$t[$k]:$t;
147 147
 	}
148
-	
148
+
149 149
 	if ($count!=1){
150 150
 		return $plural;
151 151
 	} else {
@@ -153,11 +153,34 @@ function tt($singular, $plural, $count){
153 153
 	}
154 154
 }}
155 155
 
156
-// provide a fallback which will not collide with 
157
-// a function defined in any language file 
156
+// provide a fallback which will not collide with
157
+// a function defined in any language file
158 158
 
159 159
 if(! function_exists('string_plural_select_default')) {
160 160
 function string_plural_select_default($n) {
161 161
 	return ($n != 1);
162 162
 }}
163 163
 
164
+
165
+/**
166
+ * Return installed languages as associative array
167
+ * [
168
+ * 		lang => lang,
169
+ * 		...
170
+ * ]
171
+ */
172
+function get_avaiable_languages() {
173
+	$lang_choices = array();
174
+	$langs = glob('view/*/strings.php'); /**/
175
+
176
+	if(is_array($langs) && count($langs)) {
177
+		if(! in_array('view/en/strings.php',$langs))
178
+			$langs[] = 'view/en/';
179
+		asort($langs);
180
+		foreach($langs as $l) {
181
+			$t = explode("/",$l);
182
+			$lang_choices[$t[1]] = $t[1];
183
+		}
184
+	}
185
+	return $lang_choices;
186
+}

+ 6
- 6
index.php View File

@@ -102,13 +102,13 @@ session_start();
102 102
  * Language was set earlier, but we can over-ride it in the session.
103 103
  * We have to do it here because the session was just now opened.
104 104
  */
105
-
106
-if(array_key_exists('system_language',$_POST)) {
107
-	if(strlen($_POST['system_language']))
108
-		$_SESSION['language'] = $_POST['system_language'];
109
-	else
110
-		unset($_SESSION['language']);
105
+if (x($_SESSION,'authenticated') && !x($_SESSION,'language')) {
106
+	// we didn't loaded user data yet, but we need user language
107
+	$r = q("SELECT language FROM user WHERE uid=%d", intval($_SESSION['uid']));
108
+	$_SESSION['language'] = $lang;
109
+	if (count($r)>0) $_SESSION['language'] = $r[0]['language'];
111 110
 }
111
+
112 112
 if((x($_SESSION,'language')) && ($_SESSION['language'] !== $lang)) {
113 113
 	$lang = $_SESSION['language'];
114 114
 	load_translation_table($lang);

+ 1
- 12
mod/admin.php View File

@@ -603,18 +603,7 @@ function admin_page_site_post(&$a){
603 603
 function admin_page_site(&$a) {
604 604
 
605 605
 	/* Installed langs */
606
-	$lang_choices = array();
607
-	$langs = glob('view/*/strings.php'); /**/
608
-
609
-	if(is_array($langs) && count($langs)) {
610
-		if(! in_array('view/en/strings.php',$langs))
611
-			$langs[] = 'view/en/';
612
-		asort($langs);
613
-		foreach($langs as $l) {
614
-			$t = explode("/",$l);
615
-			$lang_choices[$t[1]] = $t[1];
616
-		}
617
-	}
606
+	$lang_choices = get_avaiable_languages();
618 607
 
619 608
 	if (strlen(get_config('system','directory_submit_url')) AND
620 609
 		!strlen(get_config('system','directory'))) {

+ 20
- 2
mod/settings.php View File

@@ -386,6 +386,8 @@ function settings_post(&$a) {
386 386
 	$username         = ((x($_POST,'username'))   ? notags(trim($_POST['username']))     : '');
387 387
 	$email            = ((x($_POST,'email'))      ? notags(trim($_POST['email']))        : '');
388 388
 	$timezone         = ((x($_POST,'timezone'))   ? notags(trim($_POST['timezone']))     : '');
389
+	$language         = ((x($_POST,'language'))   ? notags(trim($_POST['language']))     : '');
390
+
389 391
 	$defloc           = ((x($_POST,'defloc'))     ? notags(trim($_POST['defloc']))       : '');
390 392
 	$openid           = ((x($_POST,'openid_url')) ? notags(trim($_POST['openid_url']))   : '');
391 393
 	$maxreq           = ((x($_POST,'maxreq'))     ? intval($_POST['maxreq'])             : 0);
@@ -532,7 +534,15 @@ function settings_post(&$a) {
532 534
 		}
533 535
 	}
534 536
 
535
-	$r = q("UPDATE `user` SET `username` = '%s', `email` = '%s', `openid` = '%s', `timezone` = '%s',  `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `notify-flags` = %d, `page-flags` = %d, `default-location` = '%s', `allow_location` = %d, `maxreq` = %d, `expire` = %d, `openidserver` = '%s', `def_gid` = %d, `blockwall` = %d, `hidewall` = %d, `blocktags` = %d, `unkmail` = %d, `cntunkmail` = %d  WHERE `uid` = %d",
537
+
538
+	$r = q("UPDATE `user` SET `username` = '%s', `email` = '%s',
539
+				`openid` = '%s', `timezone` = '%s',
540
+				`allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s',
541
+				`notify-flags` = %d, `page-flags` = %d, `default-location` = '%s',
542
+				`allow_location` = %d, `maxreq` = %d, `expire` = %d, `openidserver` = '%s',
543
+				`def_gid` = %d, `blockwall` = %d, `hidewall` = %d, `blocktags` = %d,
544
+				`unkmail` = %d, `cntunkmail` = %d, `language` = '%s'
545
+			WHERE `uid` = %d",
536 546
 			dbesc($username),
537 547
 			dbesc($email),
538 548
 			dbesc($openid),
@@ -554,11 +564,15 @@ function settings_post(&$a) {
554 564
 			intval($blocktags),
555 565
 			intval($unkmail),
556 566
 			intval($cntunkmail),
567
+			dbesc($language),
557 568
 			intval(local_user())
558 569
 	);
559 570
 	if($r)
560 571
 		info( t('Settings updated.') . EOL);
561 572
 
573
+	// clear session language
574
+	unset($_SESSION['language']);
575
+
562 576
 	$r = q("UPDATE `profile`
563 577
 		SET `publish` = %d,
564 578
 		`name` = '%s',
@@ -985,6 +999,7 @@ function settings_content(&$a) {
985 999
 	$email      = $a->user['email'];
986 1000
 	$nickname   = $a->user['nickname'];
987 1001
 	$timezone   = $a->user['timezone'];
1002
+	$language   = $a->user['language'];
988 1003
 	$notify     = $a->user['notify-flags'];
989 1004
 	$defloc     = $a->user['default-location'];
990 1005
 	$openid     = $a->user['openid'];
@@ -1168,6 +1183,8 @@ function settings_content(&$a) {
1168 1183
 	else
1169 1184
 		$public_post_link = '&public=1';
1170 1185
 
1186
+	/* Installed langs */
1187
+	$lang_choices = get_avaiable_languages();
1171 1188
 
1172 1189
 	$o .= replace_macros($stpl, array(
1173 1190
 		'$ptitle' 	=> t('Account Settings'),
@@ -1190,6 +1207,7 @@ function settings_content(&$a) {
1190 1207
 		'$username' => array('username',  t('Full Name:'), $username,''),
1191 1208
 		'$email' 	=> array('email', t('Email Address:'), $email, '', '', '', 'email'),
1192 1209
 		'$timezone' => array('timezone_select' , t('Your Timezone:'), select_timezone($timezone), ''),
1210
+		'$language' => array('language', t('Your Language:'), $language, t('Set the language we use to show you friendica interface and to send you emails'), $lang_choices),
1193 1211
 		'$defloc'	=> array('defloc', t('Default Post Location:'), $defloc, ''),
1194 1212
 		'$allowloc' => array('allow_location', t('Use Browser Location:'), ($a->user['allow_location'] == 1), ''),
1195 1213
 
@@ -1245,7 +1263,7 @@ function settings_content(&$a) {
1245 1263
 		'$notify8'  => array('notify8', t('You are poked/prodded/etc. in a post'), ($notify & NOTIFY_POKE), NOTIFY_POKE, ''),
1246 1264
 
1247 1265
         '$desktop_notifications' => array('desktop_notifications', t('Activate desktop notifications') , false, t('Show desktop popup on new notifications')),
1248
-                
1266
+
1249 1267
 		'$email_textonly' => array('email_textonly', t('Text-only notification emails'),
1250 1268
 									get_pconfig(local_user(),'system','email_textonly'),
1251 1269
 									t('Send text only notification emails, without the html part')),

+ 9
- 8
view/templates/settings.tpl View File

@@ -27,6 +27,7 @@
27 27
 {{include file="field_input.tpl" field=$email}}
28 28
 {{include file="field_password.tpl" field=$password4}}
29 29
 {{include file="field_custom.tpl" field=$timezone}}
30
+{{include file="field_select.tpl" field=$language}}
30 31
 {{include file="field_input.tpl" field=$defloc}}
31 32
 {{include file="field_checkbox.tpl" field=$allowloc}}
32 33
 
@@ -147,7 +148,7 @@
147 148
 (function(){
148 149
     var elm = $("#id_{{$desktop_notifications.0}}_onoff");
149 150
     var ckbox = $("#id_{{$desktop_notifications.0}}");
150
-    
151
+
151 152
     if (getNotificationPermission() === 'granted') {
152 153
         ckbox.val(1);
153 154
         elm.find(".off").addClass("hidden");
@@ -156,18 +157,18 @@
156 157
 	if (getNotificationPermission() === null) {
157 158
 		elm.parent(".field.yesno").hide();
158 159
 	}
159
-    
160
+
160 161
     $("#id_{{$desktop_notifications.0}}_onoff").on("click", function(e){
161
-        
162
+
162 163
         if (Notification.permission === 'granted') {
163 164
             localStorage.setItem('notification-permissions', ckbox.val()==1 ? 'granted' : 'denied');
164 165
         } else if (Notification.permission === 'denied') {
165 166
             localStorage.setItem('notification-permissions', 'denied');
166
-            
167
+
167 168
             ckbox.val(0);
168 169
             elm.find(".on").addClass("hidden");
169 170
             elm.find(".off").removeClass("hidden");
170
-            
171
+
171 172
         } else if (Notification.permission === 'default') {
172 173
             Notification.requestPermission(function(choice) {
173 174
                 if (choice === 'granted') {
@@ -181,10 +182,10 @@
181 182
                 }
182 183
             });
183 184
         }
184
-		
185
+
185 186
 		//console.log(getNotificationPermission());
186
-		
187
-        
187
+
188
+
188 189
     })
189 190
 })();
190 191
 </script>

+ 1
- 0
view/theme/decaf-mobile/templates/settings.tpl View File

@@ -27,6 +27,7 @@
27 27
 {{include file="field_input.tpl" field=$email}}
28 28
 {{include file="field_password.tpl" field=$password4}}
29 29
 {{include file="field_custom.tpl" field=$timezone}}
30
+{{include file="field_select.tpl" field=$language}}
30 31
 {{include file="field_input.tpl" field=$defloc}}
31 32
 {{include file="field_checkbox.tpl" field=$allowloc}}
32 33
 

+ 1
- 0
view/theme/frost-mobile/templates/settings.tpl View File

@@ -27,6 +27,7 @@
27 27
 {{include file="field_input.tpl" field=$email}}
28 28
 {{include file="field_password.tpl" field=$password4}}
29 29
 {{include file="field_custom.tpl" field=$timezone}}
30
+{{include file="field_select.tpl" field=$language}}
30 31
 {{include file="field_input.tpl" field=$defloc}}
31 32
 {{include file="field_checkbox.tpl" field=$allowloc}}
32 33
 

Loading…
Cancel
Save