Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
persona
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hexang 息壤平台
persona
Commits
af078ff6
Commit
af078ff6
authored
13 years ago
by
Austin King
Browse files
Options
Downloads
Patches
Plain Diff
Enhacing best language match to also match a language regardless of locale. Fixes Issue#1128
parent
482159cc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/i18n.js
+4
-1
4 additions, 1 deletion
lib/i18n.js
tests/i18n-tests.js
+42
-0
42 additions, 0 deletions
tests/i18n-tests.js
with
46 additions
and
1 deletion
lib/i18n.js
+
4
−
1
View file @
af078ff6
...
...
@@ -177,12 +177,15 @@ exports.parseAcceptLanguage = parseAcceptLanguage = function (header) {
// supported languages, returns the best match or a default language.
//
// languages must be a sorted list, the first match is returned.
function
bestLanguage
(
languages
,
supported_languages
,
defaultLanguage
)
{
exports
.
bestLanguage
=
bestLanguage
=
function
(
languages
,
supported_languages
,
defaultLanguage
)
{
var
lower
=
supported_languages
.
map
(
function
(
l
)
{
return
l
.
toLowerCase
();
});
for
(
var
i
=
0
;
i
<
languages
.
length
;
i
++
)
{
var
lq
=
languages
[
i
];
if
(
lower
.
indexOf
(
lq
.
lang
.
toLowerCase
())
!==
-
1
)
{
return
lq
.
lang
;
// Issue#1128 match locale, even if region isn't supported
}
else
if
(
lower
.
indexOf
(
lq
.
lang
.
slice
(
0
,
2
).
toLowerCase
())
!==
-
1
)
{
return
lq
.
lang
.
slice
(
0
,
2
);
}
}
return
defaultLanguage
;
...
...
This diff is collapsed.
Click to expand it.
tests/i18n-tests.js
+
42
−
0
View file @
af078ff6
...
...
@@ -54,6 +54,48 @@ suite.addBatch({
}
});
suite
.
addBatch
({
"
We find exact language match
"
:
{
topic
:
function
()
{
var
accept
=
'
pa,sv;q=0.8,fi;q=0.7,it-ch;q=0.5,en-us;q=0.3,en;q=0.2
'
;
var
supported
=
[
'
af
'
,
'
en-US
'
,
'
pa
'
];
var
def
=
'
en-US
'
;
return
i18n
.
bestLanguage
(
parseAcceptLanguage
(
accept
),
supported
,
def
);
},
"
For Punjabi
"
:
function
(
err
,
locale
)
{
assert
.
equal
(
locale
,
"
pa
"
);
}
},
"
Issue#1128 We find best locale even if region doesn't match
"
:
{
topic
:
function
()
{
var
accept
=
'
pa-it,sv;q=0.8,fi;q=0.7,it-ch;q=0.5,en-us;q=0.3,en;q=0.2
'
;
var
supported
=
[
'
af
'
,
'
en-US
'
,
'
pa
'
];
var
def
=
'
en-US
'
;
return
i18n
.
bestLanguage
(
parseAcceptLanguage
(
accept
),
supported
,
def
);
},
"
For Punjabi (India) serve Punjabi
"
:
function
(
err
,
locale
)
{
assert
.
equal
(
locale
,
"
pa
"
);
}
},
"
We don't extend into a region, unless we have an exact match
"
:
{
topic
:
function
()
{
var
accept
=
'
pa,sv;q=0.8,fi;q=0.7,it-ch;q=0.5,en-us;q=0.3,en;q=0.2
'
;
var
supported
=
[
'
af
'
,
'
en-US
'
,
'
pa-IT
'
];
var
def
=
'
en-US
'
;
return
i18n
.
bestLanguage
(
parseAcceptLanguage
(
accept
),
supported
,
def
);
},
"
Don't choose Punjabi (India)
"
:
function
(
err
,
locale
)
{
assert
.
equal
(
locale
,
"
en-us
"
);
}
}
});
// run or export the suite.
if
(
process
.
argv
[
1
]
===
__filename
)
suite
.
run
();
else
suite
.
export
(
module
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment