Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F116345
l10n.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
l10n.js
View Options
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2020-2021 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
.
import
'global-this.js'
as
MakeglobalThisAvailableInGlobal
.
import
'bundled-deps.js'
as
Deps
.
import
'fluent-bundle.js'
as
MakeFluentBundleAvailableInGlobal
.
import
'fluent-sequence.js'
as
MakeFluentSequenceAvailableInGlobal
.
import
'fluent-langneg.js'
as
MakeFluentLangNegAvailableInGlobal
this
.
Intl
=
this
.
IntlPolyfill
;
class
FluentProvider
{
constructor
(
bundles
)
{
this
.
bundles
=
bundles
;
}
get
(
id
,
args
)
{
// contexts is a negotiated iterable of FluentBundle instances.
let
ctx
=
FluentSequence
.
mapBundleSync
(
this
.
bundles
,
id
);
if
(
ctx
===
null
)
{
return
id
;
}
let
msg
=
ctx
.
getMessage
(
id
);
if
(
msg
.
value
)
{
return
ctx
.
formatPattern
(
msg
.
value
,
args
);
}
else
{
return
`<No message for
${
id
}
>`
;
// should not happen
}
}
};
function
negotiateLanguages
(
desiredLanguages
,
availableLocales
,
defaultLocale
)
{
if
(
!
desiredLanguages
)
{
desiredLanguages
=
[
Qt
.
uiLanguage
];
}
return
FluentLangNeg
.
negotiateLanguages
(
desiredLanguages
,
availableLocales
,
{
defaultLocale
}
);
}
// console.log("Available locales: ", LOCALES_ALL);
function
generateBundles
(
locales
,
messageHash
)
{
return
locales
.
map
(
locale
=>
{
const
bundle
=
new
FluentBundle
.
FluentBundle
(
locale
);
bundle
.
addResource
(
new
FluentBundle
.
FluentResource
(
messageHash
[
locale
]));
return
bundle
;
});
}
/**
const availableLocales = ftlProvider.availableLocaleCodes();
const desiredLanguages = ...; // from user input
const defaultLocale = ...; // a fallback locale for which we have all translations
const wantedLocales = negotiateLanguages(desiredLanguages, availableLocales, defaultLocale);
const bundles = generateBundles(ftlProvider.get(wantedLocales));
const provider = new FluentProvider(bundles);
const testVar = provider.get('hello-world', {name: "Anna"});
*/
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 1, 8:12 AM (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41637
Default Alt Text
l10n.js (1 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment