## Server Bug Fix: How do you set a locale non-interactively on Debian/Ubuntu?

Usually, I run `aptitude -y install locales` then `dpkg-reconfigure locales` to set up locale.

Now I want to put it into a shell script, how can I reliably do the following, automatically / non-interactively?

• Choose `en_US.UTF-8` and set it as system default
• Disable all other locales
• (Optional) Verify if `/etc/default/locale` contains one-and-only entry of `LANG=en_US.UTF-8` as expected

See locale-gen:

`````` locale-gen --purge en_US.UTF-8
``````

and

`````` echo -e 'LANG="en_US.UTF-8"nLANGUAGE="en_US:en"n' > /etc/default/locale
``````

Could not get @stone’s answer to work. Instead, I use this method (for Dockerfiles):

``````# Configure timezone and locale
echo "Europe/Oslo" > /etc/timezone &&
dpkg-reconfigure -f noninteractive tzdata &&
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen &&
sed -i -e 's/# nb_NO.UTF-8 UTF-8/nb_NO.UTF-8 UTF-8/' /etc/locale.gen &&
echo 'LANG="nb_NO.UTF-8"'>/etc/default/locale &&
dpkg-reconfigure --frontend=noninteractive locales &&
update-locale LANG=nb_NO.UTF-8
``````

Based on the fine work in @EirikW’s answer. Specific to a `Dockerfile`:

``````ENV LANG=en_GB.UTF-8
RUN apt-get install -y locales &&
sed -i -e "s/# \$LANG.*/\$LANG.UTF-8 UTF-8/" /etc/locale.gen &&
dpkg-reconfigure --frontend=noninteractive locales &&
update-locale LANG=\$LANG
``````

For me it was necessary to set the additional 3 ENV-Vars:

``````# - Set the locale + timezone --------------------------------------------------
RUN echo "Europe/Vienna" > /etc/timezone &&
dpkg-reconfigure -f noninteractive tzdata &&
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen &&
sed -i -e 's/# de_AT.UTF-8 UTF-8/de_AT.UTF-8 UTF-8/' /etc/locale.gen &&
echo 'LANG="de_AT.UTF-8"'>/etc/default/locale &&
dpkg-reconfigure --frontend=noninteractive locales &&
update-locale LANG=de_AT.UTF-8

ENV LANG de_AT.UTF-8
ENV LANGUAGE de_AT.UTF-8
ENV LC_ALL de_AT.UTF-8
``````

However Thanks to ErikWs for his answer: (https://serverfault.com/a/689947)

To reconfigure the timezone and locales non-interactively, from within a script, here is what works for me (under Debian):

• For configuring the timezone, I first create ‘/etc/localtime’ as a soft link to the appropriate zoneinfo file under the ‘/usr/share/zoneinfo’ directory. Then, I run the dpkg-reconfigure command, and everything will be put in place.
So, for instance, to set up timezone ‘Europe/Brussels’:

``````AREA='Europe'
ZONE='Brussels'

ZONEINFO_FILE='/usr/share/zoneinfo/'"\${AREA}"'/'"\${ZONE}"
ln --force --symbolic "\${ZONEINFO_FILE}" '/etc/localtime'
dpkg-reconfigure --frontend=noninteractive tzdata
``````

(Note that the AREA is a subdirectory under ‘/usr/share/info’, and the ZONE is a file under the AREA subdirectory).

• For configuring the locales, I first run a sed script that will create a new copy of the ‘/etc/locale.gen’ file, based on the contents of the ‘/usr/share/i18n/SUPPORTED’ file. Every line from the input file will be copied, but it will be turned into a comment unless it is an entry of a UTF-8 locale for a language that I wish to make available on my system (e.g., English, Dutch, French, and German):

``````sed --regexp-extended --expression='

1  {
i
# This file lists locales that you wish to have built. You can find a list
# of valid supported locales at /usr/share/i18n/SUPPORTED, and you can add
# user defined locales to /usr/local/share/i18n/SUPPORTED. If you change
# this file, you need to rerun locale-gen.

}

/^(en|nl|fr|de)(_[[:upper:]]+)?(.UTF-8)?(@[^[:space:]]+)?[[:space:]]+UTF-8\$/!   s/^/# /

' /usr/share/i18n/SUPPORTED >  /etc/locale.gen
``````

Next, I set the default environment locale in the debconf database, e.g., to British English:

``````debconf-set-selections <<< 'locales locales/default_environment_locale select en_GB.UTF-8'
``````

I subsequently remove the existing ‘/etc/default/locale’ file (just to make sure that its old contents will not interfere with my new settings), and run the dpkg-reconfigure command to generate all of the locales that the sed script selected, and to create a new ‘/etc/default/locale’ file with just an entry to set the ‘LANG’ variable to my selected default environment locale:

``````rm --force --verbose /etc/default/locale
dpkg-reconfigure --frontend=noninteractive locales
``````

Then, depending on my requirements, I may want to run a few update-locale commands, to override, e.g., the variables that affect the formatting of values, and set them to a different locale (such as Irish English):

``````update-locale LC_NUMERIC='en_IE.UTF-8'
update-locale LC_TIME='en_IE.UTF-8'
update-locale LC_MONETARY='en_IE.UTF-8'
update-locale LC_PAPER='en_IE.UTF-8'
update-locale LC_NAME='en_IE.UTF-8'
update-locale LC_TELEPHONE='en_IE.UTF-8'
update-locale LC_MEASUREMENT='en_IE.UTF-8'
update-locale LC_IDENTIFICATION='en_IE.UTF-8'
``````

(I could have specified all of these parameters on a single invocation of the update-locale command, but apparently, the order in which the entries get written to the ‘/etc/default/locale’ file is unpredictable if I do so. I prefer them to always be in the same order, which is why I generate them one by one.)

And finally, I may want to run the update-locale command one last time, to set up the LANGUAGE variable (i.e., the list of languages in which I want translatable text messages to get displayed):

``````update-locale LANGUAGE='en_GB:en_US:en'
``````

Throw all this together into a bash script, and you can easily reconfigure your locale settings with a simple run of the script, which will not require any further user interaction.

In my experience, however, the new settings will not become entirely activated until after you reboot your system twice (at least on Debian Jessie Xfce). After the first reboot, the login manager will take the new settings alright, but after you login, your user session will continue to use the old settings; then, after the second reboot, your user session will take the new settings as well.

Tagged : / / /

## Code Bug Fix: Shared resource localization is not working

I am working on shared resource localization in Asp .Net Core 3.1. For that I created resource directory and created SharedResources.cs file.

``````Root/Resources/SharedResources.cs
Root/Resources/SharedResources.en.resx
``````

I injected code in controller.

``````public AccountController(IStringLocalizer<SharedResources> sharedLocalizer)
{
_sharedLocalizer = sharedLocalizer;
}
{
{
return RedirectToAction("Dashboard", "Dashboard");
}
TempData["Error"] = _sharedLocalizer["Error"];
return View(model);
``````

In SharedResources.en.resx

``````Key : Error
Value : Invalid User
``````

In SharedResources.cs

``````namespace RootName
public class SharedResources
{
}
``````

It displays Error and it should display Invalid User. Where am I wrong?

For asp.net core 3.x,you need to create `SharedResources.cs` in your root folder and create `SharedResources.en.resx` in root/Resources folder like below:

Startup.cs:

``````public void ConfigureServices(IServiceCollection services)
{
{
// We will put our translations in a folder called Resources
o.ResourcesPath = "Resources";
});
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("de"),
};
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(localizationOptions);

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
``````

## Server Bug Fix: How do I set locale when building an Ubuntu Docker image with Packer?

I’m using Packer to build a Docker image based on Ubuntu 14.04, i.e., in my Packer template I have:

``````"builders": [{
"type": "docker",
"image": "ubuntu",
"commit": true
}],
``````

and I build it using:

``````\$ packer build my.json
``````

What do I need to put in the template to get a specific locale (say `en_GB`) to be set when I subsequently run the following?

``````\$ sudo docker run %IMAGE_ID% locale
``````

As it stands, I get:

``````LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
...
LC_IDENTIFICATION="POSIX"
LC_ALL=
``````

which causes a few problems for things I want to do next, like installing certain Python packages.

``````{
"type": "shell",
"inline": [
"locale-gen en_GB.UTF-8",
"update-locale LANG=en_GB.UTF-8 LANGUAGE=en_GB.UTF-8 LC_ALL=en_GB.UTF-8"
]
}
``````

but while that does set up the locale config it doesn’t affect the env used by docker run. Even if I add extra export lines like:

``````{
"type": "shell",
"inline": [
...
"export LANG=en_GB.UTF-8"
]
}
``````

they have no effect, presumably because when using `docker run`, it’s not a child process of the command `packer build` uses when running these commands initially.

As a workaround I can pass env vars to `docker run`, but don’t want to have to do that each time, e.g.:

``````sudo docker run -e LANG=en_GB.UTF-8 -e LANGUAGE=en_GB.UTF-8 -e LC_ALL=en_GB.UTF-8 %IMAGE_ID% locale
``````

I haven’t tried it, but according to the documentation, you should be able to do this using the `docker-import` post-processor: https://www.packer.io/docs/post-processors/docker-import.html

Example:

``````{
"type": "docker-import",
"repository": "local/ubuntu",
"tag": "latest",
"changes": [
"ENV LC_ALL en_GB.UTF-8"
]
}
``````

Tagged : / / /

## Code Bug Fix: Add localized and dynamic elements into Accordion

I have some localized strings lets say:

no:
spørmål 1.
svar 1.
spørsmål 2.
svar 2

en:
question 1.
question 2.

At the same time, i need this accordion to be dynamic cause its not always the same strings thats going into the accordion.

I am keeping this question really open, cause i am not sure how to approach this at all.

Right now i have hardcoded the text in just to reference how i want it to look like in the end.

Tagged : / / /

## Math Genius: Finitely generated \$mathbb{Z}\$-modules & localization

Prove that every $$mathbb{Z}$$-module finitely generated is free if, and only if, it is locally free.

Any hints?

I have checked this question but I’m not clear on how to proceed with the implication $$(Leftarrow)$$ and the whole “basis” thing. Is this one of those instances where we have to consider localizations as extensions of scalars?

There are two useful fact related to your question.
For $$R$$-module $$M$$,

• $$M$$ is finite locally free $$Leftrightarrow$$ $$M$$ is finite projective
• if $$R$$ is PID, $$M$$ is projective $$Leftrightarrow$$ $$M$$ is free

The first fact is explained in https://stacks.math.columbia.edu/tag/00NV, the second one is in here. Applying these to the case $$R=mathbb{Z}$$, we are done.

Tagged : / /

## Math Genius: Finitely generated \$mathbb{Z}\$-modules & localization

Prove that every $$mathbb{Z}$$-module finitely generated is free if, and only if, it is locally free.

Any hints?

I have checked this question but I’m not clear on how to proceed with the implication $$(Leftarrow)$$ and the whole “basis” thing. Is this one of those instances where we have to consider localizations as extensions of scalars?

There are two useful fact related to your question.
For $$R$$-module $$M$$,

• $$M$$ is finite locally free $$Leftrightarrow$$ $$M$$ is finite projective
• if $$R$$ is PID, $$M$$ is projective $$Leftrightarrow$$ $$M$$ is free

The first fact is explained in https://stacks.math.columbia.edu/tag/00NV, the second one is in here. Applying these to the case $$R=mathbb{Z}$$, we are done.

Tagged : / /

## Code Bug Fix: How can i store all translations in a Database?

I picked up Rails again. I’m building a general base app framework. And I’m now busy figuring out localization.
I setup successful rails-i18n. Works like a charm. I’ve read a lot on the internet and as far as I can see there are three translation situations:

1. Translation of static content.

2. Translation of labels of models only. (Could also be done the same way as translation of static content)

3. Translation of dynamic content.

I want to store translation in a db

With globalize I only see descriptions how to implement 3.

I don’t want to use to many gems. Is it possible to implement 1 and 2 too with Globalize or do I need to do this difference?

## Code Bug Fix: Default font for Windows Forms control with Latin script on a Chinese Windows

I’m developing a Windows Forms control as a plugin for a larger application. The control isn’t localized and always displays English text using the Windows Forms default font, i.e. `Control.DefaultFont`, which resolves to Microsoft Sans Serif on my German Windows 10 (or whatever is configured as the default system font).

Now, on a Chinese Windows 10 the default font may be something that’s optimized for Chinese script and the English text that my control is rendering looks rather weird (like in this question). However, the standard dialog that is shown by `MessageBox.Show()` looks okay despite it almost exclusively showing Latin characters. I assume that’s because it is using `SystemFonts.MessageBoxFont` which resolves to Microsoft YaHei UI on that system.

I guess I could explicitly use `SystemFonts.MessageBoxFont` or hard code another font like Microsoft Sans Serif in my control’s constructor (but then there are the Microsoft font guidelines). Is there any default font for Latin script in the .NET framework or Windows in general (`FontFamily.GenericSansSerif` maybe?) or at least something that tells me if a font can be used for Latin script?

I haven’t found a default font for a given script yet but at least a way to figure out whether a font supports a given script. Starting from Windows Vista, Uniscribe (linked from @Jimi’s answer to a similar question) and in particular the `ScriptGetFontScriptTags` function can be used to query information about OpenType fonts.

Using a little P/Invoke we could do:

``````private const int MaximumTagCount = 32;

private static int[] GetScriptTags(IntPtr context, Font font)
{
IntPtr oldFont = SelectObject(context, font.ToHfont());
if (oldFont == IntPtr.Zero)
throw new Win32Exception();

IntPtr scriptCache = IntPtr.Zero;
IntPtr tagsPointer = IntPtr.Zero;
IntPtr tagCountPointer = IntPtr.Zero;
try
{
// uniscribe expects a pointer to a SCRIPT_CACHE pointer
scriptCache = Marshal.AllocHGlobal(IntPtr.Size);
Marshal.WriteIntPtr(scriptCache, IntPtr.Zero);

tagsPointer = Marshal.AllocHGlobal(4 * MaximumTagCount); // one tag is 4 bytes long
tagCountPointer = Marshal.AllocHGlobal(4);

int status = ScriptGetFontScriptTags(context, scriptCache, IntPtr.Zero, MaximumTagCount, tagsPointer, tagCountPointer);
if (status != 0)
throw new Win32Exception(status);

if (tagCount > 0 && tagCount <= MaximumTagCount)
{
int[] tags = new int[tagCount];
for (int i = 0; i < tagCount; i++)
tags[i] = Marshal.ReadInt32(tagsPointer, 4 * i);
return tags;
}
else
{
return new int[0];
}
}
finally
{
SelectObject(context, oldFont);
if (scriptCache != IntPtr.Zero)
{
ScriptFreeCache(scriptCache);
Marshal.FreeHGlobal(scriptCache);
}
if (tagsPointer != IntPtr.Zero)
Marshal.FreeHGlobal(tagsPointer);
if (tagCountPointer != IntPtr.Zero)
Marshal.FreeHGlobal(tagCountPointer);
}
}

[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr value);

[DllImport("usp10.dll")]
private static extern int ScriptGetFontScriptTags(IntPtr hdc, IntPtr scriptCache, IntPtr scriptAnalysis, int maxTags, IntPtr tags, IntPtr tagCount);

[DllImport("usp10.dll")]
private static extern int ScriptFreeCache(IntPtr scriptCache);
``````

which we could then use once we have a device context from a graphics object:

``````class Form1 : Form
{
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);

using (var g = Graphics.FromHwnd(Handle))
{
IntPtr context = g.GetHdc();
try
{
foreach (FontFamily family in FontFamily.Families)
{
using (var font = new Font(family, 12))
{
Console.WriteLine(font);
Console.Write("   Scripts:");
foreach (int script in GetScriptTags(context, font))
{
Console.Write(" {0} ({1:x})", TagToString(script), script);
}
Console.WriteLine();
}
}
}
finally
{
g.ReleaseHdc(context);
}
}
}

private static string TagToString(int script)
{
var bytes = BitConverter.GetBytes(script);
var builder = new StringBuilder(bytes.Length);
foreach (var b in bytes)
builder.Append((char)b);
return builder.ToString();
}
}
``````

Now all we have to do is check whether a font has the `latn` (or `0x6e74616c` as a little endian integer) script tag. Some examples:

``````[Font: Name=楷体, Size=12, Units=3, GdiCharSet=1, GdiVerticalFont=False]
Scripts: hani (696e6168)
[Font: Name=Microsoft Himalaya, Size=12, Units=3, GdiCharSet=1, GdiVerticalFont=False]
Scripts: tibt (74626974)
[Font: Name=Microsoft JhengHei Light, Size=12, Units=3, GdiCharSet=1, GdiVerticalFont=False]
Scripts: hani (696e6168) kana (616e616b) latn (6e74616c)
[Font: Name=Microsoft Sans Serif, Size=12, Units=3, GdiCharSet=1, GdiVerticalFont=False]
Scripts: arab (62617261) hebr (72626568) latn (6e74616c) thai (69616874) cyrl (6c727963)
[Font: Name=Segoe UI, Size=12, Units=3, GdiCharSet=1, GdiVerticalFont=False]
Scripts: arab (62617261) bng2 (32676e62) cyrl (6c727963) dev2 (32766564) gjr2 (32726a67) grek (6b657267) gur2 (32727567) hebr (72626568) khmr (726d686b) knd2 (32646e6b) lao  (206f616c) latn (6e74616c) mlm2 (326d6c6d) mong (676e6f6d) mymr (726d796d) ory2 (3279726f) talu (756c6174) tel2 (326c6574) thai (69616874) tibt (74626974) tml2 (326c6d74) beng (676e6562) deva (61766564) gujr (726a7567) guru (75727567) knda (61646e6b) mlym (6d796c6d) orya (6179726f) taml (6c6d6174) telu (756c6574)
``````

Tagged : / / / /

## Linux HowTo: How do you download an english version of Foxmail, and get POP to work?

I keep uninstalling and reinstalling e-mail client foxmail. I was at least able to download Foxmail on Windows 10, but when it opens it is in Chinese, I asked someone to read it for me and used the correct POP ports but it still does not work. The only progress I made was uninstalling the bugged version and getting the software reinstalled. But I still can’t get through the steps to get POP working, anyone has a screenshot instruction book? At least on how to translate it, because it is an app and there is no translator.
foxmail.en.lo4d.com/windows
Says English version, for window 10 but something about it changed it use to work on my computer two years ago but it got bugged somehow and I had to uninstall it to even get it reinstalled

Tagged : /

## Math Genius: Show an element is not in a ring of fractions

Let $$(mathbb{R}[x,y]/(x^2-y^3))_{(x,y)}$$. How could I show that $$frac{x}{y}$$ does not belong to the ring before?
When we ask “does $$x/y$$ belong to the ring $$(mathbb{R}[x,y]/(x^2-y^3))_{(x,y)}$$?”, what we’re really asking is “does there exist some element $$a in (mathbb{R}[x,y]/(x^2-y^3))_{(x,y)}$$ such that $$ay = x$$?”. To answer that question, suppose such an $$a$$ did exist. Then by construction of the localization, $$a = p/q$$ for some $$p,q in mathbb{R}[x,y]/(x^2-y^3)$$ such that $$q notin (x,y)$$. Then $$ay = x$$ means that $$(yp)/q = x/1$$, which means that there exists some $$t notin (x,y)$$ such that $$t(yp-xq) = 0$$.
Of course this equality is happening in the quotient ring $$mathbb{R}[x,y]/(x^2-y^3)$$, and it’s easier to think about genuine polynomials, so let’s take representatives $$hat{t}, hat{p}, hat{q}$$ of $$t, p, q$$ (respectively) in $$mathbb{R}[x,y]$$, and now our condition is that $$hat{t}(y hat{p} – x hat{q}) in (x^2 – y^3)$$. Thus there is some $$b in mathbb{R}[x,y]$$ such that $$hat{t}(y hat{p} – x hat{q}) = b(x^2 – y^3)$$. This is now a question of polynomial arithmetic: does there exist a solution? Remember in the end we need $$t, q notin (x,y)/(x^2 – y^3)$$.