Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Calamares
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
osak
Calamares
Commits
cfb8ef9f
Commit
cfb8ef9f
authored
2 years ago
by
Adriaan de Groot
Browse files
Options
Downloads
Patches
Plain Diff
[locale] Use locale-similarity for searching
parent
a988298a
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/modules/locale/LocaleConfiguration.cpp
+37
-98
37 additions, 98 deletions
src/modules/locale/LocaleConfiguration.cpp
src/modules/locale/LocaleNames.h
+3
-0
3 additions, 0 deletions
src/modules/locale/LocaleNames.h
with
40 additions
and
98 deletions
src/modules/locale/LocaleConfiguration.cpp
+
37
−
98
View file @
cfb8ef9f
...
@@ -9,11 +9,13 @@
...
@@ -9,11 +9,13 @@
*/
*/
#include
"LocaleConfiguration.h"
#include
"LocaleConfiguration.h"
#include
"LocaleNames.h"
#include
"utils/Logger.h"
#include
"utils/Logger.h"
#include
<QLocale>
#include
<QLocale>
#include
<QRegularExpression>
#include
<QRegularExpression>
#include
<QVector>
LocaleConfiguration
::
LocaleConfiguration
()
LocaleConfiguration
::
LocaleConfiguration
()
:
explicit_lang
(
false
)
:
explicit_lang
(
false
)
...
@@ -47,90 +49,45 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
...
@@ -47,90 +49,45 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
const
QString
&
countryCode
)
const
QString
&
countryCode
)
{
{
cDebug
()
<<
"Mapping"
<<
languageLocale
<<
"in"
<<
countryCode
<<
"to locale."
;
cDebug
()
<<
"Mapping"
<<
languageLocale
<<
"in"
<<
countryCode
<<
"to locale."
;
QString
language
=
languageLocale
.
split
(
'_'
).
first
();
QString
region
;
if
(
language
.
contains
(
'@'
)
)
{
auto
r
=
language
.
split
(
'@'
);
language
=
r
.
first
();
region
=
r
[
1
];
// second()
}
// Either an exact match, or the whole language part matches
// (followed by .<encoding> or _<country>
QStringList
linesForLanguage
=
availableLocales
.
filter
(
QRegularExpression
(
language
+
"[._]"
)
);
cDebug
()
<<
Logger
::
SubEntry
<<
"Matching"
<<
linesForLanguage
;
const
QString
default_lang
=
QStringLiteral
(
"en_US.UTF-8"
);
QString
lang
;
QString
lang
;
if
(
linesForLanguage
.
isEmpty
()
||
languageLocale
.
isEmpty
()
)
{
lang
=
"en_US.UTF-8"
;
}
else
if
(
linesForLanguage
.
length
()
==
1
)
{
lang
=
linesForLanguage
.
first
();
}
// lang could still be empty if we found multiple locales that satisfy myLanguage
const
LocaleNameParts
self
=
LocaleNameParts
::
fromName
(
languageLocale
);
const
QString
combinedLanguageAndCountry
=
QString
(
"%1_%2"
).
arg
(
language
).
arg
(
countryCode
);
if
(
self
.
isValid
()
&&
!
availableLocales
.
isEmpty
()
)
if
(
lang
.
isEmpty
()
&&
region
.
isEmpty
()
)
{
{
QVector
<
LocaleNameParts
>
others
;
auto
l
=
linesForLanguage
.
filter
(
others
.
resize
(
availableLocales
.
length
()
);
// Makes default structs
QRegularExpression
(
combinedLanguageAndCountry
+
"[._]"
)
);
// no regional variants
std
::
transform
(
availableLocales
.
begin
(),
availableLocales
.
end
(),
others
.
begin
(),
LocaleNameParts
::
fromName
);
if
(
l
.
length
()
==
1
)
std
::
sort
(
others
.
begin
(),
{
others
.
end
(),
lang
=
l
.
first
();
[
reference
=
self
](
const
LocaleNameParts
&
lhs
,
const
LocaleNameParts
&
rhs
)
}
{
return
reference
.
similarity
(
lhs
)
<
reference
.
similarity
(
rhs
);
}
);
}
// The following block was inspired by Ubiquity, scripts/localechooser-apply.
// The best match is at the end
// No copyright statement found in file, assuming GPL v2 or later.
LocaleNameParts
best_match
=
others
.
last
();
/* # In the special cases of Portuguese and Chinese, selecting a
if
(
!
(
self
.
similarity
(
best_match
)
>
LocaleNameParts
::
no_match
)
)
# different location may imply a different dialect of the language.
# In such cases, make LANG reflect the selected language (for
# messages, character types, and collation) and make the other
# locale categories reflect the selected location. */
if
(
language
==
"pt"
||
language
==
"zh"
)
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Special-case Portuguese and Chinese"
;
QString
proposedLocale
=
QString
(
"%1_%2"
).
arg
(
language
).
arg
(
countryCode
);
for
(
const
QString
&
line
:
linesForLanguage
)
{
{
if
(
line
.
contains
(
proposedLocale
)
)
best_match
=
LocaleNameParts
{};
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Country-variant"
<<
line
<<
"chosen."
;
lang
=
line
;
break
;
}
}
}
}
// .. but it might match **better** with the chosen location country Code
if
(
lang
.
isEmpty
()
&&
!
region
.
isEmpty
()
)
if
(
self
.
similarity
(
best_match
)
<
LocaleNameParts
::
complete_match
)
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Special-case region @"
<<
region
;
QString
proposedRegion
=
QString
(
"@%1"
).
arg
(
region
);
for
(
const
QString
&
line
:
linesForLanguage
)
{
{
if
(
line
.
startsWith
(
language
)
&&
line
.
contains
(
proposedRegion
)
)
auto
self_other_country
(
self
);
self_other_country
.
country
=
countryCode
;
std
::
sort
(
others
.
begin
(),
others
.
end
(),
[
reference
=
self_other_country
](
const
LocaleNameParts
&
lhs
,
const
LocaleNameParts
&
rhs
)
{
return
reference
.
similarity
(
lhs
)
<
reference
.
similarity
(
rhs
);
}
);
if
(
self_other_country
.
similarity
(
others
.
last
()
)
>
self
.
similarity
(
best_match
)
)
{
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Region-variant"
<<
line
<<
"chosen."
;
best_match
=
others
.
last
();
lang
=
line
;
break
;
}
}
}
}
}
if
(
best_match
.
isValid
()
)
// If we found no good way to set a default lang, do a search with the whole
// language locale and pick the first result, if any.
if
(
lang
.
isEmpty
()
)
{
for
(
const
QString
&
line
:
availableLocales
)
{
if
(
line
.
startsWith
(
languageLocale
)
)
{
{
lang
=
line
;
break
;
lang
=
best_match
.
name
();
}
}
}
}
}
...
@@ -138,7 +95,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
...
@@ -138,7 +95,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
// en_US.UTF-8 UTF-8. This completes all default language setting guesswork.
// en_US.UTF-8 UTF-8. This completes all default language setting guesswork.
if
(
lang
.
isEmpty
()
)
if
(
lang
.
isEmpty
()
)
{
{
lang
=
"en_US.UTF-8"
;
lang
=
default_lang
;
}
}
...
@@ -188,34 +145,16 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
...
@@ -188,34 +145,16 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
// We make a proposed locale based on the UI language and the timezone's country. There is no
// We make a proposed locale based on the UI language and the timezone's country. There is no
// guarantee that this will be a valid, supported locale (often it won't).
// guarantee that this will be a valid, supported locale (often it won't).
QString
lc_formats
;
QString
lc_formats
;
const
QString
combined
=
QString
(
"%1_%2"
).
arg
(
language
).
arg
(
countryCode
);
const
QString
combined
=
QString
(
"%1_%2"
).
arg
(
self
.
language
).
arg
(
countryCode
);
if
(
lang
.
isEmpty
()
)
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Looking up formats for"
<<
combinedLanguageAndCountry
;
// We look up if it's a supported locale.
for
(
const
QString
&
line
:
availableLocales
)
{
if
(
line
.
startsWith
(
combinedLanguageAndCountry
)
)
{
lang
=
line
;
lc_formats
=
line
;
break
;
}
}
}
else
{
if
(
availableLocales
.
contains
(
lang
)
)
if
(
availableLocales
.
contains
(
lang
)
)
{
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Exact formats match for language tag"
<<
lang
;
cDebug
()
<<
Logger
::
SubEntry
<<
"Exact formats match for language tag"
<<
lang
;
lc_formats
=
lang
;
lc_formats
=
lang
;
}
}
else
if
(
availableLocales
.
contains
(
combined
LanguageAndCountry
)
)
else
if
(
availableLocales
.
contains
(
combined
)
)
{
{
cDebug
()
<<
Logger
::
SubEntry
<<
"Exact formats match for combined"
<<
combinedLanguageAndCountry
;
cDebug
()
<<
Logger
::
SubEntry
<<
"Exact formats match for combined"
<<
combined
;
lang
=
combinedLanguageAndCountry
;
lc_formats
=
combined
;
lc_formats
=
combinedLanguageAndCountry
;
}
}
}
if
(
lc_formats
.
isEmpty
()
)
if
(
lc_formats
.
isEmpty
()
)
...
...
This diff is collapsed.
Click to expand it.
src/modules/locale/LocaleNames.h
+
3
−
0
View file @
cfb8ef9f
...
@@ -31,6 +31,9 @@ struct LocaleNameParts
...
@@ -31,6 +31,9 @@ struct LocaleNameParts
static
LocaleNameParts
fromName
(
const
QString
&
name
);
static
LocaleNameParts
fromName
(
const
QString
&
name
);
static
inline
constexpr
const
int
no_match
=
0
;
static
inline
constexpr
const
int
complete_match
=
100
;
/** @brief Compute similarity-score with another locale-name.
/** @brief Compute similarity-score with another locale-name.
*
*
* Similarity is driven by language and region, then country.
* Similarity is driven by language and region, then country.
...
...
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