Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
mahara
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
mahara
mahara
Commits
44d34181
Commit
44d34181
authored
Jun 05, 2015
by
Son Nguyen
Committed by
Gerrit Code Review
Jun 05, 2015
Browse files
Options
Browse Files
Download
Plain Diff
Merge "Allowing lang strings in js to be loaded via ajax (Bug #1450995)"
parents
b913791c
05a80e6a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
0 deletions
+98
-0
htdocs/js/mahara.js
htdocs/js/mahara.js
+67
-0
htdocs/lang/get_string.php
htdocs/lang/get_string.php
+31
-0
No files found.
htdocs/js/mahara.js
View file @
44d34181
...
...
@@ -15,6 +15,7 @@ function get_string(s) {
if
(
typeof
(
strings
)
==
'
undefined
'
||
typeof
(
strings
[
s
])
==
'
undefined
'
)
{
return
'
[[[
'
+
s
+
((
args
.
length
>
0
)
?
(
'
(
'
+
args
.
join
(
'
,
'
)
+
'
)
'
)
:
''
)
+
'
]]]
'
;
}
var
str
=
strings
[
s
];
if
(
typeof
(
str
)
==
'
object
'
)
{
var
index
=
0
;
...
...
@@ -33,6 +34,72 @@ function get_string(s) {
return
str
.
replace
(
/%
((
%
)
|s
)
/g
,
function
(
m
)
{
return
m
[
2
]
||
args
[
i
++
];
});
}
/**
* Getting the string via ajax as deferred object
*/
function
get_string_ajax
(
str
,
section
)
{
// If string already exists in strings object
if
(
typeof
(
strings
[
str
])
!==
'
undefined
'
)
{
return
get_string
.
apply
(
arguments
);
}
var
rnd
=
randString
(
10
);
var
placeholder
=
'
<span id="str_
'
+
rnd
+
'
">
'
+
get_string
(
str
,
section
)
+
'
</span>
'
;
get_string_ajax_call
.
apply
(
this
,
arguments
).
done
(
function
(
r
)
{
// need to find the random string in the text and replace it with our lang string
jQuery
(
'
#str_
'
+
rnd
).
replaceWith
(
r
.
message
.
data
.
string
);
});
return
placeholder
;
}
/**
* Allow for the fetching of a string after the page has loaded.
* Adds the string to the stings array so we don't have to keep
* re-fetching it.
*
* Useful for page builder blocks that fetch things via ajax
* This runs asynchronously so broken string may display for a split before
* being fetched here.
* This hooks into get_string() so it can return 'missing string' string like normal
*
* @param str string The string to fetch
* @param section string The lang file to find the string
*
* @return string The output from get_string()
*/
function
get_string_ajax_call
(
str
,
section
)
{
// Try fetching the string and adding it to the strings object
return
jQuery
.
ajax
({
url
:
config
.
wwwroot
+
'
lang/get_string.php
'
,
data
:
{
'
sesskey
'
:
config
[
'
sesskey
'
],
'
string
'
:
str
,
'
section
'
:
section
,
'
args
'
:
[].
slice
.
call
(
arguments
,
2
)},
type
:
'
POST
'
,
success
:
function
(
data
)
{
// on success
if
(
data
.
message
.
data
.
string
)
{
strings
[
str
]
=
data
.
message
.
data
.
string
;
}
return
get_string
.
apply
(
this
,
arguments
);
},
error
:
function
()
{
// on error
return
get_string
.
apply
(
this
,
arguments
);
}
});
}
/**
* Return a random alphanumeric string
* @param x int Length of returned string
*/
function
randString
(
x
)
{
var
s
=
""
;
while
(
s
.
length
<
x
&&
x
>
0
)
{
var
r
=
Math
.
random
();
s
+=
(
r
<
0.1
?
Math
.
floor
(
r
*
100
)
:
String
.
fromCharCode
(
Math
.
floor
(
r
*
26
)
+
(
r
>
0.5
?
97
:
65
)));
}
return
s
;
}
// Expects an image/css path to fetch url for (requires config.theme[] to be
// set)
function
get_themeurl
(
s
)
{
...
...
htdocs/lang/get_string.php
0 → 100644
View file @
44d34181
<?php
/**
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define
(
'INTERNAL'
,
1
);
define
(
'JSON'
,
1
);
require
(
dirname
(
dirname
(
__FILE__
))
.
'/init.php'
);
$rawstring
=
param_alphanumext
(
'string'
);
$section
=
param_alphanumext
(
'section'
);
$args
=
param_variable
(
'args'
,
null
);
if
(
!
empty
(
$args
)
&&
is_array
(
$args
))
{
array_unshift
(
$args
,
$rawstring
,
$section
);
$string
=
call_user_func_array
(
'get_string'
,
$args
);
}
else
{
$string
=
get_string
(
$rawstring
,
$section
);
}
json_reply
(
false
,
array
(
'message'
=>
null
,
'data'
=>
array
(
'string'
=>
$string
,
)
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment